Gambar.php 2.42 KB
<?php

namespace App\Helpers;

use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Storage;

class Gambar
{
    public static function generate($model)
    {
        try {
           $img = Image::make(Storage::disk($model->file_disk)->get($model->file_target));
            $img->backup();
            foreach ($model->ukuran as $ukuran) {
                if(config('master.ukuran.'.$ukuran.'.width') == NULL)
                {
                    $img->resize(null, config('master.ukuran.'.$ukuran.'.height'), function ($constraint) {
                        $constraint->aspectRatio();
                    });
                } elseif (config('master.ukuran.'.$ukuran.'.height') == NULL)
                {
                    $img->resize(config('master.ukuran.'.$ukuran.'.width'), null, function ($constraint) {
                        $constraint->aspectRatio();
                    });
                } else {
                    $img->resize(config('master.ukuran.'.$ukuran.'.width'),config('master.ukuran.'.$ukuran.'.height'));
                }
                $folder = 'panitia/'.date('Y').'/'.date('m').'/'.date('d').'/';
                Storage::makeDirectory($folder);
                Storage::put($folder . $ukuran.'-'.$model->file_nama, $img->encode('jpg'));
                $img->reset();
            }
        } catch (\Throwable $th) {
            //throw $th;
        }
    }

    public static function hapus($model)
    {
        try {
            if(Storage::disk($model->file_disk)->exists($model->file_target))
            {
                Storage::disk($model->file_disk)->delete($model->file_target);
                foreach ($model->ukuran as $ukuran) {
                    Storage::disk($model->file_disk)->delete($model->file_path . '/'. $ukuran.'-'.$model->file_nama);
                }
            }
        } catch (\Throwable $th) {
            //throw $th;
        }

        // $storage = $foto == TRUE ? storage_path($model->foto_path.'/'.$model->foto_nama):$model->file_path_nama;
        // if (file_exists($storage)) {
        //     unlink($storage);
        // }
        // if ($foto == TRUE) {
        //     foreach ($model->ukuran() as $ukuran) {
        //         if (file_exists(storage_path($model->foto_path.'/'.$ukuran.'-'.$model->foto_nama))) {
        //             unlink(storage_path($model->foto_path.'/'.$ukuran.'-'.$model->foto_nama));
        //         }
        //     }
        // }
    }

}