/app/Laravel/Services/ImageUploader.php

https://bitbucket.org/cityserv/techreportph · PHP · 142 lines · 78 code · 30 blank · 34 comment · 12 complexity · 0358862300d01e225dae059b2825b10d MD5 · raw file

  1. <?php
  2. namespace App\Laravel\Services;
  3. /*
  4. *
  5. * Models used for this class
  6. */
  7. /*
  8. *
  9. * Classes used for this class
  10. */
  11. use Carbon, Str, File, Image, AzureStorage, URL;
  12. class ImageUploader {
  13. /**
  14. *
  15. *@param App\Http\Requests\RequestRequest $request
  16. *@param string $request
  17. *
  18. *@return array
  19. */
  20. public static function upload($file, $image_directory = "uploads"){
  21. $storage = env('IMAGE_STORAGE', "file");
  22. switch (Str::lower($storage)) {
  23. case 'file':
  24. // $file = $request->file("file");
  25. $ext = $file->getClientOriginalExtension();
  26. $thumbnail = ['height' => 500, 'width' => 500];
  27. $path_directory = $image_directory."/".Carbon::now()->format('Ymd');
  28. $resized_directory = $image_directory."/".Carbon::now()->format('Ymd')."/resized";
  29. $thumb_directory = $image_directory."/".Carbon::now()->format('Ymd')."/thumbnails";
  30. if (!File::exists($path_directory)){
  31. File::makeDirectory($path_directory, $mode = 0777, true, true);
  32. }
  33. if (!File::exists($resized_directory)){
  34. File::makeDirectory($resized_directory, $mode = 0777, true, true);
  35. }
  36. if (!File::exists($thumb_directory)){
  37. File::makeDirectory($thumb_directory, $mode = 0777, true, true);
  38. }
  39. $filename = Helper::create_filename($ext);
  40. $file->move($path_directory, $filename);
  41. if(in_array($ext, ['jpg','png','jpeg','gif'])){
  42. Image::make("{$path_directory}/{$filename}")->save("{$resized_directory}/{$filename}",95);
  43. Image::make("{$path_directory}/{$filename}")->crop($thumbnail['width'],$thumbnail['height'])->save("{$thumb_directory}/{$filename}",95);
  44. }
  45. return [ "path" => $image_directory, "directory" => URL::to($path_directory), "filename" => $filename ];
  46. break;
  47. case 'azure':
  48. // $file = $request->file('file');
  49. $ext = $file->getClientOriginalExtension();
  50. $thumbnail = ['height' => 500, 'width' => 500];
  51. $path_directory = "{$image_directory}/".Carbon::now()->format('Ymd');
  52. $resized_directory = "{$image_directory}/".Carbon::now()->format('Ymd')."/resized";
  53. $thumb_directory = "{$image_directory}/".Carbon::now()->format('Ymd')."/thumbnails";
  54. if (!File::exists($path_directory)){
  55. File::makeDirectory($path_directory, $mode = 0777, true, true);
  56. }
  57. if (!File::exists($resized_directory)){
  58. File::makeDirectory($resized_directory, $mode = 0777, true, true);
  59. }
  60. if (!File::exists($thumb_directory)){
  61. File::makeDirectory($thumb_directory, $mode = 0777, true, true);
  62. }
  63. $filename = Helper::create_filename($ext);
  64. $new_image_filename = $filename;
  65. $file->move($path_directory, $filename);
  66. // if(Image::make("{$path_directory}/{$filename}")->width() > Image::make("{$path_directory}/{$filename}")->height()){
  67. // Image::make("{$path_directory}/{$filename}")->resize(null, 512, function ($constraint) {
  68. // $constraint->aspectRatio();
  69. // })->orientate()->save("{$resized_directory}/{$filename}",95);
  70. // Image::make("{$path_directory}/{$filename}")->resize(null, 256, function ($constraint) {
  71. // $constraint->aspectRatio();
  72. // })->orientate()->save("{$thumb_directory}/{$filename}",90);
  73. // }else{
  74. // Image::make("{$path_directory}/{$filename}")->resize(512, null, function ($constraint) {
  75. // $constraint->aspectRatio();
  76. // })->orientate()->save("{$resized_directory}/{$filename}",95);
  77. // Image::make("{$path_directory}/{$filename}")->resize(256, null, function ($constraint) {
  78. // $constraint->aspectRatio();
  79. // })->orientate()->save("{$thumb_directory}/{$filename}",90);
  80. // }
  81. if(in_array($ext, ['jpg','png','jpeg','gif'])){
  82. Image::make("{$path_directory}/{$filename}")->save("{$resized_directory}/{$filename}",95);
  83. Image::make("{$path_directory}/{$filename}")->crop($thumbnail['width'],$thumbnail['height'])->save("{$thumb_directory}/{$filename}",95);
  84. }
  85. $client = new AzureStorage(env('BLOB_STORAGE_URL'),env('BLOB_ACCOUNT_NAME'),env('BLOB_ACCESS_KEY'));
  86. $container= env('BLOB_CONTAINER');
  87. $orig_container = env('BLOB_ORIG_CONTAINER');
  88. $directory = env('BLOB_STORAGE_URL')."/".env('BLOB_CONTAINER');
  89. // $new_image_directory = "{$directory}/{$path_directory}";
  90. // $new_image_path = "{$path_directory}";
  91. $new_image_directory = "{$directory}/".str_replace("uploads/", "", $path_directory);
  92. $new_image_path = str_replace("uploads/", "", $path_directory);
  93. $client->putBlob($orig_container, "{$new_image_path}/{$filename}", "{$path_directory}/{$filename}");
  94. $client->putBlob($container, "{$new_image_path}/thumbnails/{$filename}", "{$path_directory}/thumbnails/{$filename}");
  95. $client->putBlob($container, "{$new_image_path}/resized/{$filename}", "{$path_directory}/resized/{$filename}");
  96. if (File::exists("{$path_directory}/{$filename}")){
  97. File::delete("{$path_directory}/{$filename}");
  98. }
  99. if (File::exists("{$path_directory}/thumbnails/{$filename}")){
  100. File::delete("{$path_directory}/thumbnails/{$filename}");
  101. }
  102. if (File::exists("{$path_directory}/resized/{$filename}")){
  103. File::delete("{$path_directory}/resized/{$filename}");
  104. }
  105. return [ "path" => $new_image_path, "directory" => $new_image_directory, "filename" => $new_image_filename ];
  106. break;
  107. default:
  108. return array();
  109. break;
  110. }
  111. }
  112. }