mirror of
https://github.com/Intervention/image.git
synced 2025-08-20 04:31:24 +02:00
Add EXIF data decoding
This commit is contained in:
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace Intervention\Image\Drivers\Abstract;
|
namespace Intervention\Image\Drivers\Abstract;
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Intervention\Gif\Exception\NotReadableException;
|
use Intervention\Gif\Exception\NotReadableException;
|
||||||
use Intervention\Image\Collection;
|
use Intervention\Image\Collection;
|
||||||
use Intervention\Image\EncodedImage;
|
use Intervention\Image\EncodedImage;
|
||||||
@@ -21,6 +20,7 @@ use Intervention\Image\Interfaces\SizeInterface;
|
|||||||
use Intervention\Image\Traits\CanHandleInput;
|
use Intervention\Image\Traits\CanHandleInput;
|
||||||
use Intervention\Image\Traits\CanResolveDriverClass;
|
use Intervention\Image\Traits\CanResolveDriverClass;
|
||||||
use Intervention\Image\Traits\CanRunCallback;
|
use Intervention\Image\Traits\CanRunCallback;
|
||||||
|
use ReflectionProperty;
|
||||||
|
|
||||||
abstract class AbstractImage implements ImageInterface
|
abstract class AbstractImage implements ImageInterface
|
||||||
{
|
{
|
||||||
@@ -28,6 +28,10 @@ abstract class AbstractImage implements ImageInterface
|
|||||||
use CanHandleInput;
|
use CanHandleInput;
|
||||||
use CanRunCallback;
|
use CanRunCallback;
|
||||||
|
|
||||||
|
protected Collection $exif;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public function eachFrame(callable $callback): ImageInterface
|
public function eachFrame(callable $callback): ImageInterface
|
||||||
{
|
{
|
||||||
foreach ($this as $frame) {
|
foreach ($this as $frame) {
|
||||||
@@ -351,39 +355,16 @@ abstract class AbstractImage implements ImageInterface
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function setExif(array $data): 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')) {
|
$this->exif = new Collection($data);
|
||||||
throw new NotSupportedException(
|
|
||||||
'Reading Exif data is not supported by this PHP installation.'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
return $this;
|
||||||
$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);
|
public function getExif(?string $query = null): mixed
|
||||||
|
{
|
||||||
if (!is_null($tag) && is_array($data)) {
|
return is_null($query) ? $this->exif : $this->exif->get($query);
|
||||||
$data = array_key_exists($tag, $data) ? $data[$tag] : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return is_array($data) ? new Collection($data) : $data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function destroy(): void
|
public function destroy(): void
|
||||||
|
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace Intervention\Image\Drivers\Abstract\Decoders;
|
namespace Intervention\Image\Drivers\Abstract\Decoders;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use Intervention\Image\Exceptions\DecoderException;
|
use Intervention\Image\Exceptions\DecoderException;
|
||||||
use Intervention\Image\Interfaces\ColorInterface;
|
use Intervention\Image\Interfaces\ColorInterface;
|
||||||
use Intervention\Image\Interfaces\DecoderInterface;
|
use Intervention\Image\Interfaces\DecoderInterface;
|
||||||
@@ -31,6 +32,25 @@ abstract class AbstractDecoder implements DecoderInterface
|
|||||||
return $decoded;
|
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
|
protected function hasSuccessor(): bool
|
||||||
{
|
{
|
||||||
return $this->successor !== null;
|
return $this->successor !== null;
|
||||||
|
@@ -42,7 +42,10 @@ class BinaryImageDecoder extends AbstractDecoder implements DecoderInterface
|
|||||||
|
|
||||||
imagesavealpha($gd, true);
|
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
|
protected function decodeGif($input): ImageInterface
|
||||||
|
@@ -28,6 +28,7 @@ class BinaryImageDecoder extends AbstractDecoder implements DecoderInterface
|
|||||||
|
|
||||||
$image = new Image($imagick);
|
$image = new Image($imagick);
|
||||||
$image->setLoops($imagick->getImageIterations());
|
$image->setLoops($imagick->getImageIterations());
|
||||||
|
$image->setExif($this->decodeExifData($input));
|
||||||
|
|
||||||
return $image;
|
return $image;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user