ImageFormatReader: add iamge format readers

This commit is contained in:
Zankaria 2025-03-17 22:21:38 +01:00
parent 557e43e38f
commit 1485ef3f2d
3 changed files with 62 additions and 0 deletions

View file

@ -0,0 +1,18 @@
<?php
namespace Vichan\Service\Media;
class GdImageFormatReader implements ImageFormatReader {
/**
* getimagesize() is agnostic of any image metadata.
* If e.g. the Exif Orientation flag is set to a value which rotates the image by 90 or 270 degress, index 0 and 1
* are swapped, i.e. the contain the height and width, respectively.
*/
public function getSizes(string $file_path): array {
$ret = \getimagesize($file_path);
if ($ret === false) {
throw new \RuntimeException("Could not read image sizes of '$file_path'");
}
return $ret;
}
}

View file

@ -0,0 +1,11 @@
<?php
namespace Vichan\Service\Media;
interface ImageFormatReader {
/**
* @param string $file_path Image file path.
* @return array An array with width and height.
*/
public function getSizes(string $file_path): array;
}

View file

@ -0,0 +1,33 @@
<?php
namespace Vichan\Service\Media;
class MagickImageFormatReader implements ImageFormatReader {
private string $prefix;
public static function createImageMagickReader(): MagickImageFormatReader {
return new self('');
}
public static function createGraphicsMagickReader(): MagickImageFormatReader {
return new self('gm ');
}
private function __construct(string $prefix) {
$this->prefix = $prefix;
}
public function getSizes(string $file_path): array {
$arg = escapeshellarg("$file_path[0]");
$ret_exec = shell_exec_error("{$this->prefix}identify -format \"%w %h\" $arg");
if (!\is_string($ret_exec)) {
throw new \RuntimeException("Error while executing identify");
}
$ret_match = \preg_match('/^(\d+) (\d+)$/', $ret_exec, $m);
if (!$ret_match) {
throw new \RuntimeException("Could not parse identify output");
}
return [ $m[1], $m[2] ];
}
}