Skip to content
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
12 changes: 12 additions & 0 deletions src/Decoders/HttpResponseImageDecoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Intervention\Image\Decoders;

use Intervention\Image\Drivers\SpecializableDecoder;

class HttpResponseImageDecoder extends SpecializableDecoder
{
//
}
12 changes: 12 additions & 0 deletions src/Decoders/Psr7StreamImageDecoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Intervention\Image\Decoders;

use Intervention\Image\Drivers\SpecializableDecoder;

class Psr7StreamImageDecoder extends SpecializableDecoder
{
//
}
66 changes: 66 additions & 0 deletions src/Drivers/Gd/Decoders/HttpResponseImageDecoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Intervention\Image\Drivers\Gd\Decoders;

use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Exceptions\DriverException;
use Intervention\Image\Exceptions\StreamException;
use Intervention\Image\Exceptions\ImageDecoderException;
use Intervention\Image\Exceptions\InvalidArgumentException;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Exceptions\StateException;
use Intervention\Image\Interfaces\ImageInterface;

class HttpResponseImageDecoder extends BinaryImageDecoder
{
private const string DECODABLE_CLASSNAME = 'Psr\Http\Message\ResponseInterface';

/**
* {@inheritdoc}
*
* @see DecoderInterface::supports()
*/
public function supports(mixed $input): bool
{
return $this->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);
}
}
63 changes: 63 additions & 0 deletions src/Drivers/Gd/Decoders/Psr7StreamImageDecoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Intervention\Image\Drivers\Gd\Decoders;

use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Exceptions\DriverException;
use Intervention\Image\Exceptions\StreamException;
use Intervention\Image\Exceptions\ImageDecoderException;
use Intervention\Image\Exceptions\InvalidArgumentException;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Exceptions\StateException;
use Intervention\Image\Interfaces\ImageInterface;

class Psr7StreamImageDecoder extends BinaryImageDecoder
{
private const string DECODABLE_CLASSNAME = 'Psr\Http\Message\StreamInterface';

/**
* {@inheritdoc}
*
* @see DecoderInterface::supports()
*/
public function supports(mixed $input): bool
{
return $this->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);
}
}
66 changes: 66 additions & 0 deletions src/Drivers/Imagick/Decoders/HttpResponseImageDecoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Intervention\Image\Drivers\Imagick\Decoders;

use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Exceptions\DriverException;
use Intervention\Image\Exceptions\StreamException;
use Intervention\Image\Exceptions\ImageDecoderException;
use Intervention\Image\Exceptions\InvalidArgumentException;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Exceptions\StateException;
use Intervention\Image\Interfaces\ImageInterface;

class HttpResponseImageDecoder extends BinaryImageDecoder
{
private const string DECODABLE_CLASSNAME = 'Psr\Http\Message\ResponseInterface';

/**
* {@inheritdoc}
*
* @see DecoderInterface::supports()
*/
public function supports(mixed $input): bool
{
return $this->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);
}
}
63 changes: 63 additions & 0 deletions src/Drivers/Imagick/Decoders/Psr7StreamImageDecoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Intervention\Image\Drivers\Imagick\Decoders;

use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Exceptions\DriverException;
use Intervention\Image\Exceptions\StreamException;
use Intervention\Image\Exceptions\ImageDecoderException;
use Intervention\Image\Exceptions\InvalidArgumentException;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Exceptions\StateException;
use Intervention\Image\Interfaces\ImageInterface;

class Psr7StreamImageDecoder extends BinaryImageDecoder
{
private const string DECODABLE_CLASSNAME = 'Psr\Http\Message\StreamInterface';

/**
* {@inheritdoc}
*
* @see DecoderInterface::supports()
*/
public function supports(mixed $input): bool
{
return $this->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);
}
}
33 changes: 19 additions & 14 deletions src/ImageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}
}
Loading