2024-06-01 03:10:49 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Importers\Image\Jobs;
|
|
|
|
|
|
|
|
|
|
use App\Models\Image;
|
|
|
|
|
use Illuminate\Bus\Batchable;
|
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
use Intervention\Image\Laravel\Facades\Image as InterventionImage;
|
|
|
|
|
use \Throwable;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
|
|
class GenerateThumbnail implements ShouldQueue
|
|
|
|
|
{
|
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Batchable;
|
|
|
|
|
|
|
|
|
|
public string $source;
|
|
|
|
|
public string $destination;
|
|
|
|
|
|
|
|
|
|
public function __construct(public Image $image)
|
|
|
|
|
{
|
|
|
|
|
$this->source = Storage::disk('images')->path($this->image->album_id . '/original/' . $this->image->id . '.avif');
|
|
|
|
|
$this->destination = $this->image->album_id . '/thumbnail/' . $this->image->id . '.avif';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function handle(): void
|
|
|
|
|
{
|
2024-06-12 19:51:41 +02:00
|
|
|
if (method_exists($this, 'batch') && $this->batch()?->cancelled()) {
|
2024-06-01 03:10:49 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-07 16:26:15 +02:00
|
|
|
$thumbnail = InterventionImage::read($this->source);
|
|
|
|
|
$thumbnail = $thumbnail->scaleDown(height: config('gallery.image.thumbnail.height', 640), width: config('gallery.image.thumbnail.width', 640));
|
2024-06-01 03:10:49 +02:00
|
|
|
|
2024-06-07 16:26:15 +02:00
|
|
|
Storage::disk('images')->put($this->destination, $thumbnail->toAvif(config('gallery.image.quality', 80)));
|
2024-06-01 03:10:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function failed(?Throwable $exception): void
|
|
|
|
|
{
|
|
|
|
|
$this->image->delete();
|
|
|
|
|
|
|
|
|
|
Log::error($exception, [
|
|
|
|
|
'image' => $this->image,
|
|
|
|
|
'source' => $this->source,
|
|
|
|
|
'destination' => $this->destination
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|