1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-18 11:41:17 +02:00

Add EXIF data decoding

This commit is contained in:
Oliver Vogel
2023-10-05 17:20:15 +02:00
parent 3a6c6814df
commit a62859fbf4
4 changed files with 37 additions and 32 deletions

View File

@@ -2,7 +2,6 @@
namespace Intervention\Image\Drivers\Abstract;
use Exception;
use Intervention\Gif\Exception\NotReadableException;
use Intervention\Image\Collection;
use Intervention\Image\EncodedImage;
@@ -21,6 +20,7 @@ use Intervention\Image\Interfaces\SizeInterface;
use Intervention\Image\Traits\CanHandleInput;
use Intervention\Image\Traits\CanResolveDriverClass;
use Intervention\Image\Traits\CanRunCallback;
use ReflectionProperty;
abstract class AbstractImage implements ImageInterface
{
@@ -28,6 +28,10 @@ abstract class AbstractImage implements ImageInterface
use CanHandleInput;
use CanRunCallback;
protected Collection $exif;
public function eachFrame(callable $callback): ImageInterface
{
foreach ($this as $frame) {
@@ -351,39 +355,16 @@ 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
public function setExif(array $data): ImageInterface
{
if (!function_exists('exif_read_data')) {
throw new NotSupportedException(
'Reading Exif data is not supported by this PHP installation.'
);
$this->exif = new Collection($data);
return $this;
}
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 getExif(?string $query = null): mixed
{
return is_null($query) ? $this->exif : $this->exif->get($query);
}
public function destroy(): void

View File

@@ -2,6 +2,7 @@
namespace Intervention\Image\Drivers\Abstract\Decoders;
use Exception;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
@@ -31,6 +32,25 @@ abstract class AbstractDecoder implements DecoderInterface
return $decoded;
}
protected function decodeExifData(string $image_data): array
{
if (! function_exists('exif_read_data')) {
return [];
}
try {
$pointer = fopen('php://temp', 'rw');
fputs($pointer, $image_data);
rewind($pointer);
$data = @exif_read_data($pointer, null, true);
} catch (Exception $e) {
$data = [];
}
return is_array($data) ? $data : [];
}
protected function hasSuccessor(): bool
{
return $this->successor !== null;

View File

@@ -42,7 +42,10 @@ class BinaryImageDecoder extends AbstractDecoder implements DecoderInterface
imagesavealpha($gd, true);
return new Image(new Collection([new Frame($gd)]));
$image = new Image(new Collection([new Frame($gd)]));
$image->setExif($this->decodeExifData($input));
return $image;
}
protected function decodeGif($input): ImageInterface

View File

@@ -28,6 +28,7 @@ class BinaryImageDecoder extends AbstractDecoder implements DecoderInterface
$image = new Image($imagick);
$image->setLoops($imagick->getImageIterations());
$image->setExif($this->decodeExifData($input));
return $image;
}