mirror of
https://github.com/Intervention/image.git
synced 2025-08-17 19:26:25 +02:00
Fix image orientation with GD driver
This commit is contained in:
@@ -32,6 +32,16 @@ abstract class AbstractDecoder implements DecoderInterface
|
||||
return $decoded;
|
||||
}
|
||||
|
||||
protected function hasSuccessor(): bool
|
||||
{
|
||||
return $this->successor !== null;
|
||||
}
|
||||
|
||||
protected function inputType($input): AbstractType
|
||||
{
|
||||
return MimeSniffer::createFromString($input)->getType();
|
||||
}
|
||||
|
||||
protected function decodeExifData(string $image_data): array
|
||||
{
|
||||
if (! function_exists('exif_read_data')) {
|
||||
@@ -42,22 +52,12 @@ abstract class AbstractDecoder implements DecoderInterface
|
||||
$pointer = fopen('php://temp', 'rw');
|
||||
fputs($pointer, $image_data);
|
||||
rewind($pointer);
|
||||
|
||||
$data = @exif_read_data($pointer, null, true);
|
||||
fclose($pointer);
|
||||
} catch (Exception $e) {
|
||||
$data = [];
|
||||
}
|
||||
|
||||
return is_array($data) ? $data : [];
|
||||
}
|
||||
|
||||
protected function hasSuccessor(): bool
|
||||
{
|
||||
return $this->successor !== null;
|
||||
}
|
||||
|
||||
protected function inputType($input): AbstractType
|
||||
{
|
||||
return MimeSniffer::createFromString($input)->getType();
|
||||
}
|
||||
}
|
||||
|
@@ -42,10 +42,21 @@ class BinaryImageDecoder extends AbstractDecoder implements DecoderInterface
|
||||
|
||||
imagesavealpha($gd, true);
|
||||
|
||||
// build image instance
|
||||
$image = new Image(new Collection([new Frame($gd)]));
|
||||
$image->setExif($this->decodeExifData($input));
|
||||
|
||||
return $image;
|
||||
// fix image orientation
|
||||
return match ($image->getExif('IFD0.Orientation')) {
|
||||
2 => $image->flip(),
|
||||
3 => $image->rotate(180),
|
||||
4 => $image->rotate(180)->flip(),
|
||||
5 => $image->rotate(270)->flip(),
|
||||
6 => $image->rotate(270),
|
||||
7 => $image->rotate(90)->flip(),
|
||||
8 => $image->rotate(90),
|
||||
default => $image
|
||||
};
|
||||
}
|
||||
|
||||
protected function decodeGif($input): ImageInterface
|
||||
|
@@ -3,6 +3,7 @@
|
||||
namespace Intervention\Image\Interfaces;
|
||||
|
||||
use Countable;
|
||||
use Intervention\Image\Collection;
|
||||
use Intervention\Image\EncodedImage;
|
||||
use Traversable;
|
||||
|
||||
@@ -55,6 +56,20 @@ interface ImageInterface extends Traversable, Countable
|
||||
*/
|
||||
public function getSize(): SizeInterface;
|
||||
|
||||
/**
|
||||
* Return exif data of current image
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getExif(?string $query = null): mixed;
|
||||
|
||||
/**
|
||||
* Set exif data on current image (will not be written in final image)
|
||||
*
|
||||
* @return ImageInterface
|
||||
*/
|
||||
public function setExif(array $data): ImageInterface;
|
||||
|
||||
/**
|
||||
* Determine if current image is animated
|
||||
*
|
||||
|
Reference in New Issue
Block a user