ImageFormatReader: rework into ImageMetadataReader

This commit is contained in:
Zankaria 2025-03-18 00:40:03 +01:00
parent 7f6e84e593
commit 29684043ab
5 changed files with 58 additions and 47 deletions

View file

@ -1,6 +1,9 @@
<?php <?php
namespace Vichan\Service\Media; namespace Vichan\Service\Media;
use Vichan\Data\ImageMetadataResult;
use Vichan\Data\MagickMetadataReader;
/** /**
* Do not use this if you can. * Do not use this if you can.
@ -12,8 +15,8 @@ namespace Vichan\Service\Media;
* 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 * 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. * are swapped, i.e. the contain the height and width, respectively.
*/ */
class DefaultImageFormatReader implements ImageFormatReader { class DefaultImageMetadataReader implements ImageMetadataReader {
public function getSizes(string $file_path): array { public function getMetadata(string $file_path): ImageMetadataResult {
$ret = \getimagesize($file_path, $info); $ret = \getimagesize($file_path, $info);
if ($ret === false) { if ($ret === false) {
throw new \RuntimeException("Could not read image sizes of '$file_path'"); throw new \RuntimeException("Could not read image sizes of '$file_path'");
@ -21,6 +24,6 @@ class DefaultImageFormatReader implements ImageFormatReader {
if ($ret[2] == \IMAGETYPE_UNKNOWN) { if ($ret[2] == \IMAGETYPE_UNKNOWN) {
throw new \RuntimeException("Error '$file_path' is not an image"); throw new \RuntimeException("Error '$file_path' is not an image");
} }
return $ret; return new ImageMetadataResult($ret[0], $ret[1], $ret['mime']);
} }
} }

View file

@ -1,11 +0,0 @@
<?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,12 @@
<?php
namespace Vichan\Service\Media;
use Vichan\Data\ImageMetadataResult;
interface ImageMetadataReader {
/**
* @param string $file_path Image file path.
*/
public function getMetadata(string $file_path): ImageMetadataResult;
}

View file

@ -1,33 +0,0 @@
<?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] ];
}
}

View file

@ -0,0 +1,40 @@
<?php
namespace Vichan\Service\Media;
use Mimey\MimeTypes;
use Vichan\Data\ImageMetadataResult;
class MagickImageMetadataReader implements ImageMetadataReader {
private string $prefix;
private MimeTypes $mime_types;
public static function createImageMagickReader(MimeTypes $mime_types): MagickImageMetadataReader {
return new self('', $mime_types);
}
public static function createGraphicsMagickReader(MimeTypes $mime_types): MagickImageMetadataReader {
return new self('gm ', $mime_types);
}
private function __construct(string $prefix, MimeTypes $mime_types) {
$this->prefix = $prefix;
$this->mime_types = $mime_types;
}
public function getMetadata(string $file_path): ImageMetadataResult {
$arg = escapeshellarg("$file_path[0]");
$ret_exec = shell_exec_error("{$this->prefix}identify -format \"%w %h %m\" $arg");
if (!\is_string($ret_exec)) {
throw new \RuntimeException("Error while executing identify");
}
$ret_match = \preg_match('/^(\d+) (\d+) ([\w\d]+)$/', $ret_exec, $m);
if (!$ret_match) {
throw new \RuntimeException("Could not parse identify output");
}
$mime = $this->mime_types->getMimeType($m[3]) ?? 'application/octet-stream';
return new ImageMetadataResult($m[1], $m[2], $mime);
}
}