From 43361f81e7f4bba8d5ef922e420805d8d9542de2 Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Wed, 24 Dec 2025 11:13:28 +0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20feat(ImageResizeService):=20Tamb?= =?UTF-8?q?ahkan=20service=20resize=20gambar=20dengan=20fitur=20kualitas?= =?UTF-8?q?=20dan=20aspect=20ratio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Menambahkan ImageResizeService untuk mengelola proses resize gambar dengan fitur: - Resize gambar dengan menjaga aspect ratio atau force resize - Support resize berdasarkan width saja, height saja, atau keduanya - Kustomisasi kualitas kompresi gambar (default 80%) - Penyimpanan hasil resize di folder terpisah ('resized') - Nama file yang deskriptif dengan format: {filename}_{width}x{height}_{quality}.{extension} - Error handling dengan fallback ke gambar original - Logging untuk monitoring proses resize Service ini menggunakan Intervention Image Laravel untuk manipulasi gambar dan Laravel Storage untuk manajemen file. Cocok untuk optimasi gambar di aplikasi LPJ module. --- app/Services/ImageResizeService.php | 73 +++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 app/Services/ImageResizeService.php diff --git a/app/Services/ImageResizeService.php b/app/Services/ImageResizeService.php new file mode 100644 index 0000000..d22d7a5 --- /dev/null +++ b/app/Services/ImageResizeService.php @@ -0,0 +1,73 @@ +exists($originalPath)) { + Log::warning("Image Service: Original path is empty or does not exist: {$originalPath}"); + return ''; + } + + $height = null; + + $pathinfo = pathinfo($originalPath); + + // Kembali menggunakan direktori 'resized' dan menyertakan dimensi di nama file + $resizedPath = "{$pathinfo['dirname']}/resized/{$pathinfo['filename']}_{$width}x{$height}_{$quality}.{$pathinfo['extension']}"; + + if (Storage::disk('public')->exists($resizedPath)) { + return $resizedPath; + } + + try { + $originalFullPath = Storage::disk('public')->path($originalPath); + $newFullPath = Storage::disk('public')->path($resizedPath); + + $image = Image::read($originalFullPath); + + // Resize dengan menjaga aspek rasio jika height null + if ($width && $height) { + // Paksa resize ke dimensi yang ditentukan, abaikan aspek rasio + $image->resize($width, $height); + } elseif ($width && !$height) { + // Resize hanya berdasarkan width, tinggi menyesuaikan aspek rasio + $image->scale(width: $width); + } elseif (!$width && $height) { + // Resize hanya berdasarkan height, lebar menyesuaikan aspek rasio + $image->scale(height: $height); + } + + + + if (!Storage::disk('public')->exists(dirname($resizedPath))) { + Storage::disk('public')->makeDirectory(dirname($resizedPath)); + } + + // Simpan dengan kualitas yang ditentukan + $image->save($newFullPath, $quality); + + Log::info("Image Service: Successfully resized {$originalPath} to {$resizedPath} with quality {$quality}%."); + + return $resizedPath; + } catch (\Exception $e) { + Log::error("Image Service: Resize failed for {$originalPath}. Error: " . $e->getMessage()); + return $originalPath; // Fallback ke gambar asli jika gagal + } + } +}