Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}