From 461650a665cf47e8479f4f8292dcd4f61df32d73 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Tue, 18 Mar 2025 11:31:23 +0100 Subject: [PATCH] MediaHandler: add handle indirection --- inc/Service/Media/FallbackMediaHandler.php | 7 +++++-- inc/Service/Media/GdMediaHandler.php | 13 +++++++++++-- inc/Service/Media/MediaHandler.php | 2 ++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/inc/Service/Media/FallbackMediaHandler.php b/inc/Service/Media/FallbackMediaHandler.php index f1f94bf0..cbd4a07a 100644 --- a/inc/Service/Media/FallbackMediaHandler.php +++ b/inc/Service/Media/FallbackMediaHandler.php @@ -23,9 +23,12 @@ class FallbackMediaHandler implements MediaHandler { return true; } + public function openHandle(string $file_path, string $file_mime): mixed { + return null; + } + public function generateThumb( - string $source_file_path, - string $source_file_mime, + mixed $handle, string $preferred_out_file_path, string $preferred_out_mime, int $max_width, diff --git a/inc/Service/Media/GdMediaHandler.php b/inc/Service/Media/GdMediaHandler.php index a544ebf5..62f82d6c 100644 --- a/inc/Service/Media/GdMediaHandler.php +++ b/inc/Service/Media/GdMediaHandler.php @@ -81,14 +81,23 @@ class GdMediaHandler implements MediaHandler { || ($mime === 'image/avif' && self::PHP81 && $info['AVIF Support']); } + public function openHandle(string $file_path, string $file_mime): mixed { + $gd = self::imageCreateFrom($file_path, $file_mime); + if ($gd === false) { + throw new \RuntimeException("Could not open '$file_path'"); + } + return [ $gd, $file_path, $file_mime ]; + } + public function generateThumb( - string $source_file_path, - string $source_file_mime, + mixed $handle, string $preferred_out_file_path, string $preferred_out_mime, int $max_width, int $max_height ): ThumbGenerationResult { + list($gd, $source_file_path, $source_file_mime) = $handle; + $gd = self::imageCreateFrom($source_file_path, $source_file_mime); if ($gd === false) { throw new \RuntimeException("Could not open '$source_file_path'"); diff --git a/inc/Service/Media/MediaHandler.php b/inc/Service/Media/MediaHandler.php index dcd935b5..f668e35e 100644 --- a/inc/Service/Media/MediaHandler.php +++ b/inc/Service/Media/MediaHandler.php @@ -7,6 +7,8 @@ use Vichan\Data\ThumbGenerationResult; interface MediaHandler { public function supportsMime(string $mime): bool; + public function openHandle(string $file_path, string $file_mime): mixed; + /** * Generates a thumbnail from the given file. *