From ee45783a1c8d7c51bfb72ea62a0e115ed7c5bacc Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sat, 17 Oct 2026 09:14:55 +0200 Subject: [PATCH 1/2] Add support for decoding psr7 stream objects --- composer.json | 3 +- src/Decoders/Psr7StreamImageDecoder.php | 12 ++ .../Gd/Decoders/Psr7StreamImageDecoder.php | 63 ++++++++++ .../Decoders/Psr7StreamImageDecoder.php | 63 ++++++++++ src/ImageManager.php | 3 +- .../Decoders/Psr7StreamImageDecoderTest.php | 110 ++++++++++++++++++ .../Decoders/Psr7StreamImageDecoderTest.php | 110 ++++++++++++++++++ 7 files changed, 362 insertions(+), 2 deletions(-) create mode 100644 src/Decoders/Psr7StreamImageDecoder.php create mode 100644 src/Drivers/Gd/Decoders/Psr7StreamImageDecoder.php create mode 100644 src/Drivers/Imagick/Decoders/Psr7StreamImageDecoder.php create mode 100644 tests/Unit/Drivers/Gd/Decoders/Psr7StreamImageDecoderTest.php create mode 100644 tests/Unit/Drivers/Imagick/Decoders/Psr7StreamImageDecoderTest.php diff --git a/composer.json b/composer.json index 4afdb3117..18ada0d39 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,8 @@ "phpunit/phpunit": "^12.0", "mockery/mockery": "^1.6", "phpstan/phpstan": "^2.1", - "intervention/coding-standard": "^1" + "intervention/coding-standard": "^1", + "psr/http-message": "^2" }, "suggest": { "ext-exif": "Recommended to be able to read EXIF data properly." diff --git a/src/Decoders/Psr7StreamImageDecoder.php b/src/Decoders/Psr7StreamImageDecoder.php new file mode 100644 index 000000000..4cb6969c2 --- /dev/null +++ b/src/Decoders/Psr7StreamImageDecoder.php @@ -0,0 +1,12 @@ +isPsr7Stream($input); + } + + /** + * {@inheritdoc} + * + * @see DecoderInterface::decode() + * + * @throws InvalidArgumentException + * @throws StreamException + * @throws DriverException + * @throws StateException + * @throws ImageDecoderException + * @throws NotSupportedException + */ + public function decode(mixed $input): ImageInterface + { + if (!$this->isPsr7Stream($input)) { + throw new InvalidArgumentException( + "Image source must be of type '" . self::DECODABLE_CLASSNAME . "'", + ); + } + + try { + return parent::decode((string) $input); + } catch (DecoderException) { + throw new ImageDecoderException( + "Failed to decode image from 'GuzzleHttp\Psr7\Response', could be unsupported image format", + ); + } + } + + private function isPsr7Stream(mixed $input): bool + { + return is_object($input) && is_a($input, self::DECODABLE_CLASSNAME); + } +} diff --git a/src/Drivers/Imagick/Decoders/Psr7StreamImageDecoder.php b/src/Drivers/Imagick/Decoders/Psr7StreamImageDecoder.php new file mode 100644 index 000000000..74e49ec35 --- /dev/null +++ b/src/Drivers/Imagick/Decoders/Psr7StreamImageDecoder.php @@ -0,0 +1,63 @@ +isPsr7Stream($input); + } + + /** + * {@inheritdoc} + * + * @see DecoderInterface::decode() + * + * @throws InvalidArgumentException + * @throws StreamException + * @throws DriverException + * @throws StateException + * @throws ImageDecoderException + * @throws NotSupportedException + */ + public function decode(mixed $input): ImageInterface + { + if (!$this->isPsr7Stream($input)) { + throw new InvalidArgumentException( + "Image source must be of type '" . self::DECODABLE_CLASSNAME . "'", + ); + } + + try { + return parent::decode((string) $input); + } catch (DecoderException) { + throw new ImageDecoderException( + "Failed to decode image from 'GuzzleHttp\Psr7\Response', could be unsupported image format", + ); + } + } + + private function isPsr7Stream(mixed $input): bool + { + return is_object($input) && is_a($input, self::DECODABLE_CLASSNAME); + } +} diff --git a/src/ImageManager.php b/src/ImageManager.php index 44c330c71..c41da96c4 100644 --- a/src/ImageManager.php +++ b/src/ImageManager.php @@ -8,6 +8,7 @@ use Intervention\Image\Decoders\BinaryImageDecoder; use Intervention\Image\Decoders\DataUriImageDecoder; use Intervention\Image\Decoders\FilePathImageDecoder; +use Intervention\Image\Decoders\Psr7StreamImageDecoder; use Intervention\Image\Decoders\StreamImageDecoder; use Intervention\Image\Decoders\SplFileInfoImageDecoder; use Intervention\Image\Exceptions\DriverException; @@ -175,6 +176,6 @@ public function decodeDataUri(string|Stringable|DataUriInterface $dataUri): Imag */ public function decodeStream(mixed $stream): ImageInterface { - return $this->decode($stream, StreamImageDecoder::class); + return $this->decode($stream, [StreamImageDecoder::class, Psr7StreamImageDecoder::class]); } } diff --git a/tests/Unit/Drivers/Gd/Decoders/Psr7StreamImageDecoderTest.php b/tests/Unit/Drivers/Gd/Decoders/Psr7StreamImageDecoderTest.php new file mode 100644 index 000000000..cdcd8ad49 --- /dev/null +++ b/tests/Unit/Drivers/Gd/Decoders/Psr7StreamImageDecoderTest.php @@ -0,0 +1,110 @@ +setDriver(new Driver()); + + $result = $decoder->decode($this->psr7Stream()); + $this->assertInstanceOf(Image::class, $result); + } + + private function psr7Stream(): object + { + return new class () implements StreamInterface + { + public function getContents(): string + { + return Resource::create('test.jpg')->data(); + } + + public function close(): void + { + throw new Exception('Not implemented'); + } + + public function detach(): null + { + throw new Exception('Not implemented'); + } + + public function getSize(): ?int + { + throw new Exception('Not implemented'); + } + + public function tell(): int + { + throw new Exception('Not implemented'); + } + + public function eof(): bool + { + throw new Exception('Not implemented'); + } + + public function isSeekable(): bool + { + throw new Exception('Not implemented'); + } + + public function seek(int $offset, int $whence = SEEK_SET): void + { + throw new Exception('Not implemented'); + } + + public function rewind(): void + { + throw new Exception('Not implemented'); + } + + public function isWritable(): bool + { + throw new Exception('Not implemented'); + } + + public function write(string $string): int + { + throw new Exception('Not implemented'); + } + + public function isReadable(): bool + { + throw new Exception('Not implemented'); + } + + public function read(int $length): string + { + throw new Exception('Not implemented'); + } + + public function getMetadata(?string $key = null): null + { + return null; + } + + public function __toString(): string + { + return $this->getContents(); + } + }; + } +} diff --git a/tests/Unit/Drivers/Imagick/Decoders/Psr7StreamImageDecoderTest.php b/tests/Unit/Drivers/Imagick/Decoders/Psr7StreamImageDecoderTest.php new file mode 100644 index 000000000..fe80c0f04 --- /dev/null +++ b/tests/Unit/Drivers/Imagick/Decoders/Psr7StreamImageDecoderTest.php @@ -0,0 +1,110 @@ +setDriver(new Driver()); + + $result = $decoder->decode($this->psr7Stream()); + $this->assertInstanceOf(Image::class, $result); + } + + private function psr7Stream(): object + { + return new class () implements StreamInterface + { + public function getContents(): string + { + return Resource::create('test.jpg')->data(); + } + + public function close(): void + { + throw new Exception('Not implemented'); + } + + public function detach(): null + { + throw new Exception('Not implemented'); + } + + public function getSize(): ?int + { + throw new Exception('Not implemented'); + } + + public function tell(): int + { + throw new Exception('Not implemented'); + } + + public function eof(): bool + { + throw new Exception('Not implemented'); + } + + public function isSeekable(): bool + { + throw new Exception('Not implemented'); + } + + public function seek(int $offset, int $whence = SEEK_SET): void + { + throw new Exception('Not implemented'); + } + + public function rewind(): void + { + throw new Exception('Not implemented'); + } + + public function isWritable(): bool + { + throw new Exception('Not implemented'); + } + + public function write(string $string): int + { + throw new Exception('Not implemented'); + } + + public function isReadable(): bool + { + throw new Exception('Not implemented'); + } + + public function read(int $length): string + { + throw new Exception('Not implemented'); + } + + public function getMetadata(?string $key = null): null + { + return null; + } + + public function __toString(): string + { + return $this->getContents(); + } + }; + } +} From 6855a7e5b3b3ba5983219541b042597bcaff54f2 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sat, 17 Oct 2026 09:26:37 +0200 Subject: [PATCH 2/2] Add support for decoding psr7 http responses --- src/Decoders/HttpResponseImageDecoder.php | 12 ++ .../Gd/Decoders/HttpResponseImageDecoder.php | 66 ++++++++++ .../Decoders/HttpResponseImageDecoder.php | 66 ++++++++++ src/ImageManager.php | 34 ++--- src/InputHandler.php | 4 + .../Decoders/HttpResponseImageDecoderTest.php | 116 ++++++++++++++++++ .../Decoders/Psr7StreamImageDecoderTest.php | 4 +- .../Decoders/HttpResponseImageDecoderTest.php | 116 ++++++++++++++++++ .../Decoders/Psr7StreamImageDecoderTest.php | 4 +- 9 files changed, 403 insertions(+), 19 deletions(-) create mode 100644 src/Decoders/HttpResponseImageDecoder.php create mode 100644 src/Drivers/Gd/Decoders/HttpResponseImageDecoder.php create mode 100644 src/Drivers/Imagick/Decoders/HttpResponseImageDecoder.php create mode 100644 tests/Unit/Drivers/Gd/Decoders/HttpResponseImageDecoderTest.php create mode 100644 tests/Unit/Drivers/Imagick/Decoders/HttpResponseImageDecoderTest.php diff --git a/src/Decoders/HttpResponseImageDecoder.php b/src/Decoders/HttpResponseImageDecoder.php new file mode 100644 index 000000000..a6aedd3f9 --- /dev/null +++ b/src/Decoders/HttpResponseImageDecoder.php @@ -0,0 +1,12 @@ +isPsr7Response($input); + } + + /** + * {@inheritdoc} + * + * @see DecoderInterface::decode() + * + * @throws InvalidArgumentException + * @throws StreamException + * @throws DriverException + * @throws StateException + * @throws ImageDecoderException + * @throws NotSupportedException + */ + public function decode(mixed $input): ImageInterface + { + if (!$this->isPsr7Response($input)) { + throw new InvalidArgumentException( + "Image source must be of type '" . self::DECODABLE_CLASSNAME . "'", + ); + } + + try { + return parent::decode($input->getBody()); + } catch (DecoderException) { + throw new ImageDecoderException( + "Failed to decode image from " . self::DECODABLE_CLASSNAME . ", could be unsupported image format", + ); + } + } + + /** + * Determine if input can be handled as psr7 response. + */ + private function isPsr7Response(mixed $input): bool + { + return is_object($input) && is_a($input, self::DECODABLE_CLASSNAME); + } +} diff --git a/src/Drivers/Imagick/Decoders/HttpResponseImageDecoder.php b/src/Drivers/Imagick/Decoders/HttpResponseImageDecoder.php new file mode 100644 index 000000000..a22d8c10b --- /dev/null +++ b/src/Drivers/Imagick/Decoders/HttpResponseImageDecoder.php @@ -0,0 +1,66 @@ +isPsr7Response($input); + } + + /** + * {@inheritdoc} + * + * @see DecoderInterface::decode() + * + * @throws InvalidArgumentException + * @throws StreamException + * @throws DriverException + * @throws StateException + * @throws ImageDecoderException + * @throws NotSupportedException + */ + public function decode(mixed $input): ImageInterface + { + if (!$this->isPsr7Response($input)) { + throw new InvalidArgumentException( + "Image source must be of type '" . self::DECODABLE_CLASSNAME . "'", + ); + } + + try { + return parent::decode($input->getBody()); + } catch (DecoderException) { + throw new ImageDecoderException( + "Failed to decode image from " . self::DECODABLE_CLASSNAME . ", could be unsupported image format", + ); + } + } + + /** + * Determine if input can be handled as psr7 response. + */ + private function isPsr7Response(mixed $input): bool + { + return is_object($input) && is_a($input, self::DECODABLE_CLASSNAME); + } +} diff --git a/src/ImageManager.php b/src/ImageManager.php index c41da96c4..d1018ee76 100644 --- a/src/ImageManager.php +++ b/src/ImageManager.php @@ -4,13 +4,6 @@ namespace Intervention\Image; -use Intervention\Image\Decoders\Base64ImageDecoder; -use Intervention\Image\Decoders\BinaryImageDecoder; -use Intervention\Image\Decoders\DataUriImageDecoder; -use Intervention\Image\Decoders\FilePathImageDecoder; -use Intervention\Image\Decoders\Psr7StreamImageDecoder; -use Intervention\Image\Decoders\StreamImageDecoder; -use Intervention\Image\Decoders\SplFileInfoImageDecoder; use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\ImageDecoderException; use Intervention\Image\Exceptions\InvalidArgumentException; @@ -20,13 +13,12 @@ use Intervention\Image\Interfaces\DriverInterface; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ImageManagerInterface; -use Intervention\Image\Traits\CanResolveDriver; use SplFileInfo; use Stringable; class ImageManager implements ImageManagerInterface { - use CanResolveDriver; + use Traits\CanResolveDriver; public DriverInterface $driver; @@ -106,7 +98,7 @@ public function decode(mixed $source, null|string|array|DecoderInterface $decode */ public function decodePath(string|Stringable $path): ImageInterface { - return $this->decode($path, FilePathImageDecoder::class); + return $this->decode($path, Decoders\FilePathImageDecoder::class); } /** @@ -120,7 +112,7 @@ public function decodePath(string|Stringable $path): ImageInterface */ public function decodeBinary(string|Stringable $binary): ImageInterface { - return $this->decode($binary, BinaryImageDecoder::class); + return $this->decode($binary, Decoders\BinaryImageDecoder::class); } /** @@ -134,7 +126,7 @@ public function decodeBinary(string|Stringable $binary): ImageInterface */ public function decodeSplFileInfo(SplFileInfo $splFileInfo): ImageInterface { - return $this->decode($splFileInfo, SplFileInfoImageDecoder::class); + return $this->decode($splFileInfo, Decoders\SplFileInfoImageDecoder::class); } /** @@ -148,7 +140,7 @@ public function decodeSplFileInfo(SplFileInfo $splFileInfo): ImageInterface */ public function decodeBase64(string|Stringable $base64): ImageInterface { - return $this->decode($base64, Base64ImageDecoder::class); + return $this->decode($base64, Decoders\Base64ImageDecoder::class); } /** @@ -162,7 +154,7 @@ public function decodeBase64(string|Stringable $base64): ImageInterface */ public function decodeDataUri(string|Stringable|DataUriInterface $dataUri): ImageInterface { - return $this->decode($dataUri, DataUriImageDecoder::class); + return $this->decode($dataUri, Decoders\DataUriImageDecoder::class); } /** @@ -176,6 +168,18 @@ public function decodeDataUri(string|Stringable|DataUriInterface $dataUri): Imag */ public function decodeStream(mixed $stream): ImageInterface { - return $this->decode($stream, [StreamImageDecoder::class, Psr7StreamImageDecoder::class]); + return $this->decode($stream, [Decoders\StreamImageDecoder::class, Decoders\Psr7StreamImageDecoder::class]); + } + + /** + * Decode an image from the contents of the HTTP response. + * + * @throws InvalidArgumentException + * @throws ImageDecoderException + * @throws DriverException + */ + public function decodeHttpResponse(mixed $response): ImageInterface + { + return $this->decode($response, Decoders\HttpResponseImageDecoder::class); } } diff --git a/src/InputHandler.php b/src/InputHandler.php index c5396f34d..4dff14aa4 100644 --- a/src/InputHandler.php +++ b/src/InputHandler.php @@ -19,9 +19,11 @@ use Intervention\Image\Decoders\DataUriImageDecoder; use Intervention\Image\Decoders\EncodedImageObjectDecoder; use Intervention\Image\Decoders\FilePathImageDecoder; +use Intervention\Image\Decoders\HttpResponseImageDecoder; use Intervention\Image\Decoders\StreamImageDecoder; use Intervention\Image\Decoders\ImageObjectDecoder; use Intervention\Image\Decoders\NativeObjectDecoder; +use Intervention\Image\Decoders\Psr7StreamImageDecoder; use Intervention\Image\Decoders\SplFileInfoImageDecoder; use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\InvalidArgumentException; @@ -41,6 +43,8 @@ class InputHandler implements InputHandlerInterface ImageObjectDecoder::class, NativeObjectDecoder::class, StreamImageDecoder::class, + Psr7StreamImageDecoder::class, + HttpResponseImageDecoder::class, SplFileInfoImageDecoder::class, EncodedImageObjectDecoder::class, DataUriImageDecoder::class, diff --git a/tests/Unit/Drivers/Gd/Decoders/HttpResponseImageDecoderTest.php b/tests/Unit/Drivers/Gd/Decoders/HttpResponseImageDecoderTest.php new file mode 100644 index 000000000..6ffd364c3 --- /dev/null +++ b/tests/Unit/Drivers/Gd/Decoders/HttpResponseImageDecoderTest.php @@ -0,0 +1,116 @@ +setDriver(new Driver()); + + $result = $decoder->decode($this->psr7HttpResponse()); + $this->assertInstanceOf(Image::class, $result); + } + + private function psr7HttpResponse(): object + { + return new class ($this->psr7Stream()) implements ResponseInterface + { + public function __construct(private object $stream) + { + // + } + + public function getStatusCode(): int + { + throw new Exception('Not implemented'); + } + + public function withStatus(int $code, string $reasonPhrase = ''): ResponseInterface + { + throw new Exception('Not implemented'); + } + + public function getReasonPhrase(): string + { + throw new Exception('Not implemented'); + } + + public function getProtocolVersion(): string + { + throw new Exception('Not implemented'); + } + + public function withProtocolVersion(string $version): MessageInterface + { + throw new Exception('Not implemented'); + } + + /** + * @return array + */ + public function getHeaders(): array + { + throw new Exception('Not implemented'); + } + + public function hasHeader(string $name): bool + { + throw new Exception('Not implemented'); + } + + /** + * @return array + */ + public function getHeader(string $name): array + { + throw new Exception('Not implemented'); + } + + public function getHeaderLine(string $name): string + { + throw new Exception('Not implemented'); + } + + public function withHeader(string $name, mixed $value): MessageInterface + { + throw new Exception('Not implemented'); + } + + public function withAddedHeader(string $name, mixed $value): MessageInterface + { + throw new Exception('Not implemented'); + } + + public function withoutHeader(string $name): MessageInterface + { + throw new Exception('Not implemented'); + } + + public function getBody(): StreamInterface + { + return $this->stream; + } + + public function withBody(StreamInterface $body): MessageInterface + { + throw new Exception('Not implemented'); + } + }; + } +} diff --git a/tests/Unit/Drivers/Gd/Decoders/Psr7StreamImageDecoderTest.php b/tests/Unit/Drivers/Gd/Decoders/Psr7StreamImageDecoderTest.php index cdcd8ad49..5268040bd 100644 --- a/tests/Unit/Drivers/Gd/Decoders/Psr7StreamImageDecoderTest.php +++ b/tests/Unit/Drivers/Gd/Decoders/Psr7StreamImageDecoderTest.php @@ -16,7 +16,7 @@ #[RequiresPhpExtension('gd')] #[CoversClass(Psr7StreamImageDecoder::class)] -final class Psr7StreamImageDecoderTest extends GdTestCase +class Psr7StreamImageDecoderTest extends GdTestCase { public function testDecode(): void { @@ -27,7 +27,7 @@ public function testDecode(): void $this->assertInstanceOf(Image::class, $result); } - private function psr7Stream(): object + protected function psr7Stream(): object { return new class () implements StreamInterface { diff --git a/tests/Unit/Drivers/Imagick/Decoders/HttpResponseImageDecoderTest.php b/tests/Unit/Drivers/Imagick/Decoders/HttpResponseImageDecoderTest.php new file mode 100644 index 000000000..73ca78e65 --- /dev/null +++ b/tests/Unit/Drivers/Imagick/Decoders/HttpResponseImageDecoderTest.php @@ -0,0 +1,116 @@ +setDriver(new Driver()); + + $result = $decoder->decode($this->psr7HttpResponse()); + $this->assertInstanceOf(Image::class, $result); + } + + private function psr7HttpResponse(): object + { + return new class ($this->psr7Stream()) implements ResponseInterface + { + public function __construct(private object $stream) + { + // + } + + public function getStatusCode(): int + { + throw new Exception('Not implemented'); + } + + public function withStatus(int $code, string $reasonPhrase = ''): ResponseInterface + { + throw new Exception('Not implemented'); + } + + public function getReasonPhrase(): string + { + throw new Exception('Not implemented'); + } + + public function getProtocolVersion(): string + { + throw new Exception('Not implemented'); + } + + public function withProtocolVersion(string $version): MessageInterface + { + throw new Exception('Not implemented'); + } + + /** + * @return array + */ + public function getHeaders(): array + { + throw new Exception('Not implemented'); + } + + public function hasHeader(string $name): bool + { + throw new Exception('Not implemented'); + } + + /** + * @return array + */ + public function getHeader(string $name): array + { + throw new Exception('Not implemented'); + } + + public function getHeaderLine(string $name): string + { + throw new Exception('Not implemented'); + } + + public function withHeader(string $name, mixed $value): MessageInterface + { + throw new Exception('Not implemented'); + } + + public function withAddedHeader(string $name, mixed $value): MessageInterface + { + throw new Exception('Not implemented'); + } + + public function withoutHeader(string $name): MessageInterface + { + throw new Exception('Not implemented'); + } + + public function getBody(): StreamInterface + { + return $this->stream; + } + + public function withBody(StreamInterface $body): MessageInterface + { + throw new Exception('Not implemented'); + } + }; + } +} diff --git a/tests/Unit/Drivers/Imagick/Decoders/Psr7StreamImageDecoderTest.php b/tests/Unit/Drivers/Imagick/Decoders/Psr7StreamImageDecoderTest.php index fe80c0f04..bf0c941a7 100644 --- a/tests/Unit/Drivers/Imagick/Decoders/Psr7StreamImageDecoderTest.php +++ b/tests/Unit/Drivers/Imagick/Decoders/Psr7StreamImageDecoderTest.php @@ -16,7 +16,7 @@ #[RequiresPhpExtension('imagick')] #[CoversClass(Psr7StreamImageDecoder::class)] -final class Psr7StreamImageDecoderTest extends ImagickTestCase +class Psr7StreamImageDecoderTest extends ImagickTestCase { public function testDecode(): void { @@ -27,7 +27,7 @@ public function testDecode(): void $this->assertInstanceOf(Image::class, $result); } - private function psr7Stream(): object + protected function psr7Stream(): object { return new class () implements StreamInterface {