LibMagickMediaHandler.php: check image size, use MediaException and MediaHandlerTrait

This commit is contained in:
Zankaria 2025-03-25 23:53:07 +01:00
parent be43c419c1
commit 592cf2e41d

View file

@ -6,6 +6,8 @@ use Vichan\Functions\{Fs, Metadata};
class LibMagickMediaHandler implements MediaHandler { class LibMagickMediaHandler implements MediaHandler {
use MediaHandlerTrait;
// getImageAlphaChannel requires Imagick >= 2.3.0 and ImageMagick >= 6.4.0 // getImageAlphaChannel requires Imagick >= 2.3.0 and ImageMagick >= 6.4.0
private const MIN_IMAGICK_VERSION = '2.3.0'; private const MIN_IMAGICK_VERSION = '2.3.0';
private const MIN_IMAGEMAGICK_VERSION = '6.4.0'; private const MIN_IMAGEMAGICK_VERSION = '6.4.0';
@ -16,6 +18,8 @@ class LibMagickMediaHandler implements MediaHandler {
private bool $strip_metadata; private bool $strip_metadata;
private bool $frames_for_gif_thumbs; private bool $frames_for_gif_thumbs;
private int $image_max_width;
private int $image_max_height;
private static function degreesFromOrientation(int $orientation): int { private static function degreesFromOrientation(int $orientation): int {
@ -195,9 +199,11 @@ class LibMagickMediaHandler implements MediaHandler {
return false; return false;
} }
public function __construct(bool $strip_metadata, int $frames_for_gif_thumbs) { public function __construct(bool $strip_metadata, int $frames_for_gif_thumbs, int $max_width, int $max_height) {
$this->strip_metadata = $strip_metadata; $this->strip_metadata = $strip_metadata;
$this->frames_for_gif_thumbs = $frames_for_gif_thumbs; $this->frames_for_gif_thumbs = $frames_for_gif_thumbs;
$this->image_max_width = $max_width;
$this->image_max_height = $max_height;
} }
public function supportsMime(string $mime): bool { public function supportsMime(string $mime): bool {
@ -216,6 +222,14 @@ class LibMagickMediaHandler implements MediaHandler {
// Open it as the supplied mime type. // Open it as the supplied mime type.
$imagick = new \Imagick("$ext:$path"); $imagick = new \Imagick("$ext:$path");
$width = $imagick->getImageWidth();
$height = $imagick->getImageHeight();
if ($width > $this->image_max_width || $height > $this->image_max_height) {
$imagick->clear();
throw new MediaException("Image too big", MediaException::ERR_IMAGE_TOO_LARGE);
}
return [ $imagick, $file_path, $file_mime, $file_kind ]; return [ $imagick, $file_path, $file_mime, $file_kind ];
} }
@ -239,15 +253,7 @@ class LibMagickMediaHandler implements MediaHandler {
if (!$this->strip_metadata && $width <= $max_width && $height <= $max_height) { if (!$this->strip_metadata && $width <= $max_width && $height <= $max_height) {
$out_path = $preferred_out_file_dir . \DIRECTORY_SEPARATOR . $preferred_out_file_name . '.' . Metadata\mime_to_ext($source_file_mime); $out_path = $preferred_out_file_dir . \DIRECTORY_SEPARATOR . $preferred_out_file_name . '.' . Metadata\mime_to_ext($source_file_mime);
if ($source_file_kind === self::FILE_KIND_UPLOADED) { $this->move_or_link_or_copy($source_file_kind, $source_file_path, $out_path);
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( return new ThumbGenerationResult(
$out_path, $out_path,
@ -296,20 +302,12 @@ class LibMagickMediaHandler implements MediaHandler {
if (!$this->strip_metadata) { if (!$this->strip_metadata) {
$media_out_path = $media_preferred_out_file_dir . \DIRECTORY_SEPARATOR . $media_preferred_out_file_name . '.' . Metadata\mime_to_ext($media_file_mime); $media_out_path = $media_preferred_out_file_dir . \DIRECTORY_SEPARATOR . $media_preferred_out_file_name . '.' . Metadata\mime_to_ext($media_file_mime);
if ($media_file_kind === self::FILE_KIND_UPLOADED) { $this->move_or_link_or_copy($media_file_kind, $media_file_path, $media_out_path);
if (!Fs\move_or_copy_uploaded($media_file_path, $media_out_path)) {
throw new \RuntimeException("Could not move or copy uploaded file '$media_file_path' to '$media_out_path'");
}
} else {
if (!Fs\link_or_copy($media_file_path, $media_out_path)) {
throw new \RuntimeException("Could not link or copy '$media_file_path' to '$media_out_path'");
}
}
if ($width <= $thumb_max_width && $height >= $thumb_max_height) { if ($width <= $thumb_max_width && $height >= $thumb_max_height) {
$thumb_out_path = $thumb_preferred_out_file_dir . \DIRECTORY_SEPARATOR . '.' . Metadata\mime_to_ext($media_file_mime); $thumb_out_path = $thumb_preferred_out_file_dir . \DIRECTORY_SEPARATOR . '.' . Metadata\mime_to_ext($media_file_mime);
if (!Fs\link_or_copy($media_out_path, $thumb_out_path)) { if (!Fs\link_or_copy($media_out_path, $thumb_out_path)) {
throw new \RuntimeException("Could not link or copy '$media_out_path' to '$thumb_out_path'"); throw new MediaException("Could not link or copy '$media_out_path' to '$thumb_out_path'", MediaException::ERR_IO_ERR);
} }
return new ThumbGenerationResult($thumb_out_path, $media_file_mime, false, $width, $height); return new ThumbGenerationResult($thumb_out_path, $media_file_mime, false, $width, $height);