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

@ -0,0 +1,29 @@
<?php
namespace Vichan\Service\Media;
use Vichan\Data\ImageMetadataResult;
use Vichan\Data\MagickMetadataReader;
/**
* Do not use this if you can.
*
* Some formats may contain no image or may contain multiple images. In these cases, getimagesize() might not be
* able to properly determine the image size. getimagesize() will return zero for width and height in these cases.
*
* 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.
*/
class DefaultImageMetadataReader implements ImageMetadataReader {
public function getMetadata(string $file_path): ImageMetadataResult {
$ret = \getimagesize($file_path, $info);
if ($ret === false) {
throw new \RuntimeException("Could not read image sizes of '$file_path'");
}
if ($ret[2] == \IMAGETYPE_UNKNOWN) {
throw new \RuntimeException("Error '$file_path' is not an image");
}
return new ImageMetadataResult($ret[0], $ret[1], $ret['mime']);
}
}