/app/libraries/common/Image_Library.php

https://gitlab.com/jeamar1234/medicloud · PHP · 198 lines · 155 code · 30 blank · 13 comment · 18 complexity · 7471c2c77f64bb035c8359f26e76ab94 MD5 · raw file

  1. <?php
  2. use \Eventviva\ImageResize;
  3. class Image_Library{
  4. public static function ImageUpload($destinationPath,$file){
  5. if(!empty($file)){
  6. $rules = array(
  7. 'file' => 'required|mimes:png,gif,jpeg,jpg|max:20000'
  8. );
  9. $validator = \Validator::make( Input::all() , $rules);
  10. if($validator->passes()){
  11. //$destinationPath = self::FindUploadLocation($usertype);
  12. //$destinationPath = public_path('assets/upload/user/');
  13. $filename = str_random(32) .time(). '.' . 'png';
  14. $uploaded = $file->move($destinationPath, $filename);
  15. if($uploaded){
  16. return $filename;
  17. }else{
  18. return FALSE;
  19. }
  20. }else{
  21. return FALSE;
  22. }
  23. }else{
  24. return FALSE;
  25. }
  26. }
  27. public static function CloudinaryImageUpload($filepath){
  28. //$up = \Cloudinary\Uploader::upload($filepath,array("crop" => "scale", "width" => 100,"format" => "png"));
  29. $up = \Cloudinary\Uploader::upload($filepath);
  30. if(!empty($up)){
  31. return $up['secure_url'];
  32. }else{
  33. return FALSE;
  34. }
  35. }
  36. public static function CloudinaryImageUploadResize($filepath,$width,$height){
  37. //$up = \Cloudinary\Uploader::upload($filepath,array("crop" => "scale", "width" => 100,"format" => "png"));
  38. $up = \Cloudinary\Uploader::upload($filepath,array("width" => $width, "height" => $height));
  39. if(!empty($up)){
  40. return $up['secure_url'];
  41. }else{
  42. return FALSE;
  43. }
  44. }
  45. public static function CloudinaryUpload(){
  46. $file = Input::file('file');
  47. if(!empty($file)){
  48. $rules = array(
  49. 'file' => 'required|mimes:png,gif,jpeg,jpg|max:20000'
  50. );
  51. $validator = \Validator::make( Input::all() , $rules);
  52. if($validator->passes()){
  53. $upload = self::CloudinaryImageUpload($file->getPathName());
  54. if(!empty($upload)){
  55. return $upload;
  56. }else{
  57. return FALSE;
  58. }
  59. }else{
  60. return FALSE;
  61. }
  62. }else{
  63. return FALSE;
  64. }
  65. }
  66. public static function CloudinaryUploadWithResize($width,$height){
  67. $file = Input::file('file');
  68. if(!empty($file)){
  69. $rules = array(
  70. 'file' => 'required|mimes:png,gif,jpeg,jpg|max:20000'
  71. );
  72. $validator = \Validator::make( Input::all() , $rules);
  73. if($validator->passes()){
  74. //$upload = self::CloudinaryImageUpload($file->getPathName());
  75. $upload = self::CloudinaryImageUploadResize($file->getPathName(),$width,$height);
  76. if(!empty($upload)){
  77. return $upload;
  78. }else{
  79. return FALSE;
  80. }
  81. }else{
  82. return FALSE;
  83. }
  84. }else{
  85. return FALSE;
  86. }
  87. }
  88. public static function CloudinaryUploadFile($file){
  89. // $file = Input::file('file');
  90. if(!empty($file)){
  91. $rules = array(
  92. 'clinic_price' => 'required|mimes:png,gif,jpeg,jpg|max:20000'
  93. );
  94. $validator = \Validator::make( Input::all() , $rules);
  95. if($validator->passes()){
  96. $upload = self::CloudinaryImageUpload($file->getPathName());
  97. if(!empty($upload)){
  98. return $upload;
  99. }else{
  100. return FALSE;
  101. }
  102. }else{
  103. return FALSE;
  104. }
  105. }else{
  106. return FALSE;
  107. }
  108. }
  109. public static function ImageResizing($usertype,$with){
  110. $file = Input::file('file');
  111. $destinationPath = self::FindUploadLocation($usertype);
  112. $imageUpload = self::ImageUpload($destinationPath,$file);
  113. if($imageUpload){
  114. $image = new ImageResize($destinationPath.$imageUpload);
  115. $image->resizeToWidth($with);
  116. $imageSaved = $image->save($destinationPath.$imageUpload, IMAGETYPE_PNG);
  117. if($imageSaved){
  118. return $imageUpload;
  119. }else{
  120. return FALSE;
  121. }
  122. }else{
  123. return FALSE;
  124. }
  125. }
  126. private static function FindUploadLocation($usertype){
  127. if($usertype ==1){
  128. $destinationPath = public_path('assets/upload/user/');
  129. }elseif($usertype ==2){
  130. $destinationPath = public_path('assets/upload/doctor/');
  131. }elseif($usertype ==3){
  132. $destinationPath = public_path('assets/upload/clinic/');
  133. }
  134. return $destinationPath;
  135. }
  136. private static function cleanFileName($fileName){
  137. //remove blanks
  138. $fileName = preg_replace('/\s+/', '', $fileName);
  139. //remove charactes
  140. $fileName = preg_replace("/[^A-Za-z0-9_-\s.]/", "", $fileName);
  141. return $fileName;
  142. }
  143. public static function ImageResizing1(){
  144. $file = Input::file('file');
  145. echo '<pre>';
  146. print_r($file);
  147. echo '</pre>'; echo '<br>';
  148. $rules = array(
  149. 'file' => 'required|mimes:png,gif,jpeg,jpg|max:20000'
  150. );
  151. $validator = \Validator::make( Input::all() , $rules);
  152. if($validator->passes()){
  153. //$originalName = $file->getClientOriginalName();
  154. //$tempPath = $file->getRealPath().$originalName;
  155. $destinationPath = public_path('assets/upload/user/');
  156. //$filename = str_random(32) .time(). '.' . $file->getClientOriginalExtension();
  157. $filename = str_random(32) .time(). '.' . 'png';
  158. $uploaded = $file->move($destinationPath, $filename);
  159. $image = new ImageResize($destinationPath.$filename);
  160. //$image = new ImageResize($filename);
  161. $image->resizeToWidth(150);
  162. //$image->save($destinationPath.$filename);
  163. $image->save($destinationPath.$filename, IMAGETYPE_PNG);
  164. }else{
  165. echo 'fail';
  166. }
  167. }
  168. }