75 lines
2.5 KiB
PHP
75 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Enhance\Processors;
|
|
|
|
use App\Models\EnhanceJob;
|
|
use App\Services\Enhance\EnhanceProcessor;
|
|
use App\Services\Enhance\EnhanceProcessorResult;
|
|
use App\Services\Enhance\EnhanceStorageService;
|
|
use Intervention\Image\Drivers\Gd\Driver as GdDriver;
|
|
use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver;
|
|
use Intervention\Image\Encoders\WebpEncoder;
|
|
use Intervention\Image\ImageManager;
|
|
|
|
final class StubEnhanceProcessor implements EnhanceProcessor
|
|
{
|
|
private ?ImageManager $manager = null;
|
|
|
|
public function __construct(
|
|
private readonly EnhanceStorageService $storage,
|
|
) {
|
|
try {
|
|
$this->manager = extension_loaded('gd')
|
|
? new ImageManager(new GdDriver())
|
|
: new ImageManager(new ImagickDriver());
|
|
} catch (\Throwable) {
|
|
$this->manager = null;
|
|
}
|
|
}
|
|
|
|
public function process(EnhanceJob $job): EnhanceProcessorResult
|
|
{
|
|
$sourceBinary = $this->storage->fetchSourceBinary($job);
|
|
$outputBinary = $sourceBinary;
|
|
$outputMime = (string) ($job->input_mime ?: 'image/jpeg');
|
|
$scale = max(1, (int) $job->scale);
|
|
$metadata = [
|
|
'stub' => true,
|
|
'engine' => EnhanceJob::ENGINE_STUB,
|
|
'requested_scale' => $scale,
|
|
];
|
|
|
|
if ($this->manager !== null) {
|
|
try {
|
|
$image = $this->manager->read($sourceBinary);
|
|
$targetWidth = max((int) $image->width(), (int) $image->width() * $scale);
|
|
$targetHeight = max((int) $image->height(), (int) $image->height() * $scale);
|
|
$outputBinary = (string) $image
|
|
->resize($targetWidth, $targetHeight)
|
|
->encode(new WebpEncoder(88));
|
|
$outputMime = 'image/webp';
|
|
$metadata['actual_scale'] = $scale;
|
|
} catch (\Throwable) {
|
|
$metadata['actual_scale'] = 1;
|
|
$metadata['fallback'] = 'source-copy';
|
|
}
|
|
} else {
|
|
$metadata['actual_scale'] = 1;
|
|
$metadata['fallback'] = 'source-copy';
|
|
}
|
|
|
|
$stored = $this->storage->putOutputBinary($job, $outputBinary, $outputMime);
|
|
|
|
return new EnhanceProcessorResult(
|
|
disk: $stored['disk'],
|
|
path: $stored['path'],
|
|
width: (int) $stored['width'],
|
|
height: (int) $stored['height'],
|
|
filesize: (int) $stored['filesize'],
|
|
mime: (string) $stored['mime'],
|
|
metadata: $metadata,
|
|
);
|
|
}
|
|
} |