123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Http\Controllers\Controller;
- use Image;
- use Storage;
- use App\Exceptions\Handler;
-
- //load modelmu
- class UploadController extends Controller
- {
- public static function upload($config)
- {
- /**tempat simpan storagenya ada di ... */
- $patern_path = '../storage/app/' . $config['path'];
- /**ini khusus storage ya */
- Storage::disk('local')->makeDirectory($config['path'], 0777, true);
- /**buat variabel untuk simpan responnya */
- $result = [];
- if (isset($config['file'])) { //apakah filenya ada ?
- if (is_array($config['file'])) {
-
- /**jika array / multiple */
- foreach ($config['file'] as $key_file => $value_file) {
-
- if (!empty($config['rename'])) {
- $rename = $config['rename'] . '-' . $key_file;
- } else {
- $rename = $config['file'] . '-' . $key_file;
- }
- $filename = self::rename($rename, $value_file);
- $resize = self::is_resize($config['resize'], $value_file, $patern_path, $filename);
- $result['file_name'][] = $filename;
- $result['path'][] = $config['path'];
- }
- $result['type'] = 'multiple';
- $result['status'] = true;
- } else {
- // dd($config, $patern_path);
- /**jika single file */
- /**rename filenya */
- $filename = self::rename($config['file'], ($config['rename'] ?? null));
- /**jika ada resize */
- if (!empty($config['resize'])) {
- $resize = self::is_resize($config['resize'], $config['file'], $patern_path, $filename);
- } else {
- /**jika tidak langsung dimasukkan ke storage */
- $file = $config['file']->storeAs($config['path'], $filename);
- }
- // dd($filename, $resize);
-
- $result['file_name'] = $filename;
- $result['path'] = $config['path'];
- $result['type'] = 'single';
- $result['status'] = true;
- }
- } else {
- $result['status'] = FALSE;
- }
- // dd($result);
- return $result;
- }
-
- public static function rename($file, $rename = null)
- {
- try {
- /**rename space dengan - */
- $filename = implode('-', explode(' ', $file->getClientOriginalName()));
- /**jika rename tersedia maka pakai nama renamenya */
- if (!empty($rename)) {
-
- $filename = $rename . '.' . $file->extension();
- }
- return $filename;
- } catch (\Throwable $th) {
- Handler::report($th);
- }
- }
-
- public static function is_resize($resize, $file, $patern_path, $filename)
- {
- try {
-
- /**cek, apakah resize ada */
- if (!empty($resize)) {
- /**cek apakah format resize array, jika iya bisa digunakan */
- if (is_array($resize)) {
-
- /**resize bisa digunakan jika formatnya png, jpg / jpeg, selain itu no */
- if (in_array($file->extension(), ['png', 'jpg', 'jpeg'])) {
-
- $img = Image::make($file->path());
- $img->resize($resize[0], $resize[1])->save($patern_path . '/' . $filename);
- return $img;
- } else {
- dd('format harus png, jpg atau jpeg');
- }
- } else {
- dd('format resize harus array, exp => [300,400]');
- }
- }
- } catch (\Throwable $th) {
- Handler::report($th);
- }
- }
-
- public static function get_image($file)
- {
- return response()->file(storage_path('app/' . ($file)));
- }
- }
|