Gambar.php
2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?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));
// }
// }
// }
}
}