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 + } + } +}