Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

UploadController.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use Image;
  5. use Storage;
  6. use App\Exceptions\Handler;
  7. //load modelmu
  8. class UploadController extends Controller
  9. {
  10. public static function upload($config)
  11. {
  12. /**tempat simpan storagenya ada di ... */
  13. $patern_path = '../storage/app/' . $config['path'];
  14. /**ini khusus storage ya */
  15. Storage::disk('local')->makeDirectory($config['path'], 0777, true);
  16. /**buat variabel untuk simpan responnya */
  17. $result = [];
  18. if (isset($config['file'])) { //apakah filenya ada ?
  19. if (is_array($config['file'])) {
  20. /**jika array / multiple */
  21. foreach ($config['file'] as $key_file => $value_file) {
  22. if (!empty($config['rename'])) {
  23. $rename = $config['rename'] . '-' . $key_file;
  24. } else {
  25. $rename = $config['file'] . '-' . $key_file;
  26. }
  27. $filename = self::rename($rename, $value_file);
  28. $resize = self::is_resize($config['resize'], $value_file, $patern_path, $filename);
  29. $result['file_name'][] = $filename;
  30. $result['path'][] = $config['path'];
  31. }
  32. $result['type'] = 'multiple';
  33. $result['status'] = true;
  34. } else {
  35. // dd($config, $patern_path);
  36. /**jika single file */
  37. /**rename filenya */
  38. $filename = self::rename($config['file'], ($config['rename'] ?? null));
  39. /**jika ada resize */
  40. if (!empty($config['resize'])) {
  41. $resize = self::is_resize($config['resize'], $config['file'], $patern_path, $filename);
  42. } else {
  43. /**jika tidak langsung dimasukkan ke storage */
  44. $file = $config['file']->storeAs($config['path'], $filename);
  45. }
  46. // dd($filename, $resize);
  47. $result['file_name'] = $filename;
  48. $result['path'] = $config['path'];
  49. $result['type'] = 'single';
  50. $result['status'] = true;
  51. }
  52. } else {
  53. $result['status'] = FALSE;
  54. }
  55. // dd($result);
  56. return $result;
  57. }
  58. public static function rename($file, $rename = null)
  59. {
  60. try {
  61. /**rename space dengan - */
  62. $filename = implode('-', explode(' ', $file->getClientOriginalName()));
  63. /**jika rename tersedia maka pakai nama renamenya */
  64. if (!empty($rename)) {
  65. $filename = $rename . '.' . $file->extension();
  66. }
  67. return $filename;
  68. } catch (\Throwable $th) {
  69. Handler::report($th);
  70. }
  71. }
  72. public static function is_resize($resize, $file, $patern_path, $filename)
  73. {
  74. try {
  75. /**cek, apakah resize ada */
  76. if (!empty($resize)) {
  77. /**cek apakah format resize array, jika iya bisa digunakan */
  78. if (is_array($resize)) {
  79. /**resize bisa digunakan jika formatnya png, jpg / jpeg, selain itu no */
  80. if (in_array($file->extension(), ['png', 'jpg', 'jpeg'])) {
  81. $img = Image::make($file->path());
  82. $img->resize($resize[0], $resize[1])->save($patern_path . '/' . $filename);
  83. return $img;
  84. } else {
  85. dd('format harus png, jpg atau jpeg');
  86. }
  87. } else {
  88. dd('format resize harus array, exp => [300,400]');
  89. }
  90. }
  91. } catch (\Throwable $th) {
  92. Handler::report($th);
  93. }
  94. }
  95. public static function get_image($file)
  96. {
  97. return response()->file(storage_path('app/' . ($file)));
  98. }
  99. }