2025-03-17 22:21:38 +01:00
|
|
|
<?php
|
|
|
|
namespace Vichan\Service\Media;
|
|
|
|
|
2025-03-21 01:40:43 +01:00
|
|
|
use Vichan\Data\Driver\ExifReaderFactory;
|
2025-03-18 00:40:03 +01:00
|
|
|
use Vichan\Data\ImageMetadataResult;
|
|
|
|
|
2025-03-17 22:21:38 +01:00
|
|
|
|
2025-03-17 22:48:00 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2025-03-21 01:40:43 +01:00
|
|
|
* 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.
|
2025-03-17 22:48:00 +01:00
|
|
|
*/
|
2025-03-18 00:40:03 +01:00
|
|
|
class DefaultImageMetadataReader implements ImageMetadataReader {
|
2025-03-21 01:40:43 +01:00
|
|
|
private ExifReaderFactory $exif_reader_factory;
|
|
|
|
|
|
|
|
public function __construct(ExifReaderFactory $exif_reader_factory) {
|
|
|
|
$this->exif_reader_factory = $exif_reader_factory;
|
|
|
|
}
|
|
|
|
|
2025-03-18 00:40:03 +01:00
|
|
|
public function getMetadata(string $file_path): ImageMetadataResult {
|
2025-03-17 22:48:00 +01:00
|
|
|
$ret = \getimagesize($file_path, $info);
|
2025-03-17 22:21:38 +01:00
|
|
|
if ($ret === false) {
|
|
|
|
throw new \RuntimeException("Could not read image sizes of '$file_path'");
|
|
|
|
}
|
2025-03-17 22:48:00 +01:00
|
|
|
if ($ret[2] == \IMAGETYPE_UNKNOWN) {
|
|
|
|
throw new \RuntimeException("Error '$file_path' is not an image");
|
|
|
|
}
|
2025-03-21 01:40:43 +01:00
|
|
|
$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);
|
2025-03-17 22:21:38 +01:00
|
|
|
}
|
|
|
|
}
|