MediaHandler: add handle indirection

This commit is contained in:
Zankaria 2025-03-18 11:31:23 +01:00
parent f3ce9bf055
commit 461650a665
3 changed files with 18 additions and 4 deletions

View file

@ -23,9 +23,12 @@ class FallbackMediaHandler implements MediaHandler {
return true; return true;
} }
public function openHandle(string $file_path, string $file_mime): mixed {
return null;
}
public function generateThumb( public function generateThumb(
string $source_file_path, mixed $handle,
string $source_file_mime,
string $preferred_out_file_path, string $preferred_out_file_path,
string $preferred_out_mime, string $preferred_out_mime,
int $max_width, int $max_width,

View file

@ -81,14 +81,23 @@ class GdMediaHandler implements MediaHandler {
|| ($mime === 'image/avif' && self::PHP81 && $info['AVIF Support']); || ($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( public function generateThumb(
string $source_file_path, mixed $handle,
string $source_file_mime,
string $preferred_out_file_path, string $preferred_out_file_path,
string $preferred_out_mime, string $preferred_out_mime,
int $max_width, int $max_width,
int $max_height int $max_height
): ThumbGenerationResult { ): ThumbGenerationResult {
list($gd, $source_file_path, $source_file_mime) = $handle;
$gd = self::imageCreateFrom($source_file_path, $source_file_mime); $gd = self::imageCreateFrom($source_file_path, $source_file_mime);
if ($gd === false) { if ($gd === false) {
throw new \RuntimeException("Could not open '$source_file_path'"); throw new \RuntimeException("Could not open '$source_file_path'");

View file

@ -7,6 +7,8 @@ use Vichan\Data\ThumbGenerationResult;
interface MediaHandler { interface MediaHandler {
public function supportsMime(string $mime): bool; public function supportsMime(string $mime): bool;
public function openHandle(string $file_path, string $file_mime): mixed;
/** /**
* Generates a thumbnail from the given file. * Generates a thumbnail from the given file.
* *