LibMagickMediaHandler.php: refactor inner implementation

This commit is contained in:
Zankaria 2025-03-25 15:15:09 +01:00
parent 0a09144d88
commit 66dd81537a

View file

@ -80,85 +80,18 @@ class LibMagickMediaHandler implements MediaHandler {
return $degrees == 0 || $degrees == 180;
}
public static function checkImagickVersion(): bool {
$imagick_ver = \phpversion('imagick');
if ($imagick_ver !== false && \version_compare($imagick_ver, self::MIN_IMAGICK_VERSION, '>=')) {
$str = \Imagick::getVersion()['versionString'];
if (\preg_match('/ImageMagick ([0-9]+\.[0-9]+\.[0-9]+)/', $str, $matches)) {
return \version_compare($matches[1], self::MIN_IMAGEMAGICK_VERSION, '>=');
}
}
return false;
}
public function __construct(bool $strip_metadata, int $frames_for_gif_thumbs) {
$this->strip_metadata = $strip_metadata;
$this->frames_for_gif_thumbs = $frames_for_gif_thumbs;
}
public function supportsMime(string $mime): bool {
$ext = Mime\mime_to_ext($mime);
if ($ext === null) {
return false;
}
return !empty(\Imagick::queryFormats(\strtoupper($ext)));
}
public function openHandle(string $file_path, string $file_mime, int $file_kind): mixed {
$ext = Mime\mime_to_ext($file_mime);
$path = \realpath($file_path);
// Open it as the supplied mime type.
$imagick = new \Imagick("$ext:$path");
return [ $imagick, $file_path, $file_mime, $file_kind ];
}
public function closeHandle(mixed $handle) {
$handle[0]->clear();
}
public function generateThumb(
mixed $handle,
private function generateThumbImpl(
\Imagick $imagick,
string $source_file_path,
string $source_file_mime,
string $preferred_out_file_dir,
string $preferred_out_file_name,
string $preferred_out_mime,
int $width,
int $height,
int $max_width,
int $max_height
): ThumbGenerationResult {
list($imagick, $source_file_path, $source_file_mime, $source_file_kind) = $handle;
$width = $imagick->getImageWidth();
$height = $imagick->getImageHeight();
if (!$this->strip_metadata && $width <= $max_width && $height <= $max_height) {
$out_path = $preferred_out_file_dir . \DIRECTORY_SEPARATOR . $preferred_out_file_name . '.' . Mime\mime_to_ext($source_file_mime);
if ($source_file_kind === self::FILE_KIND_UPLOADED) {
if (!Fs\move_or_copy_uploaded($source_file_path, $out_path)) {
throw new \RuntimeException("Could not move or copy uploaded file '$source_file_path' to '$out_path'");
}
} else {
if (!Fs\link_or_copy($source_file_path, $out_path)) {
throw new \RuntimeException("Could not link or copy '$source_file_path' to '$out_path'");
}
}
return new ThumbGenerationResult(
$out_path,
$source_file_mime,
false,
$max_width,
$max_height
);
} else {
$swap = self::adjustOrientation($imagick);
if ($swap) {
$tmp = $width;
$width = $height;
$height = $tmp;
}
) {
if (
$source_file_mime === 'image/gif'
&& $this->frames_for_gif_thumbs !== self::THUMB_KEEP_FRAMES_NO
@ -241,6 +174,99 @@ class LibMagickMediaHandler implements MediaHandler {
);
}
}
public static function checkImagickVersion(): bool {
$imagick_ver = \phpversion('imagick');
if ($imagick_ver !== false && \version_compare($imagick_ver, self::MIN_IMAGICK_VERSION, '>=')) {
$str = \Imagick::getVersion()['versionString'];
if (\preg_match('/ImageMagick ([0-9]+\.[0-9]+\.[0-9]+)/', $str, $matches)) {
return \version_compare($matches[1], self::MIN_IMAGEMAGICK_VERSION, '>=');
}
}
return false;
}
public function __construct(bool $strip_metadata, int $frames_for_gif_thumbs) {
$this->strip_metadata = $strip_metadata;
$this->frames_for_gif_thumbs = $frames_for_gif_thumbs;
}
public function supportsMime(string $mime): bool {
$ext = Mime\mime_to_ext($mime);
if ($ext === null) {
return false;
}
return !empty(\Imagick::queryFormats(\strtoupper($ext)));
}
public function openHandle(string $file_path, string $file_mime, int $file_kind): mixed {
$ext = Mime\mime_to_ext($file_mime);
$path = \realpath($file_path);
// Open it as the supplied mime type.
$imagick = new \Imagick("$ext:$path");
return [ $imagick, $file_path, $file_mime, $file_kind ];
}
public function closeHandle(mixed $handle) {
$handle[0]->clear();
}
public function generateThumb(
mixed $handle,
string $preferred_out_file_dir,
string $preferred_out_file_name,
string $preferred_out_mime,
int $max_width,
int $max_height
): ThumbGenerationResult {
list($imagick, $source_file_path, $source_file_mime, $source_file_kind) = $handle;
$width = $imagick->getImageWidth();
$height = $imagick->getImageHeight();
if (!$this->strip_metadata && $width <= $max_width && $height <= $max_height) {
$out_path = $preferred_out_file_dir . \DIRECTORY_SEPARATOR . $preferred_out_file_name . '.' . Mime\mime_to_ext($source_file_mime);
if ($source_file_kind === self::FILE_KIND_UPLOADED) {
if (!Fs\move_or_copy_uploaded($source_file_path, $out_path)) {
throw new \RuntimeException("Could not move or copy uploaded file '$source_file_path' to '$out_path'");
}
} else {
if (!Fs\link_or_copy($source_file_path, $out_path)) {
throw new \RuntimeException("Could not link or copy '$source_file_path' to '$out_path'");
}
}
return new ThumbGenerationResult(
$out_path,
$source_file_mime,
false,
$max_width,
$max_height
);
} else {
$swap = self::adjustOrientation($imagick);
if ($swap) {
$tmp = $width;
$width = $height;
$height = $tmp;
}
return self::generateThumbImpl(
$imagick,
$source_file_path,
$source_file_mime,
$preferred_out_file_dir,
$preferred_out_file_name,
$preferred_out_mime,
$width,
$height,
$max_width,
$max_height
);
}
}
public function installMediaAndGenerateThumb(