1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-31 01:29:51 +02:00

Add ability to read instances of EncodedImaged::class (#1386)

* Implement EncodedImageObjectDecoder::class
* Add tests for EncodedImageObjectDecoder::class
This commit is contained in:
Oliver Vogel
2024-08-11 11:43:54 +02:00
committed by GitHub
parent 4a614ddfb8
commit 31779d0263
6 changed files with 131 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Decoders;
use Intervention\Image\Drivers\SpecializableDecoder;
class EncodedImageObjectDecoder extends SpecializableDecoder
{
}

View File

@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Drivers\Gd\Decoders;
use Intervention\Image\EncodedImage;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ColorInterface;
class EncodedImageObjectDecoder extends BinaryImageDecoder
{
/**
* {@inheritdoc}
*
* @see DecoderInterface::decode()
*/
public function decode(mixed $input): ImageInterface|ColorInterface
{
if (!is_a($input, EncodedImage::class)) {
throw new DecoderException('Unable to decode input');
}
return parent::decode($input->toString());
}
}

View File

@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Drivers\Imagick\Decoders;
use Intervention\Image\EncodedImage;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ColorInterface;
class EncodedImageObjectDecoder extends BinaryImageDecoder
{
/**
* {@inheritdoc}
*
* @see DecoderInterface::decode()
*/
public function decode(mixed $input): ImageInterface|ColorInterface
{
if (!is_a($input, EncodedImage::class)) {
throw new DecoderException('Unable to decode input');
}
return parent::decode($input->toString());
}
}

View File

@@ -15,6 +15,7 @@ use Intervention\Image\Decoders\Base64ImageDecoder;
use Intervention\Image\Decoders\BinaryImageDecoder;
use Intervention\Image\Decoders\ColorObjectDecoder;
use Intervention\Image\Decoders\DataUriImageDecoder;
use Intervention\Image\Decoders\EncodedImageObjectDecoder;
use Intervention\Image\Decoders\FilePathImageDecoder;
use Intervention\Image\Decoders\FilePointerImageDecoder;
use Intervention\Image\Decoders\ImageObjectDecoder;
@@ -53,6 +54,7 @@ class InputHandler implements InputHandlerInterface
BinaryImageDecoder::class,
DataUriImageDecoder::class,
Base64ImageDecoder::class,
EncodedImageObjectDecoder::class,
];
/**

View File

@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Tests\Unit\Drivers\Gd\Decoders;
use Intervention\Image\Drivers\Gd\Decoders\EncodedImageObjectDecoder;
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\EncodedImage;
use Intervention\Image\Image;
use Intervention\Image\Tests\ImagickTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
#[RequiresPhpExtension('gd')]
#[CoversClass(EncodedImageObjectDecoder::class)]
class EncodedImageObjectDecoderTest extends ImagickTestCase
{
protected EncodedImageObjectDecoder $decoder;
protected function setUp(): void
{
$this->decoder = new EncodedImageObjectDecoder();
$this->decoder->setDriver(new Driver());
}
public function testDecode(): void
{
$result = $this->decoder->decode(new EncodedImage($this->getTestResourceData()));
$this->assertInstanceOf(Image::class, $result);
}
}

View File

@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Intervention\Image\Tests\Unit\Drivers\Imagick\Decoders;
use Intervention\Image\Drivers\Imagick\Decoders\EncodedImageObjectDecoder;
use Intervention\Image\Drivers\Imagick\Driver;
use Intervention\Image\EncodedImage;
use Intervention\Image\Image;
use Intervention\Image\Tests\ImagickTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
#[RequiresPhpExtension('imagick')]
#[CoversClass(EncodedImageObjectDecoder::class)]
class EncodedImageObjectDecoderTest extends ImagickTestCase
{
protected EncodedImageObjectDecoder $decoder;
protected function setUp(): void
{
$this->decoder = new EncodedImageObjectDecoder();
$this->decoder->setDriver(new Driver());
}
public function testDecode(): void
{
$result = $this->decoder->decode(new EncodedImage($this->getTestResourceData()));
$this->assertInstanceOf(Image::class, $result);
}
}