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/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/Gd/Decoders/Psr7StreamImageDecoder.php b/src/Drivers/Gd/Decoders/Psr7StreamImageDecoder.php new file mode 100644 index 000000000..675ac0052 --- /dev/null +++ b/src/Drivers/Gd/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/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/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..d1018ee76 100644 --- a/src/ImageManager.php +++ b/src/ImageManager.php @@ -4,12 +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\StreamImageDecoder; -use Intervention\Image\Decoders\SplFileInfoImageDecoder; use Intervention\Image\Exceptions\DriverException; use Intervention\Image\Exceptions\ImageDecoderException; use Intervention\Image\Exceptions\InvalidArgumentException; @@ -19,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; @@ -105,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); } /** @@ -119,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); } /** @@ -133,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); } /** @@ -147,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); } /** @@ -161,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); } /** @@ -175,6 +168,18 @@ public function decodeDataUri(string|Stringable|DataUriInterface $dataUri): Imag */ public function decodeStream(mixed $stream): ImageInterface { - return $this->decode($stream, StreamImageDecoder::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 new file mode 100644 index 000000000..5268040bd --- /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); + } + + protected 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/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 new file mode 100644 index 000000000..bf0c941a7 --- /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); + } + + protected 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(); + } + }; + } +}