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 <?php
namespace Vichan\Service\Media; namespace Vichan\Service\Media;
use Vichan\Data\Driver\ExifReaderFactory;
use Vichan\Data\ImageMetadataResult; 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 * 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. * 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. * The implementation only supports a limited subset of metadata for only the provided exif image formats readers.
* 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 * It won't always be able to correct the orientation, but it will try.
* are swapped, i.e. the contain the height and width, respectively.
*/ */
class DefaultImageMetadataReader implements ImageMetadataReader { 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 { public function getMetadata(string $file_path): ImageMetadataResult {
$ret = \getimagesize($file_path, $info); $ret = \getimagesize($file_path, $info);
if ($ret === false) { if ($ret === false) {
@ -24,6 +29,9 @@ class DefaultImageMetadataReader implements ImageMetadataReader {
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 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);
} }
} }