From 4fbc7f5840d7a7895fb38c9e77ea13582ed1a389 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Wed, 4 Oct 2023 17:09:13 +0200 Subject: [PATCH] Add Image::readExif() --- src/Drivers/Abstract/AbstractImage.php | 38 ++++++++++++++++++++++++ src/Exceptions/NotSupportedException.php | 8 +++++ 2 files changed, 46 insertions(+) create mode 100644 src/Exceptions/NotSupportedException.php diff --git a/src/Drivers/Abstract/AbstractImage.php b/src/Drivers/Abstract/AbstractImage.php index 3b70d93a..7565dcf6 100644 --- a/src/Drivers/Abstract/AbstractImage.php +++ b/src/Drivers/Abstract/AbstractImage.php @@ -2,8 +2,11 @@ namespace Intervention\Image\Drivers\Abstract; +use Exception; +use Intervention\Gif\Exception\NotReadableException; use Intervention\Image\Collection; use Intervention\Image\EncodedImage; +use Intervention\Image\Exceptions\NotSupportedException; use Intervention\Image\Geometry\Circle; use Intervention\Image\Geometry\Ellipse; use Intervention\Image\Geometry\Line; @@ -348,6 +351,41 @@ abstract class AbstractImage implements ImageInterface ); } + /** + * Read exif data from current image instance + * + * Returns value of given key or null if key was not found. If no + * parameter is given an array of all available data is returned. + * + * @param null|string $tag + * @return mixed + * @throws NotSupportedException + * @throws NotReadableException + */ + public function readExif(?string $tag = null): mixed + { + if (!function_exists('exif_read_data')) { + throw new NotSupportedException( + 'Reading Exif data is not supported by this PHP installation.' + ); + } + + try { + $pointer = $this->toJpeg()->toFilePointer(); + $data = @exif_read_data($pointer); + } catch (Exception $e) { + throw new NotReadableException('Unable to read Exif data from this image.'); + } + + fclose($pointer); + + if (!is_null($tag) && is_array($data)) { + $data = array_key_exists($tag, $data) ? $data[$tag] : null; + } + + return is_array($data) ? new Collection($data) : $data; + } + public function destroy(): void { $this->modify( diff --git a/src/Exceptions/NotSupportedException.php b/src/Exceptions/NotSupportedException.php new file mode 100644 index 00000000..e4c88468 --- /dev/null +++ b/src/Exceptions/NotSupportedException.php @@ -0,0 +1,8 @@ +