Compare commits
3 Commits
58b53a0284
...
f402c0831a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f402c0831a | ||
|
|
ceca0aa5e8 | ||
|
|
43361f81e7 |
@@ -20,6 +20,7 @@
|
||||
use Modules\Usermanagement\Models\User;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Modules\Lpj\Services\ImageResizeService;
|
||||
|
||||
/**
|
||||
* Format tanggal ke dalam format Bahasa Indonesia
|
||||
@@ -775,3 +776,20 @@
|
||||
Log::error('Tidak dapat memparsing timestamp dengan format apapun: "' . $timestamp . '"');
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!function_exists('resize_image')) {
|
||||
/**
|
||||
* Merubah ukuran gambar secara on-the-fly dan mengembalikan path-nya.
|
||||
*
|
||||
* @param string $path Path asli gambar.
|
||||
* @param int|null $width Lebar yang diinginkan.
|
||||
* @param int|null $height Tinggi yang diinginkan (opsional, akan menjaga rasio aspek jika null).
|
||||
* @param int $quality Kualitas gambar (1-100).
|
||||
* @return string Path gambar yang sudah di-resize.
|
||||
*/
|
||||
function resize_image(string $path, ?int $width, ?int $height = null, int $quality = 80): string
|
||||
{
|
||||
|
||||
return app(ImageResizeService::class)->resize($path, $width, $height, $quality);
|
||||
}
|
||||
}
|
||||
|
||||
73
app/Services/ImageResizeService.php
Normal file
73
app/Services/ImageResizeService.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Lpj\Services;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Intervention\Image\Laravel\Facades\Image;
|
||||
|
||||
class ImageResizeService
|
||||
{
|
||||
/**
|
||||
* Resize an image and store it.
|
||||
*
|
||||
* @param string $originalPath
|
||||
* @param int $width
|
||||
* @param int|null $height
|
||||
* @param int $quality
|
||||
* @return string
|
||||
*/
|
||||
public function resize(string $originalPath, ?int $width, ?int $height = null, int $quality = 80): string
|
||||
{
|
||||
if (empty($originalPath) || !Storage::disk('public')->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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -347,6 +347,7 @@ class PreviewLaporanService
|
||||
$pdf->setPaper('A4', 'portrait');
|
||||
$pdf->set_option('isHtml5ParserEnabled', true);
|
||||
$pdf->set_option('isPhpEnabled', true);
|
||||
$pdf->set_option('dpi', '96');
|
||||
|
||||
return $pdf;
|
||||
}
|
||||
|
||||
@@ -154,10 +154,12 @@
|
||||
? $fallbackPath
|
||||
: $originalPath;
|
||||
|
||||
$resizedPath = resize_image($pathToUse, 800, 400, 25);
|
||||
|
||||
$filePath =
|
||||
$statusLpj == 1
|
||||
? storage_path('app/public/' . $pathToUse)
|
||||
: asset('storage/' . $pathToUse);
|
||||
? storage_path('app/public/' . $resizedPath)
|
||||
: asset('storage/' . $resizedPath);
|
||||
|
||||
$extension = strtolower(pathinfo($pathToUse, PATHINFO_EXTENSION));
|
||||
$isImage = in_array($extension, [
|
||||
@@ -248,12 +250,16 @@
|
||||
style="align-content: center; text-align: center; margin-bottom: 20px">
|
||||
@foreach ($chunkedPhotos as $item)
|
||||
@php
|
||||
$originalPath = $item['path'];
|
||||
$resizedPath = resize_image($originalPath, 800, 400, 25);
|
||||
|
||||
|
||||
$filePath =
|
||||
$statusLpj == 1
|
||||
? storage_path('app/public/' . $item['path'])
|
||||
: asset('storage/' . $item['path']);
|
||||
? storage_path('app/public/' . $resizedPath)
|
||||
: asset('storage/' . $resizedPath);
|
||||
|
||||
$extension = strtolower(pathinfo($item['path'], PATHINFO_EXTENSION));
|
||||
$extension = strtolower(pathinfo($originalPath, PATHINFO_EXTENSION));
|
||||
$isImage = in_array($extension, [
|
||||
'jpg',
|
||||
'jpeg',
|
||||
|
||||
Reference in New Issue
Block a user