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, ); } }