1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-19 12:11:26 +02:00

Implement image origin

This commit is contained in:
Oliver Vogel
2023-12-14 19:52:22 +01:00
parent c0a3ef85a0
commit cf1958c6e8
5 changed files with 89 additions and 24 deletions

View File

@@ -13,6 +13,7 @@ use Intervention\Image\Drivers\Gd\Core;
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Image;
use Intervention\Image\Origin;
class BinaryImageDecoder extends AbstractDecoder implements DecoderInterface
{
@@ -26,13 +27,41 @@ class BinaryImageDecoder extends AbstractDecoder implements DecoderInterface
return $this->decodeGif($input); // decode (animated) gif
}
return $this->decodeString($input);
}
private function decodeString(string $input): ImageInterface
{
$gd = @imagecreatefromstring($input);
if ($gd === false) {
throw new DecoderException('Unable to decode input');
}
if (!imageistruecolor($gd)) {
imagepalettetotruecolor($gd);
}
imagesavealpha($gd, true);
// build image instance
$image = new Image(
new Driver(),
$this->coreFromString($input),
new Core([
new Frame($gd)
]),
$this->extractExifData($input)
);
if ($info = getimagesizefromstring($input)) {
$image->setOrigin(
new Origin(
$info['mime'],
imagecolorstotal($gd)
),
);
}
// fix image orientation
return match ($image->exif('IFD0.Orientation')) {
2 => $image->flip(),
@@ -46,34 +75,12 @@ class BinaryImageDecoder extends AbstractDecoder implements DecoderInterface
};
}
private function coreFromString(string $input): Core
{
$data = @imagecreatefromstring($input);
if ($data === false) {
throw new DecoderException('Unable to decode input');
}
if (!imageistruecolor($data)) {
imagepalettetotruecolor($data);
}
imagesavealpha($data, true);
return new Core([
new Frame($data)
]);
}
private function decodeGif(string $input): ImageInterface
{
$gif = GifDecoder::decode($input);
if (!$gif->isAnimated()) {
return new Image(
new Driver(),
$this->coreFromString($input)
);
return $this->decodeString($input);
}
$splitter = GifSplitter::create($gif)->split();

View File

@@ -12,6 +12,7 @@ use Intervention\Image\Image;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Origin;
class BinaryImageDecoder extends AbstractDecoder implements DecoderInterface
{
@@ -54,6 +55,11 @@ class BinaryImageDecoder extends AbstractDecoder implements DecoderInterface
$this->extractExifData($input)
);
$image->setOrigin(new Origin(
$imagick->getImageMimeType(),
$imagick->getImageColors()
));
return $image;
}
}

View File

@@ -78,6 +78,8 @@ use Intervention\Image\Typography\FontFactory;
final class Image implements ImageInterface, Countable
{
protected Origin $origin;
public function __construct(
protected DriverInterface $driver,
protected CoreInterface $core,
@@ -105,6 +107,28 @@ final class Image implements ImageInterface, Countable
return $this->core;
}
/**
* {@inheritdoc}
*
* @see ImageInterface::origin()
*/
public function origin(): Origin
{
return $this->origin;
}
/**
* {@inheritdoc}
*
* @see ImageInterface::setOrigin()
*/
public function setOrigin(Origin $origin): ImageInterface
{
$this->origin = $origin;
return $this;
}
/**
* {@inheritdoc}
*

View File

@@ -4,6 +4,7 @@ namespace Intervention\Image\Interfaces;
use Countable;
use Intervention\Image\EncodedImage;
use Intervention\Image\Origin;
use IteratorAggregate;
interface ImageInterface extends IteratorAggregate, Countable
@@ -22,6 +23,21 @@ interface ImageInterface extends IteratorAggregate, Countable
*/
public function core(): CoreInterface;
/**
* Return the origin of the image
*
* @return Origin
*/
public function origin(): Origin;
/**
* Set the origin of the image
*
* @param Origin $origin
* @return ImageInterface
*/
public function setOrigin(Origin $origin): ImageInterface;
/**
* Return width of current image
*

12
src/Origin.php Normal file
View File

@@ -0,0 +1,12 @@
<?php
namespace Intervention\Image;
class Origin
{
public function __construct(
protected string $mimetype,
protected int $colors
) {
}
}