DefaultImageMetadataReader.php: add exif orientation support

This commit is contained in:
Zankaria 2025-03-21 01:40:43 +01:00
parent 8dbd92de76
commit 5e503af1e9

View file

@ -1,8 +1,8 @@
<?php
namespace Vichan\Service\Media;
use Vichan\Data\Driver\ExifReaderFactory;
use Vichan\Data\ImageMetadataResult;
use Vichan\Data\MagickMetadataReader;
/**
@ -11,11 +11,16 @@ use Vichan\Data\MagickMetadataReader;
* 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.
* The implementation only supports a limited subset of metadata for only the provided exif image formats readers.
* It won't always be able to correct the orientation, but it will try.
*/
class DefaultImageMetadataReader implements ImageMetadataReader {
private ExifReaderFactory $exif_reader_factory;
public function __construct(ExifReaderFactory $exif_reader_factory) {
$this->exif_reader_factory = $exif_reader_factory;
}
public function getMetadata(string $file_path): ImageMetadataResult {
$ret = \getimagesize($file_path, $info);
if ($ret === false) {
@ -24,6 +29,9 @@ class DefaultImageMetadataReader implements ImageMetadataReader {
if ($ret[2] == \IMAGETYPE_UNKNOWN) {
throw new \RuntimeException("Error '$file_path' is not an image");
}
return new ImageMetadataResult($ret[0], $ret[1], $ret['mime']);
$exif_reader = $this->exif_reader_factory->getReader($ret['mime']);
$orientation = $exif_reader === null ? null : $exif_reader->getOrientation($file_path);
return new ImageMetadataResult($ret[0], $ret[1], $ret['mime'], $orientation);
}
}