1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-29 16:50:07 +02:00

Add ability to read Imagick or GDImage directly (#1329)

This commit is contained in:
Oliver Vogel
2024-04-10 15:17:36 +02:00
committed by GitHub
parent 2439fd80cb
commit 6e104b6594
11 changed files with 44 additions and 6 deletions

View File

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

View File

@@ -12,7 +12,7 @@ use Intervention\Image\Drivers\Gd\Decoders\Traits\CanDecodeGif;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Modifiers\AlignRotationModifier;
class BinaryImageDecoder extends GdImageDecoder implements DecoderInterface
class BinaryImageDecoder extends NativeObjectDecoder implements DecoderInterface
{
use CanDecodeGif;

View File

@@ -11,7 +11,7 @@ use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Modifiers\AlignRotationModifier;
class FilePathImageDecoder extends GdImageDecoder implements DecoderInterface
class FilePathImageDecoder extends NativeObjectDecoder implements DecoderInterface
{
use CanDecodeGif;

View File

@@ -13,8 +13,13 @@ use Intervention\Image\Image;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ColorInterface;
class GdImageDecoder extends AbstractDecoder
class NativeObjectDecoder extends AbstractDecoder
{
/**
* {@inheritdoc}
*
* @see DecoderInterface::decode()
*/
public function decode(mixed $input): ImageInterface|ColorInterface
{
if (!is_object($input)) {

View File

@@ -19,11 +19,13 @@ use Intervention\Image\Drivers\Gd\Decoders\FilePathImageDecoder;
use Intervention\Image\Drivers\Gd\Decoders\BinaryImageDecoder;
use Intervention\Image\Drivers\Gd\Decoders\DataUriImageDecoder;
use Intervention\Image\Drivers\Gd\Decoders\Base64ImageDecoder;
use Intervention\Image\Drivers\Gd\Decoders\NativeObjectDecoder;
use Intervention\Image\Drivers\Gd\Decoders\SplFileInfoImageDecoder;
class InputHandler extends AbstractInputHandler
{
protected array $decoders = [
NativeObjectDecoder::class,
ImageObjectDecoder::class,
ColorObjectDecoder::class,
RgbHexColorDecoder::class,

View File

@@ -10,7 +10,7 @@ use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ImageInterface;
class BinaryImageDecoder extends ImagickImageDecoder
class BinaryImageDecoder extends NativeObjectDecoder
{
public function decode(mixed $input): ImageInterface|ColorInterface
{

View File

@@ -10,7 +10,7 @@ use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ImageInterface;
class FilePathImageDecoder extends ImagickImageDecoder
class FilePathImageDecoder extends NativeObjectDecoder
{
public function decode(mixed $input): ImageInterface|ColorInterface
{

View File

@@ -14,7 +14,7 @@ use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Modifiers\AlignRotationModifier;
class ImagickImageDecoder extends SpecializableDecoder
class NativeObjectDecoder extends SpecializableDecoder
{
public function decode(mixed $input): ImageInterface|ColorInterface
{

View File

@@ -19,11 +19,13 @@ use Intervention\Image\Drivers\Imagick\Decoders\FilePathImageDecoder;
use Intervention\Image\Drivers\Imagick\Decoders\BinaryImageDecoder;
use Intervention\Image\Drivers\Imagick\Decoders\DataUriImageDecoder;
use Intervention\Image\Drivers\Imagick\Decoders\Base64ImageDecoder;
use Intervention\Image\Drivers\Imagick\Decoders\NativeObjectDecoder;
use Intervention\Image\Drivers\Imagick\Decoders\SplFileInfoImageDecoder;
class InputHandler extends AbstractInputHandler
{
protected array $decoders = [
NativeObjectDecoder::class,
ImageObjectDecoder::class,
ColorObjectDecoder::class,
RgbHexColorDecoder::class,

View File

@@ -34,6 +34,13 @@ final class InputHandlerTest extends BaseTestCase
$this->assertInstanceOf(Image::class, $result);
}
public function testHandleGdImage(): void
{
$handler = new InputHandler();
$result = $handler->handle(imagecreatetruecolor(3, 2));
$this->assertInstanceOf(Image::class, $result);
}
public function testHandleSplFileInfo(): void
{
$handler = new InputHandler();

View File

@@ -4,6 +4,8 @@ declare(strict_types=1);
namespace Intervention\Image\Tests\Unit\Drivers\Imagick;
use Imagick;
use ImagickPixel;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Intervention\Image\Colors\Cmyk\Color as CmykColor;
@@ -34,6 +36,15 @@ final class InputHandlerTest extends BaseTestCase
$this->assertInstanceOf(Image::class, $result);
}
public function testHandleImagick(): void
{
$imagick = new Imagick();
$imagick->newImage(3, 2, new ImagickPixel('rgba(255, 255, 255, 255)'), 'png');
$handler = new InputHandler();
$result = $handler->handle($imagick);
$this->assertInstanceOf(Image::class, $result);
}
public function testHandleSplFileInfo(): void
{
$handler = new InputHandler();