From 77f5ebf32498446758641d3bb81115d9fbfdc9fc 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 | 71 +++++++++++++++++++ 2 files changed, 94 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..c918204e --- /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; + switch ($driver) { + case 'gd': + return $this->supportedByGd($from, $toExtension); + case 'imagick': + return $this->supportedByImagick($from, $toExtension); + // If the driver is somehow not GD or Imagick, we have no way to know what it might support + default: + return false; + } + // We shouldn't get here, but if we do, we clearly don't know what's going on and therefore can't support it + return false; + } + + 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 supportedByGd(string $from, string $to): bool + { + // TODO: Follow the logic in AbstractEncoder::process() and the various methods in Encoder + return false; + } + + private function supportedByImagick(string $from, string $to): bool + { + // TODO: Follow the logic in AbstractEncoder::process() and the various methods in Encoder + return false; + } +}