From 46448263469b5b803948e3461bb77b82fe94da7e Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Thu, 4 Apr 2024 17:19:14 +1300 Subject: [PATCH] NEW File converter API --- src/Conversion/FileConverter.php | 23 +++ src/Conversion/InterventionImageConverter.php | 164 ++++++++++++++++++ 2 files changed, 187 insertions(+) create mode 100644 src/Conversion/FileConverter.php create mode 100644 src/Conversion/InterventionImageConverter.php diff --git a/src/Conversion/FileConverter.php b/src/Conversion/FileConverter.php new file mode 100644 index 00000000..ed9c1287 --- /dev/null +++ b/src/Conversion/FileConverter.php @@ -0,0 +1,23 @@ +getImageBackend(); + $from = $from->getExtension() ?: $from->getMimeType(); + } else { + $backend = Injector::inst()->get(Image_Backend::class); + } + // This converter requires intervention image + if (!is_a($backend, InterventionBackend::class)) { + return false; + } + /** @var InterventionBackend $backend */ + $driver = $backend->getImageManager()->config['driver'] ?? null; + return $this->supportedByIntervention($from, $toExtension, $driver); + } + + public function convert(DBFile $original, string $toExtension, array $options = []): DBFile + { + // TODO allow swapping quality + // TODO find any other options intervention supports + return $original->manipulateExtension( + $toExtension, + function (AssetStore $store, string $filename, string $hash, string $variant) use ($original) { + $backend = $original->getImageBackend(); + $config = ['conflict' => AssetStore::CONFLICT_USE_EXISTING]; + $tuple = $backend->writeToStore($store, $filename, $hash, $variant, $config); + return [$tuple, $backend]; + } + ); + } + + private function supportedByIntervention(string $from, string $to, string $driver): bool + { + // If the driver is somehow not GD or Imagick, we have no way to know what it might support + if ($driver !== 'gd' && $driver !== 'imagick') { + return false; + } + + foreach ([$from, $to] as $format) { + // This follows the logic in intervention's AbstractEncoder::process() method + // and the various methods in the Encoder classes for GD and Imagick + switch (strtolower($format)) { + case 'gif': + case 'image/gif': + // always supported + break; + case 'png': + case 'image/png': + case 'image/x-png': + // always supported + break; + case 'jpg': + case 'jpeg': + case 'jfif': + case 'image/jp2': + case 'image/jpg': + case 'image/jpeg': + case 'image/pjpeg': + case 'image/jfif': + // always supported + break; + case 'tif': + case 'tiff': + case 'image/tiff': + case 'image/tif': + case 'image/x-tif': + case 'image/x-tiff': + if ($driver === 'gd') { + false; + } + // always supported by imagick + break; + case 'bmp': + case 'ms-bmp': + case 'x-bitmap': + case 'x-bmp': + case 'x-ms-bmp': + case 'x-win-bitmap': + case 'x-windows-bmp': + case 'x-xbitmap': + case 'image/ms-bmp': + case 'image/x-bitmap': + case 'image/x-bmp': + case 'image/x-ms-bmp': + case 'image/x-win-bitmap': + case 'image/x-windows-bmp': + case 'image/x-xbitmap': + if ($driver === 'gd' && !function_exists('imagebmp')) { + return false; + } + // always supported by imagick + break; + case 'ico': + case 'image/x-ico': + case 'image/x-icon': + case 'image/vnd.microsoft.icon': + if ($driver === 'gd') { + return false; + } + // always supported by imagick + break; + case 'psd': + case 'image/vnd.adobe.photoshop': + if ($driver === 'gd') { + return false; + } + // always supported by imagick + break; + case 'webp': + case 'image/webp': + case 'image/x-webp': + if ($driver === 'gd' && !function_exists('imagewebp')) { + return false; + } + if ($driver === 'imagick' && !\Imagick::queryFormats('WEBP')) { + return false; + } + break; + case 'avif': + case 'image/avif': + if ($driver === 'gd' && !function_exists('imageavif')) { + return false; + } + if ($driver === 'imagick' && !\Imagick::queryFormats('AVIF')) { + return false; + } + break; + case 'heic': + case 'image/heic': + case 'image/heif': + if ($driver === 'gd') { + return false; + } + if ($driver === 'imagick' && !\Imagick::queryFormats('HEIC')) { + return false; + } + break; + default: + // Anything else is not supported + return false; + } + } + // If we get here both formats are in the explicit list above and are supported + return true; + } +}