Skip to content

Commit

Permalink
Add ability to read instances of EncodedImaged::class (#1386)
Browse files Browse the repository at this point in the history
* Implement EncodedImageObjectDecoder::class
* Add tests for EncodedImageObjectDecoder::class
  • Loading branch information
olivervogel authored Aug 11, 2024
1 parent 4a614dd commit 31779d0
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Decoders/EncodedImageObjectDecoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Intervention\Image\Decoders;

use Intervention\Image\Drivers\SpecializableDecoder;

class EncodedImageObjectDecoder extends SpecializableDecoder
{
}
27 changes: 27 additions & 0 deletions src/Drivers/Gd/Decoders/EncodedImageObjectDecoder.php
Original file line number Diff line number Diff line change
@@ -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());
}
}
27 changes: 27 additions & 0 deletions src/Drivers/Imagick/Decoders/EncodedImageObjectDecoder.php
Original file line number Diff line number Diff line change
@@ -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());
}
}
2 changes: 2 additions & 0 deletions src/InputHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
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;
Expand Down Expand Up @@ -53,6 +54,7 @@ class InputHandler implements InputHandlerInterface
BinaryImageDecoder::class,
DataUriImageDecoder::class,
Base64ImageDecoder::class,
EncodedImageObjectDecoder::class,
];

/**
Expand Down
32 changes: 32 additions & 0 deletions tests/Unit/Drivers/Gd/Decoders/EncodedImageObjectDecoderTest.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 31779d0

Please sign in to comment.