penjadwalan-ga/app/Jobs/CompressLogbookImages.php

45 lines
1.4 KiB
PHP

<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable; // ✅ ini yang benar
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; // ✅ bukan dari Queue
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Laravel\Facades\Image;
use Carbon\Carbon;
class CompressLogbookImages implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function handle(): void
{
$directory = storage_path('app/public/foto-logbook');
$files = glob($directory . '/*.{jpg,jpeg,png,webp}', GLOB_BRACE);
$batch = array_chunk($files, 100);
$total = 0;
foreach ($batch as $index => $chunk) {
foreach ($chunk as $fullPath) {
try {
$image = Image::read($fullPath)
->scaleDown(width: 800)
->save($fullPath, quality: 85);
$total++;
} catch (\Exception $e) {
Log::error("Gagal resize {$fullPath}: " . $e->getMessage());
}
}
Log::info("Batch ke-".($index+1)." selesai diproses ({$total} gambar total)");
sleep(2);
}
Log::info("Resize selesai total {$total} gambar");
}
}