From 6103f182270390580cb00e1ee22b24004f2c68a1 Mon Sep 17 00:00:00 2001 From: Konnng Date: Mon, 22 Apr 2024 10:29:43 -0300 Subject: [PATCH 01/20] chore: dependency bump --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index e6d578c..1d4f206 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ ], "require": { "php": "^8.1", - "intervention/image": "^3.3", + "intervention/image": "^3.6", "league/flysystem": "^3.0", "psr/http-message": "^1.0|^2.0" }, From 811370136f109735b9dfce1cfc87736024d55fdc Mon Sep 17 00:00:00 2001 From: Konnng Date: Mon, 22 Apr 2024 18:27:36 -0300 Subject: [PATCH 02/20] fix: update image generation to support progressive and interlaced images --- src/Api/Api.php | 8 ++- src/Manipulators/BaseManipulator.php | 3 +- src/Manipulators/Encode.php | 80 +++++++++++------------ src/Manipulators/ManipulatorInterface.php | 3 +- src/ServerFactory.php | 1 - 5 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/Api/Api.php b/src/Api/Api.php index 42799a2..ed8faa7 100644 --- a/src/Api/Api.php +++ b/src/Api/Api.php @@ -3,6 +3,7 @@ namespace League\Glide\Api; use Intervention\Image\ImageManager; +use League\Glide\Manipulators\Encode; use League\Glide\Manipulators\ManipulatorInterface; class Api implements ApiInterface @@ -91,10 +92,13 @@ public function run(string $source, array $params): string foreach ($this->manipulators as $manipulator) { $manipulator->setParams($params); - $image = $manipulator->run($image); } - return $image->encodeByMediaType()->toString(); + $encoder = new Encode(); + $encoder->setParams($params); + $encoded = $encoder->run($image); + + return $encoded->toString(); } } diff --git a/src/Manipulators/BaseManipulator.php b/src/Manipulators/BaseManipulator.php index 61df0e1..aba7147 100644 --- a/src/Manipulators/BaseManipulator.php +++ b/src/Manipulators/BaseManipulator.php @@ -2,6 +2,7 @@ namespace League\Glide\Manipulators; +use Intervention\Image\Interfaces\EncodedImageInterface; use Intervention\Image\Interfaces\ImageInterface; abstract class BaseManipulator implements ManipulatorInterface @@ -40,5 +41,5 @@ public function getParam(string $name): mixed * * @return ImageInterface The manipulated image. */ - abstract public function run(ImageInterface $image): ImageInterface; + abstract public function run(ImageInterface $image): ImageInterface | EncodedImageInterface; } diff --git a/src/Manipulators/Encode.php b/src/Manipulators/Encode.php index 1ce47de..3c432c4 100644 --- a/src/Manipulators/Encode.php +++ b/src/Manipulators/Encode.php @@ -2,11 +2,10 @@ namespace League\Glide\Manipulators; -use Intervention\Image\Drivers\Gd\Driver as GdDriver; -use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver; -use Intervention\Image\ImageManager; -use Intervention\Image\Interfaces\DriverInterface; +use Intervention\Image\Interfaces\EncodedImageInterface; use Intervention\Image\Interfaces\ImageInterface; +use Intervention\Image\FileExtension; +use Intervention\Image\MediaType; class Encode extends BaseManipulator { @@ -15,30 +14,40 @@ class Encode extends BaseManipulator * * @param ImageInterface $image The source image. * - * @return ImageInterface The manipulated image. + * @return EncodedImageInterface The encoded image. */ - public function run(ImageInterface $image): ImageInterface + public function run(ImageInterface $image): EncodedImageInterface { $format = $this->getFormat($image); $quality = $this->getQuality(); - $driver = $image->driver(); - $interlace = false; + $shouldInterlace = filter_var($this->getParam('interlace'), FILTER_VALIDATE_BOOLEAN); if ('pjpg' === $format) { - $interlace = true; - - $format = 'jpg'; + $shouldInterlace = true; + $format = FileExtension::JPG->value; } - $image = (new ImageManager($driver))->read( - $image->encodeByExtension($format, quality: $quality)->toString() - ); - - if ($interlace) { - $image = $this->interlace($image, $driver); + $encoderOptions = ['extension' => $format]; + switch ($format) { + case FileExtension::AVIF->value: + case FileExtension::HEIC->value: + case FileExtension::AVIF->value: + case FileExtension::TIFF->value: + case FileExtension::AVIF->value: + case FileExtension::JPG->value: + case FileExtension::WEBP->value: + $encoderOptions['quality'] = $quality; + case FileExtension::JPG->value: + $encoderOptions['progressive'] = $shouldInterlace; + break; + case FileExtension::GIF->value: + case FileExtension::PNG->value: + $encoderOptions['interlaced'] = $shouldInterlace; + break; + default: } - return $image; + return $image->encodeByExtension(...$encoderOptions); } /** @@ -57,7 +66,7 @@ public function getFormat(ImageInterface $image): string } /** @psalm-suppress RiskyTruthyFalsyComparison */ - return array_search($image->origin()->mediaType(), static::supportedFormats(), true) ?: 'jpg'; + return array_search($image->origin()->mediaType(), static::supportedFormats(), true) ?: FileExtension::JPG->value; } /** @@ -68,14 +77,15 @@ public function getFormat(ImageInterface $image): string public static function supportedFormats(): array { return [ - 'avif' => 'image/avif', - 'gif' => 'image/gif', - 'jpg' => 'image/jpeg', - 'pjpg' => 'image/jpeg', - 'png' => 'image/png', - 'webp' => 'image/webp', - 'tiff' => 'image/tiff', - 'heic' => 'image/heic', + FileExtension::AVIF->value => MediaType::IMAGE_AVIF->value, + FileExtension::GIF->value => MediaType::IMAGE_GIF->value, + FileExtension::JPEG->value => MediaType::IMAGE_JPEG->value, + FileExtension::JPG->value => MediaType::IMAGE_JPEG->value, + 'pjpg' => MediaType::IMAGE_JPEG->value, + FileExtension::PNG->value => MediaType::IMAGE_PNG->value, + FileExtension::WEBP->value => MediaType::IMAGE_WEBP->value, + FileExtension::TIF->value => MediaType::IMAGE_TIFF->value, + FileExtension::HEIC->value => MediaType::IMAGE_HEIC->value, ]; } @@ -89,7 +99,8 @@ public function getQuality(): int $default = 90; $q = $this->getParam('q'); - if (!is_numeric($q) + if ( + !is_numeric($q) || $q < 0 || $q > 100 ) { @@ -98,17 +109,4 @@ public function getQuality(): int return (int) $q; } - - protected function interlace(ImageInterface $image, DriverInterface $driver): ImageInterface - { - $img = $image->core()->native(); - - if ($driver instanceof ImagickDriver) { - $img->setInterlaceScheme(\Imagick::INTERLACE_PLANE); - } elseif ($driver instanceof GdDriver) { - imageinterlace($img, true); - } - - return $image; - } } diff --git a/src/Manipulators/ManipulatorInterface.php b/src/Manipulators/ManipulatorInterface.php index d04c266..3efb48a 100644 --- a/src/Manipulators/ManipulatorInterface.php +++ b/src/Manipulators/ManipulatorInterface.php @@ -2,6 +2,7 @@ namespace League\Glide\Manipulators; +use Intervention\Image\Interfaces\EncodedImageInterface; use Intervention\Image\Interfaces\ImageInterface; interface ManipulatorInterface @@ -27,5 +28,5 @@ public function getParam(string $name): mixed; * * @return ImageInterface The manipulated image. */ - public function run(ImageInterface $image): ImageInterface; + public function run(ImageInterface $image): ImageInterface | EncodedImageInterface; } diff --git a/src/ServerFactory.php b/src/ServerFactory.php index eb08e7b..5597125 100644 --- a/src/ServerFactory.php +++ b/src/ServerFactory.php @@ -255,7 +255,6 @@ public function getManipulators(): array new Watermark($this->getWatermarks(), $this->getWatermarksPathPrefix() ?? ''), new Background(), new Border(), - new Encode(), ]; } From 9a17fb5afd9a0470ac15c6055b70cd7227929884 Mon Sep 17 00:00:00 2001 From: Konnng Date: Wed, 8 May 2024 09:27:58 -0300 Subject: [PATCH 03/20] fix: linting issues --- src/Manipulators/BaseManipulator.php | 2 +- src/Manipulators/Encode.php | 3 ++- src/Manipulators/ManipulatorInterface.php | 2 +- src/ServerFactory.php | 1 - 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Manipulators/BaseManipulator.php b/src/Manipulators/BaseManipulator.php index aba7147..85d2d60 100644 --- a/src/Manipulators/BaseManipulator.php +++ b/src/Manipulators/BaseManipulator.php @@ -41,5 +41,5 @@ public function getParam(string $name): mixed * * @return ImageInterface The manipulated image. */ - abstract public function run(ImageInterface $image): ImageInterface | EncodedImageInterface; + abstract public function run(ImageInterface $image): ImageInterface|EncodedImageInterface; } diff --git a/src/Manipulators/Encode.php b/src/Manipulators/Encode.php index 3c432c4..951b7cd 100644 --- a/src/Manipulators/Encode.php +++ b/src/Manipulators/Encode.php @@ -2,9 +2,9 @@ namespace League\Glide\Manipulators; +use Intervention\Image\FileExtension; use Intervention\Image\Interfaces\EncodedImageInterface; use Intervention\Image\Interfaces\ImageInterface; -use Intervention\Image\FileExtension; use Intervention\Image\MediaType; class Encode extends BaseManipulator @@ -37,6 +37,7 @@ public function run(ImageInterface $image): EncodedImageInterface case FileExtension::JPG->value: case FileExtension::WEBP->value: $encoderOptions['quality'] = $quality; + // no break case FileExtension::JPG->value: $encoderOptions['progressive'] = $shouldInterlace; break; diff --git a/src/Manipulators/ManipulatorInterface.php b/src/Manipulators/ManipulatorInterface.php index 3efb48a..0169c14 100644 --- a/src/Manipulators/ManipulatorInterface.php +++ b/src/Manipulators/ManipulatorInterface.php @@ -28,5 +28,5 @@ public function getParam(string $name): mixed; * * @return ImageInterface The manipulated image. */ - public function run(ImageInterface $image): ImageInterface | EncodedImageInterface; + public function run(ImageInterface $image): ImageInterface|EncodedImageInterface; } diff --git a/src/ServerFactory.php b/src/ServerFactory.php index 5597125..d2e9acc 100644 --- a/src/ServerFactory.php +++ b/src/ServerFactory.php @@ -13,7 +13,6 @@ use League\Glide\Manipulators\Brightness; use League\Glide\Manipulators\Contrast; use League\Glide\Manipulators\Crop; -use League\Glide\Manipulators\Encode; use League\Glide\Manipulators\Filter; use League\Glide\Manipulators\Flip; use League\Glide\Manipulators\Gamma; From 923669c0d0fd3fea2cd508fb5bdd5acf618c21cb Mon Sep 17 00:00:00 2001 From: Konnng Date: Wed, 8 May 2024 09:37:48 -0300 Subject: [PATCH 04/20] fix: remove repeated condition --- src/Manipulators/Encode.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Manipulators/Encode.php b/src/Manipulators/Encode.php index 951b7cd..51dd6f5 100644 --- a/src/Manipulators/Encode.php +++ b/src/Manipulators/Encode.php @@ -31,9 +31,7 @@ public function run(ImageInterface $image): EncodedImageInterface switch ($format) { case FileExtension::AVIF->value: case FileExtension::HEIC->value: - case FileExtension::AVIF->value: case FileExtension::TIFF->value: - case FileExtension::AVIF->value: case FileExtension::JPG->value: case FileExtension::WEBP->value: $encoderOptions['quality'] = $quality; From 6d50e0fb895cd9a651765c4ef21209f49bae4949 Mon Sep 17 00:00:00 2001 From: Konnng Date: Wed, 8 May 2024 11:10:58 -0300 Subject: [PATCH 05/20] fix(test): fix Encoding test --- tests/Manipulators/EncodeTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/Manipulators/EncodeTest.php b/tests/Manipulators/EncodeTest.php index f3a5d1d..b29ea80 100644 --- a/tests/Manipulators/EncodeTest.php +++ b/tests/Manipulators/EncodeTest.php @@ -2,6 +2,7 @@ namespace League\Glide\Manipulators; +use Intervention\Image\EncodedImage; use Intervention\Image\Encoders\MediaTypeEncoder; use Intervention\Image\ImageManager; use Intervention\Image\Interfaces\ImageInterface; @@ -123,6 +124,7 @@ public function testGetFormat() $this->assertSame('png', $this->manipulator->setParams(['fm' => null])->getFormat($image)); $this->assertSame('gif', $this->manipulator->setParams(['fm' => null])->getFormat($image)); $this->assertSame('jpg', $this->manipulator->setParams(['fm' => null])->getFormat($image)); + $this->assertSame('jpg', $this->manipulator->setParams(['fm' => ''])->getFormat($image)); $this->assertSame('jpg', $this->manipulator->setParams(['fm' => 'invalid'])->getFormat($image)); @@ -188,8 +190,8 @@ public function testSupportedFormats() $this->assertSame($expected, Encode::supportedFormats()); } - protected function getMime(ImageInterface $image) + protected function getMime(EncodedImage $image) { - return $image->origin()->mediaType(); + return $image->mediaType(); } } From 0a78cc521aaf30b5d399062ffc534d1e1b10f22c Mon Sep 17 00:00:00 2001 From: Konnng Date: Wed, 8 May 2024 11:11:45 -0300 Subject: [PATCH 06/20] fix: revert changes --- src/Manipulators/Encode.php | 39 +++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/Manipulators/Encode.php b/src/Manipulators/Encode.php index 51dd6f5..11a240e 100644 --- a/src/Manipulators/Encode.php +++ b/src/Manipulators/Encode.php @@ -2,10 +2,8 @@ namespace League\Glide\Manipulators; -use Intervention\Image\FileExtension; use Intervention\Image\Interfaces\EncodedImageInterface; use Intervention\Image\Interfaces\ImageInterface; -use Intervention\Image\MediaType; class Encode extends BaseManipulator { @@ -24,23 +22,23 @@ public function run(ImageInterface $image): EncodedImageInterface if ('pjpg' === $format) { $shouldInterlace = true; - $format = FileExtension::JPG->value; + $format = 'jpg'; } $encoderOptions = ['extension' => $format]; switch ($format) { - case FileExtension::AVIF->value: - case FileExtension::HEIC->value: - case FileExtension::TIFF->value: - case FileExtension::JPG->value: - case FileExtension::WEBP->value: + case 'avif': + case 'heic': + case 'tiff': + case 'jpg': + case 'webp': $encoderOptions['quality'] = $quality; // no break - case FileExtension::JPG->value: + case 'jpg': $encoderOptions['progressive'] = $shouldInterlace; break; - case FileExtension::GIF->value: - case FileExtension::PNG->value: + case 'gif': + case 'png': $encoderOptions['interlaced'] = $shouldInterlace; break; default: @@ -65,7 +63,7 @@ public function getFormat(ImageInterface $image): string } /** @psalm-suppress RiskyTruthyFalsyComparison */ - return array_search($image->origin()->mediaType(), static::supportedFormats(), true) ?: FileExtension::JPG->value; + return array_search($image->origin()->mediaType(), static::supportedFormats(), true) ?: 'jpg'; } /** @@ -76,15 +74,14 @@ public function getFormat(ImageInterface $image): string public static function supportedFormats(): array { return [ - FileExtension::AVIF->value => MediaType::IMAGE_AVIF->value, - FileExtension::GIF->value => MediaType::IMAGE_GIF->value, - FileExtension::JPEG->value => MediaType::IMAGE_JPEG->value, - FileExtension::JPG->value => MediaType::IMAGE_JPEG->value, - 'pjpg' => MediaType::IMAGE_JPEG->value, - FileExtension::PNG->value => MediaType::IMAGE_PNG->value, - FileExtension::WEBP->value => MediaType::IMAGE_WEBP->value, - FileExtension::TIF->value => MediaType::IMAGE_TIFF->value, - FileExtension::HEIC->value => MediaType::IMAGE_HEIC->value, + 'avif' => 'image/avif', + 'gif' => 'image/gif', + 'jpg' => 'image/jpeg', + 'pjpg' => 'image/jpeg', + 'png' => 'image/png', + 'webp' => 'image/webp', + 'tiff' => 'image/tiff', + 'heic' => 'image/heic', ]; } From 6a6cce243e3b090cb5a54e8a59f23f1fc6025037 Mon Sep 17 00:00:00 2001 From: Konnng Date: Wed, 8 May 2024 11:26:31 -0300 Subject: [PATCH 07/20] fix(test): fix ApiTest assertions --- tests/Api/ApiTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/Api/ApiTest.php b/tests/Api/ApiTest.php index da3b58c..c55b2c1 100644 --- a/tests/Api/ApiTest.php +++ b/tests/Api/ApiTest.php @@ -61,7 +61,11 @@ public function testGetManipulators() public function testRun() { $image = \Mockery::mock(ImageInterface::class, function ($mock) { - $mock->shouldReceive('encodeByMediaType')->andReturn(\Mockery::mock(EncodedImageInterface::class, function ($mock) { + $mock->shouldReceive('origin')->andReturn(\Mockery::mock('\Intervention\Image\Origin', function ($mock) { + $mock->shouldReceive('mediaType')->andReturn('image/png'); + })); + + $mock->shouldReceive('encodeByExtension')->with('png')->andReturn(\Mockery::mock(EncodedImageInterface::class, function ($mock) { $mock->shouldReceive('toString')->andReturn('encoded'); })); }); From bb744d15e6337215bfa22e18811511e9f1aca676 Mon Sep 17 00:00:00 2001 From: Konnng Date: Wed, 8 May 2024 13:57:53 -0300 Subject: [PATCH 08/20] fix: update logic to avoid psalm error --- src/Manipulators/Encode.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Manipulators/Encode.php b/src/Manipulators/Encode.php index 11a240e..0313131 100644 --- a/src/Manipulators/Encode.php +++ b/src/Manipulators/Encode.php @@ -30,11 +30,11 @@ public function run(ImageInterface $image): EncodedImageInterface case 'avif': case 'heic': case 'tiff': - case 'jpg': case 'webp': $encoderOptions['quality'] = $quality; - // no break + break; case 'jpg': + $encoderOptions['quality'] = $quality; $encoderOptions['progressive'] = $shouldInterlace; break; case 'gif': From 11fb2204e9ca5e7774241b43dd70fda23e745773 Mon Sep 17 00:00:00 2001 From: Konnng Date: Wed, 8 May 2024 14:00:20 -0300 Subject: [PATCH 09/20] chore: update returning types documentation --- src/Manipulators/BaseManipulator.php | 2 +- src/Manipulators/ManipulatorInterface.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Manipulators/BaseManipulator.php b/src/Manipulators/BaseManipulator.php index 85d2d60..c9108cb 100644 --- a/src/Manipulators/BaseManipulator.php +++ b/src/Manipulators/BaseManipulator.php @@ -39,7 +39,7 @@ public function getParam(string $name): mixed /** * Perform the image manipulation. * - * @return ImageInterface The manipulated image. + * @return ImageInterface|EncodedImageInterface The manipulated image. */ abstract public function run(ImageInterface $image): ImageInterface|EncodedImageInterface; } diff --git a/src/Manipulators/ManipulatorInterface.php b/src/Manipulators/ManipulatorInterface.php index 0169c14..cb11be1 100644 --- a/src/Manipulators/ManipulatorInterface.php +++ b/src/Manipulators/ManipulatorInterface.php @@ -26,7 +26,7 @@ public function getParam(string $name): mixed; * * @param ImageInterface $image The source image. * - * @return ImageInterface The manipulated image. + * @return ImageInterface|EncodedImageInterface The manipulated image. */ public function run(ImageInterface $image): ImageInterface|EncodedImageInterface; } From b0086f8f05bbd9bc0e9c5b352d9330c05438f3ba Mon Sep 17 00:00:00 2001 From: Konnng Date: Thu, 9 May 2024 11:00:53 -0300 Subject: [PATCH 10/20] fix: psalm validation issues addressed --- src/Api/Api.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Api/Api.php b/src/Api/Api.php index ed8faa7..495c2c0 100644 --- a/src/Api/Api.php +++ b/src/Api/Api.php @@ -92,11 +92,13 @@ public function run(string $source, array $params): string foreach ($this->manipulators as $manipulator) { $manipulator->setParams($params); + /** @var \Intervention\Image\Interfaces\ImageInterface */ $image = $manipulator->run($image); } $encoder = new Encode(); $encoder->setParams($params); + /** @var \Intervention\Image\Interfaces\EncodedImageInterface */ $encoded = $encoder->run($image); return $encoded->toString(); From 62623feaeb6a1f6cf068693cc001fb16d50582cf Mon Sep 17 00:00:00 2001 From: Konnng Date: Mon, 3 Jun 2024 10:58:36 -0300 Subject: [PATCH 11/20] fix: move Encoder manipulator do Api --- src/Api/Api.php | 20 ++++++++++++---- src/{Manipulators => Api}/Encode.php | 36 +++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 8 deletions(-) rename src/{Manipulators => Api}/Encode.php (79%) diff --git a/src/Api/Api.php b/src/Api/Api.php index 495c2c0..9df1f24 100644 --- a/src/Api/Api.php +++ b/src/Api/Api.php @@ -3,7 +3,7 @@ namespace League\Glide\Api; use Intervention\Image\ImageManager; -use League\Glide\Manipulators\Encode; +use Intervention\Image\Interfaces\ImageInterface; use League\Glide\Manipulators\ManipulatorInterface; class Api implements ApiInterface @@ -92,13 +92,23 @@ public function run(string $source, array $params): string foreach ($this->manipulators as $manipulator) { $manipulator->setParams($params); - /** @var \Intervention\Image\Interfaces\ImageInterface */ $image = $manipulator->run($image); } - $encoder = new Encode(); - $encoder->setParams($params); - /** @var \Intervention\Image\Interfaces\EncodedImageInterface */ + return $this->encode($image, $params); + } + + /** + * Perform image encoding to a given format. + * + * @param ImageInterface $image Image object + * @param array $params the manipulator params + * + * @return string Manipulated image binary data + */ + public function encode(ImageInterface $image, array $params): string + { + $encoder = new Encode($params); $encoded = $encoder->run($image); return $encoded->toString(); diff --git a/src/Manipulators/Encode.php b/src/Api/Encode.php similarity index 79% rename from src/Manipulators/Encode.php rename to src/Api/Encode.php index 0313131..8721503 100644 --- a/src/Manipulators/Encode.php +++ b/src/Api/Encode.php @@ -1,12 +1,41 @@ params = $params; + } + + /** + * Get a specific manipulation param. + */ + public function getParam(string $name): mixed + { + return array_key_exists($name, $this->params) + ? $this->params[$name] + : null; + } + /** * Perform output image manipulation. * @@ -42,6 +71,7 @@ public function run(ImageInterface $image): EncodedImageInterface $encoderOptions['interlaced'] = $shouldInterlace; break; default: + throw new Exception("Invalid format provided: {$format}"); } return $image->encodeByExtension(...$encoderOptions); From c5eb1e2ca070e347a85c44253ecad7094b2c5b30 Mon Sep 17 00:00:00 2001 From: Konnng Date: Mon, 3 Jun 2024 10:58:59 -0300 Subject: [PATCH 12/20] fix: revert typings return --- src/Manipulators/BaseManipulator.php | 5 ++--- src/Manipulators/ManipulatorInterface.php | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Manipulators/BaseManipulator.php b/src/Manipulators/BaseManipulator.php index c9108cb..61df0e1 100644 --- a/src/Manipulators/BaseManipulator.php +++ b/src/Manipulators/BaseManipulator.php @@ -2,7 +2,6 @@ namespace League\Glide\Manipulators; -use Intervention\Image\Interfaces\EncodedImageInterface; use Intervention\Image\Interfaces\ImageInterface; abstract class BaseManipulator implements ManipulatorInterface @@ -39,7 +38,7 @@ public function getParam(string $name): mixed /** * Perform the image manipulation. * - * @return ImageInterface|EncodedImageInterface The manipulated image. + * @return ImageInterface The manipulated image. */ - abstract public function run(ImageInterface $image): ImageInterface|EncodedImageInterface; + abstract public function run(ImageInterface $image): ImageInterface; } diff --git a/src/Manipulators/ManipulatorInterface.php b/src/Manipulators/ManipulatorInterface.php index cb11be1..d04c266 100644 --- a/src/Manipulators/ManipulatorInterface.php +++ b/src/Manipulators/ManipulatorInterface.php @@ -2,7 +2,6 @@ namespace League\Glide\Manipulators; -use Intervention\Image\Interfaces\EncodedImageInterface; use Intervention\Image\Interfaces\ImageInterface; interface ManipulatorInterface @@ -26,7 +25,7 @@ public function getParam(string $name): mixed; * * @param ImageInterface $image The source image. * - * @return ImageInterface|EncodedImageInterface The manipulated image. + * @return ImageInterface The manipulated image. */ - public function run(ImageInterface $image): ImageInterface|EncodedImageInterface; + public function run(ImageInterface $image): ImageInterface; } From a602e8f0fafa050d589fb9925c32996443aa3140 Mon Sep 17 00:00:00 2001 From: Konnng Date: Mon, 3 Jun 2024 12:03:20 -0300 Subject: [PATCH 13/20] test: move Encoder test --- src/Api/Encode.php | 21 ++- tests/Api/EncodeTest.php | 254 ++++++++++++++++++++++++++++++ tests/Manipulators/EncodeTest.php | 197 ----------------------- 3 files changed, 271 insertions(+), 201 deletions(-) create mode 100644 tests/Api/EncodeTest.php delete mode 100644 tests/Manipulators/EncodeTest.php diff --git a/src/Api/Encode.php b/src/Api/Encode.php index 8721503..735c253 100644 --- a/src/Api/Encode.php +++ b/src/Api/Encode.php @@ -2,15 +2,14 @@ namespace League\Glide\Api; -use Exception; use Intervention\Image\Interfaces\EncodedImageInterface; use Intervention\Image\Interfaces\ImageInterface; /** * Encoder Api class to convert a given image to a specific format. */ -class Encode { - +class Encode +{ /** * The manipulation params. */ @@ -26,6 +25,20 @@ public function __construct(array $params = []) $this->params = $params; } + /** + * Set the manipulation params. + * + * @param array $params The manipulation params. + * + * @return $this + */ + public function setParams(array $params) + { + $this->params = $params; + + return $this; + } + /** * Get a specific manipulation param. */ @@ -71,7 +84,7 @@ public function run(ImageInterface $image): EncodedImageInterface $encoderOptions['interlaced'] = $shouldInterlace; break; default: - throw new Exception("Invalid format provided: {$format}"); + throw new \Exception("Invalid format provided: {$format}"); } return $image->encodeByExtension(...$encoderOptions); diff --git a/tests/Api/EncodeTest.php b/tests/Api/EncodeTest.php new file mode 100644 index 0000000..21f8d8b --- /dev/null +++ b/tests/Api/EncodeTest.php @@ -0,0 +1,254 @@ +jpg = $manager->read( + $manager->create(100, 100)->encode(new MediaTypeEncoder('image/jpeg'))->toFilePointer() + ); + $this->png = $manager->read( + $manager->create(100, 100)->encode(new MediaTypeEncoder('image/png'))->toFilePointer() + ); + $this->gif = $manager->read( + $manager->create(100, 100)->encode(new MediaTypeEncoder('image/gif'))->toFilePointer() + ); + + if (function_exists('imagecreatefromwebp')) { + $this->webp = $manager->read( + $manager->create(100, 100)->encode(new MediaTypeEncoder('image/webp'))->toFilePointer() + ); + } + + if (function_exists('imagecreatefromavif')) { + $this->avif = $manager->read( + $manager->create(100, 100)->encode(new MediaTypeEncoder('image/avif'))->toFilePointer() + ); + } + + $this->encoder = new Encode(); + } + + public function tearDown(): void + { + \Mockery::close(); + } + + public function testCreateInstance(): void + { + /** + * @psalm-suppress ArgumentTypeCoercion + */ + $this->assertInstanceOf('League\Glide\Api\Encode', $this->encoder); + } + + public function testRun(): void + { + $this->assertSame('image/jpeg', $this->getMime($this->encoder->setParams(['fm' => 'jpg'])->run($this->jpg))); + $this->assertSame('image/jpeg', $this->getMime($this->encoder->setParams(['fm' => 'jpg'])->run($this->png))); + $this->assertSame('image/jpeg', $this->getMime($this->encoder->setParams(['fm' => 'jpg'])->run($this->gif))); + $this->assertSame('image/jpeg', $this->getMime($this->encoder->setParams(['fm' => 'pjpg'])->run($this->jpg))); + $this->assertSame('image/jpeg', $this->getMime($this->encoder->setParams(['fm' => 'pjpg'])->run($this->png))); + $this->assertSame('image/jpeg', $this->getMime($this->encoder->setParams(['fm' => 'pjpg'])->run($this->gif))); + $this->assertSame('image/png', $this->getMime($this->encoder->setParams(['fm' => 'png'])->run($this->jpg))); + $this->assertSame('image/png', $this->getMime($this->encoder->setParams(['fm' => 'png'])->run($this->png))); + $this->assertSame('image/png', $this->getMime($this->encoder->setParams(['fm' => 'png'])->run($this->gif))); + $this->assertSame('image/gif', $this->getMime($this->encoder->setParams(['fm' => 'gif'])->run($this->jpg))); + $this->assertSame('image/gif', $this->getMime($this->encoder->setParams(['fm' => 'gif'])->run($this->png))); + $this->assertSame('image/gif', $this->getMime($this->encoder->setParams(['fm' => 'gif'])->run($this->gif))); + + if (function_exists('imagecreatefromwebp')) { + $this->assertSame('image/jpeg', $this->getMime($this->encoder->setParams(['fm' => 'jpg'])->run($this->webp))); + $this->assertSame('image/jpeg', $this->getMime($this->encoder->setParams(['fm' => 'pjpg'])->run($this->webp))); + $this->assertSame('image/png', $this->getMime($this->encoder->setParams(['fm' => 'png'])->run($this->webp))); + $this->assertSame('image/gif', $this->getMime($this->encoder->setParams(['fm' => 'gif'])->run($this->webp))); + $this->assertSame('image/webp', $this->getMime($this->encoder->setParams(['fm' => 'webp'])->run($this->jpg))); + $this->assertSame('image/webp', $this->getMime($this->encoder->setParams(['fm' => 'webp'])->run($this->png))); + $this->assertSame('image/webp', $this->getMime($this->encoder->setParams(['fm' => 'webp'])->run($this->gif))); + $this->assertSame('image/webp', $this->getMime($this->encoder->setParams(['fm' => 'webp'])->run($this->webp))); + } + if (function_exists('imagecreatefromavif')) { + $this->assertSame('image/jpeg', $this->getMime($this->encoder->setParams(['fm' => 'jpg'])->run($this->avif))); + $this->assertSame('image/jpeg', $this->getMime($this->encoder->setParams(['fm' => 'pjpg'])->run($this->avif))); + $this->assertSame('image/png', $this->getMime($this->encoder->setParams(['fm' => 'png'])->run($this->avif))); + $this->assertSame('image/gif', $this->getMime($this->encoder->setParams(['fm' => 'gif'])->run($this->avif))); + $this->assertSame('image/avif', $this->getMime($this->encoder->setParams(['fm' => 'avif'])->run($this->jpg))); + $this->assertSame('image/avif', $this->getMime($this->encoder->setParams(['fm' => 'avif'])->run($this->png))); + $this->assertSame('image/avif', $this->getMime($this->encoder->setParams(['fm' => 'avif'])->run($this->gif))); + $this->assertSame('image/avif', $this->getMime($this->encoder->setParams(['fm' => 'avif'])->run($this->avif))); + } + + if (function_exists('imagecreatefromwebp') && function_exists('imagecreatefromavif')) { + $this->assertSame('image/webp', $this->getMime($this->encoder->setParams(['fm' => 'webp'])->run($this->avif))); + $this->assertSame('image/avif', $this->getMime($this->encoder->setParams(['fm' => 'avif'])->run($this->webp))); + } + } + + public function testGetFormat(): void + { + $image = \Mockery::mock(ImageInterface::class, function (Mock $mock) { + $this->assertMediaType($mock, 'image/jpeg')->once(); + $this->assertMediaType($mock, 'image/png')->once(); + $this->assertMediaType($mock, 'image/gif')->once(); + $this->assertMediaType($mock, 'image/bmp')->once(); + $this->assertMediaType($mock, 'image/jpeg')->twice(); + + if (function_exists('imagecreatefromwebp')) { + $this->assertMediaType($mock, 'image/webp')->once(); + } + + if (function_exists('imagecreatefromavif')) { + $this->assertMediaType($mock, 'image/avif')->once(); + } + }); + + $this->assertSame('jpg', $this->encoder->setParams(['fm' => 'jpg'])->getFormat($image)); + $this->assertSame('png', $this->encoder->setParams(['fm' => 'png'])->getFormat($image)); + $this->assertSame('gif', $this->encoder->setParams(['fm' => 'gif'])->getFormat($image)); + $this->assertSame('jpg', $this->encoder->setParams(['fm' => null])->getFormat($image)); + $this->assertSame('png', $this->encoder->setParams(['fm' => null])->getFormat($image)); + $this->assertSame('gif', $this->encoder->setParams(['fm' => null])->getFormat($image)); + $this->assertSame('jpg', $this->encoder->setParams(['fm' => null])->getFormat($image)); + + $this->assertSame('jpg', $this->encoder->setParams(['fm' => ''])->getFormat($image)); + $this->assertSame('jpg', $this->encoder->setParams(['fm' => 'invalid'])->getFormat($image)); + + if (function_exists('imagecreatefromwebp')) { + $this->assertSame('webp', $this->encoder->setParams(['fm' => null])->getFormat($image)); + } + + if (function_exists('imagecreatefromavif')) { + $this->assertSame('avif', $this->encoder->setParams(['fm' => null])->getFormat($image)); + } + } + + public function testGetQuality(): void + { + $this->assertSame(100, $this->encoder->setParams(['q' => '100'])->getQuality()); + $this->assertSame(100, $this->encoder->setParams(['q' => 100])->getQuality()); + $this->assertSame(90, $this->encoder->setParams(['q' => null])->getQuality()); + $this->assertSame(90, $this->encoder->setParams(['q' => 'a'])->getQuality()); + $this->assertSame(50, $this->encoder->setParams(['q' => '50.50'])->getQuality()); + $this->assertSame(90, $this->encoder->setParams(['q' => '-1'])->getQuality()); + $this->assertSame(90, $this->encoder->setParams(['q' => '101'])->getQuality()); + } + + /** + * Test functions that require the imagick extension. + */ + public function testWithImagick(): void + { + if (!extension_loaded('imagick')) { + $this->markTestSkipped( + 'The imagick extension is not available.' + ); + } + $manager = ImageManager::imagick(); + // These need to be recreated with the imagick driver selected in the manager + $this->jpg = $manager->read($manager->create(100, 100)->encode(new MediaTypeEncoder('image/jpeg'))->toFilePointer()); + $this->png = $manager->read($manager->create(100, 100)->encode(new MediaTypeEncoder('image/png'))->toFilePointer()); + $this->gif = $manager->read($manager->create(100, 100)->encode(new MediaTypeEncoder('image/gif'))->toFilePointer()); + $this->heic = $manager->read($manager->create(100, 100)->encode(new MediaTypeEncoder('image/heic'))->toFilePointer()); + $this->tif = $manager->read($manager->create(100, 100)->encode(new MediaTypeEncoder('image/tiff'))->toFilePointer()); + + $this->assertSame('image/tiff', $this->getMime($this->encoder->setParams(['fm' => 'tiff'])->run($this->jpg))); + $this->assertSame('image/tiff', $this->getMime($this->encoder->setParams(['fm' => 'tiff'])->run($this->png))); + $this->assertSame('image/tiff', $this->getMime($this->encoder->setParams(['fm' => 'tiff'])->run($this->gif))); + $this->assertSame('image/tiff', $this->getMime($this->encoder->setParams(['fm' => 'tiff'])->run($this->heic))); + } + + public function testSupportedFormats(): void + { + $expected = [ + 'avif' => 'image/avif', + 'gif' => 'image/gif', + 'jpg' => 'image/jpeg', + 'pjpg' => 'image/jpeg', + 'png' => 'image/png', + 'webp' => 'image/webp', + 'tiff' => 'image/tiff', + 'heic' => 'image/heic', + ]; + + $this->assertSame($expected, Encode::supportedFormats()); + } + + protected function getMime(EncodedImageInterface $image): string + { + return $image->mediaType(); + } + + /** + * Creates an assertion to check media type. + */ + /** + * @psalm-suppress MoreSpecificReturnType + */ + protected function assertMediaType(Mock $mock, string $mediaType): Mockery\Expectation + { + /** + * @psalm-suppress LessSpecificReturnStatement, UndefinedMagicMethod + */ + return $mock->shouldReceive('origin')->andReturn(\Mockery::mock('Intervention\Image\Origin', ['mediaType' => $mediaType])); + } +} diff --git a/tests/Manipulators/EncodeTest.php b/tests/Manipulators/EncodeTest.php deleted file mode 100644 index b29ea80..0000000 --- a/tests/Manipulators/EncodeTest.php +++ /dev/null @@ -1,197 +0,0 @@ -jpg = $manager->read( - $manager->create(100, 100)->encode(new MediaTypeEncoder('image/jpeg'))->toFilePointer() - ); - $this->png = $manager->read( - $manager->create(100, 100)->encode(new MediaTypeEncoder('image/png'))->toFilePointer() - ); - $this->gif = $manager->read( - $manager->create(100, 100)->encode(new MediaTypeEncoder('image/gif'))->toFilePointer() - ); - - if (function_exists('imagecreatefromwebp')) { - $this->webp = $manager->read( - $manager->create(100, 100)->encode(new MediaTypeEncoder('image/webp'))->toFilePointer() - ); - } - - if (function_exists('imagecreatefromavif')) { - $this->avif = $manager->read( - $manager->create(100, 100)->encode(new MediaTypeEncoder('image/avif'))->toFilePointer() - ); - } - - $this->manipulator = new Encode(); - } - - public function tearDown(): void - { - \Mockery::close(); - } - - public function testCreateInstance() - { - $this->assertInstanceOf('League\Glide\Manipulators\Encode', $this->manipulator); - } - - public function testRun() - { - $this->assertSame('image/jpeg', $this->getMime($this->manipulator->setParams(['fm' => 'jpg'])->run($this->jpg))); - $this->assertSame('image/jpeg', $this->getMime($this->manipulator->setParams(['fm' => 'jpg'])->run($this->png))); - $this->assertSame('image/jpeg', $this->getMime($this->manipulator->setParams(['fm' => 'jpg'])->run($this->gif))); - $this->assertSame('image/jpeg', $this->getMime($this->manipulator->setParams(['fm' => 'pjpg'])->run($this->jpg))); - $this->assertSame('image/jpeg', $this->getMime($this->manipulator->setParams(['fm' => 'pjpg'])->run($this->png))); - $this->assertSame('image/jpeg', $this->getMime($this->manipulator->setParams(['fm' => 'pjpg'])->run($this->gif))); - $this->assertSame('image/png', $this->getMime($this->manipulator->setParams(['fm' => 'png'])->run($this->jpg))); - $this->assertSame('image/png', $this->getMime($this->manipulator->setParams(['fm' => 'png'])->run($this->png))); - $this->assertSame('image/png', $this->getMime($this->manipulator->setParams(['fm' => 'png'])->run($this->gif))); - $this->assertSame('image/gif', $this->getMime($this->manipulator->setParams(['fm' => 'gif'])->run($this->jpg))); - $this->assertSame('image/gif', $this->getMime($this->manipulator->setParams(['fm' => 'gif'])->run($this->png))); - $this->assertSame('image/gif', $this->getMime($this->manipulator->setParams(['fm' => 'gif'])->run($this->gif))); - - if (function_exists('imagecreatefromwebp')) { - $this->assertSame('image/jpeg', $this->getMime($this->manipulator->setParams(['fm' => 'jpg'])->run($this->webp))); - $this->assertSame('image/jpeg', $this->getMime($this->manipulator->setParams(['fm' => 'pjpg'])->run($this->webp))); - $this->assertSame('image/png', $this->getMime($this->manipulator->setParams(['fm' => 'png'])->run($this->webp))); - $this->assertSame('image/gif', $this->getMime($this->manipulator->setParams(['fm' => 'gif'])->run($this->webp))); - $this->assertSame('image/webp', $this->getMime($this->manipulator->setParams(['fm' => 'webp'])->run($this->jpg))); - $this->assertSame('image/webp', $this->getMime($this->manipulator->setParams(['fm' => 'webp'])->run($this->png))); - $this->assertSame('image/webp', $this->getMime($this->manipulator->setParams(['fm' => 'webp'])->run($this->gif))); - $this->assertSame('image/webp', $this->getMime($this->manipulator->setParams(['fm' => 'webp'])->run($this->webp))); - } - if (function_exists('imagecreatefromavif')) { - $this->assertSame('image/jpeg', $this->getMime($this->manipulator->setParams(['fm' => 'jpg'])->run($this->avif))); - $this->assertSame('image/jpeg', $this->getMime($this->manipulator->setParams(['fm' => 'pjpg'])->run($this->avif))); - $this->assertSame('image/png', $this->getMime($this->manipulator->setParams(['fm' => 'png'])->run($this->avif))); - $this->assertSame('image/gif', $this->getMime($this->manipulator->setParams(['fm' => 'gif'])->run($this->avif))); - $this->assertSame('image/avif', $this->getMime($this->manipulator->setParams(['fm' => 'avif'])->run($this->jpg))); - $this->assertSame('image/avif', $this->getMime($this->manipulator->setParams(['fm' => 'avif'])->run($this->png))); - $this->assertSame('image/avif', $this->getMime($this->manipulator->setParams(['fm' => 'avif'])->run($this->gif))); - $this->assertSame('image/avif', $this->getMime($this->manipulator->setParams(['fm' => 'avif'])->run($this->avif))); - } - - if (function_exists('imagecreatefromwebp') && function_exists('imagecreatefromavif')) { - $this->assertSame('image/webp', $this->getMime($this->manipulator->setParams(['fm' => 'webp'])->run($this->avif))); - $this->assertSame('image/avif', $this->getMime($this->manipulator->setParams(['fm' => 'avif'])->run($this->webp))); - } - } - - public function testGetFormat() - { - $image = \Mockery::mock(ImageInterface::class, function ($mock) { - $mock->shouldReceive('origin')->andReturn(\Mockery::mock('Intervention\Image\Origin', ['mediaType' => 'image/jpeg']))->once(); - $mock->shouldReceive('origin')->andReturn(\Mockery::mock('Intervention\Image\Origin', ['mediaType' => 'image/png']))->once(); - $mock->shouldReceive('origin')->andReturn(\Mockery::mock('Intervention\Image\Origin', ['mediaType' => 'image/gif']))->once(); - $mock->shouldReceive('origin')->andReturn(\Mockery::mock('Intervention\Image\Origin', ['mediaType' => 'image/bmp']))->once(); - $mock->shouldReceive('origin')->andReturn(\Mockery::mock('Intervention\Image\Origin', ['mediaType' => 'image/jpeg']))->twice(); - - if (function_exists('imagecreatefromwebp')) { - $mock->shouldReceive('origin')->andReturn(\Mockery::mock('Intervention\Image\Origin', ['mediaType' => 'image/webp']))->once(); - } - - if (function_exists('imagecreatefromavif')) { - $mock->shouldReceive('origin')->andReturn(\Mockery::mock('Intervention\Image\Origin', ['mediaType' => 'image/avif']))->once(); - } - }); - - $this->assertSame('jpg', $this->manipulator->setParams(['fm' => 'jpg'])->getFormat($image)); - $this->assertSame('png', $this->manipulator->setParams(['fm' => 'png'])->getFormat($image)); - $this->assertSame('gif', $this->manipulator->setParams(['fm' => 'gif'])->getFormat($image)); - $this->assertSame('jpg', $this->manipulator->setParams(['fm' => null])->getFormat($image)); - $this->assertSame('png', $this->manipulator->setParams(['fm' => null])->getFormat($image)); - $this->assertSame('gif', $this->manipulator->setParams(['fm' => null])->getFormat($image)); - $this->assertSame('jpg', $this->manipulator->setParams(['fm' => null])->getFormat($image)); - - $this->assertSame('jpg', $this->manipulator->setParams(['fm' => ''])->getFormat($image)); - $this->assertSame('jpg', $this->manipulator->setParams(['fm' => 'invalid'])->getFormat($image)); - - if (function_exists('imagecreatefromwebp')) { - $this->assertSame('webp', $this->manipulator->setParams(['fm' => null])->getFormat($image)); - } - - if (function_exists('imagecreatefromavif')) { - $this->assertSame('avif', $this->manipulator->setParams(['fm' => null])->getFormat($image)); - } - } - - public function testGetQuality() - { - $this->assertSame(100, $this->manipulator->setParams(['q' => '100'])->getQuality()); - $this->assertSame(100, $this->manipulator->setParams(['q' => 100])->getQuality()); - $this->assertSame(90, $this->manipulator->setParams(['q' => null])->getQuality()); - $this->assertSame(90, $this->manipulator->setParams(['q' => 'a'])->getQuality()); - $this->assertSame(50, $this->manipulator->setParams(['q' => '50.50'])->getQuality()); - $this->assertSame(90, $this->manipulator->setParams(['q' => '-1'])->getQuality()); - $this->assertSame(90, $this->manipulator->setParams(['q' => '101'])->getQuality()); - } - - /** - * Test functions that require the imagick extension. - * - * @return void - */ - public function testWithImagick() - { - if (!extension_loaded('imagick')) { - $this->markTestSkipped( - 'The imagick extension is not available.' - ); - } - $manager = ImageManager::imagick(); - // These need to be recreated with the imagick driver selected in the manager - $this->jpg = $manager->read($manager->create(100, 100)->encode(new MediaTypeEncoder('image/jpeg'))->toFilePointer()); - $this->png = $manager->read($manager->create(100, 100)->encode(new MediaTypeEncoder('image/png'))->toFilePointer()); - $this->gif = $manager->read($manager->create(100, 100)->encode(new MediaTypeEncoder('image/gif'))->toFilePointer()); - $this->heic = $manager->read($manager->create(100, 100)->encode(new MediaTypeEncoder('image/heic'))->toFilePointer()); - $this->tif = $manager->read($manager->create(100, 100)->encode(new MediaTypeEncoder('image/tiff'))->toFilePointer()); - - $this->assertSame('image/tiff', $this->getMime($this->manipulator->setParams(['fm' => 'tiff'])->run($this->jpg))); - $this->assertSame('image/tiff', $this->getMime($this->manipulator->setParams(['fm' => 'tiff'])->run($this->png))); - $this->assertSame('image/tiff', $this->getMime($this->manipulator->setParams(['fm' => 'tiff'])->run($this->gif))); - $this->assertSame('image/tiff', $this->getMime($this->manipulator->setParams(['fm' => 'tiff'])->run($this->heic))); - } - - public function testSupportedFormats() - { - $expected = [ - 'avif' => 'image/avif', - 'gif' => 'image/gif', - 'jpg' => 'image/jpeg', - 'pjpg' => 'image/jpeg', - 'png' => 'image/png', - 'webp' => 'image/webp', - 'tiff' => 'image/tiff', - 'heic' => 'image/heic', - ]; - - $this->assertSame($expected, Encode::supportedFormats()); - } - - protected function getMime(EncodedImage $image) - { - return $image->mediaType(); - } -} From d2dd5f26b7542e108b507bfbf1c6ac833f6ea7df Mon Sep 17 00:00:00 2001 From: Konnng Date: Mon, 3 Jun 2024 12:29:53 -0300 Subject: [PATCH 14/20] test: fix test typings --- tests/Api/EncodeTest.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tests/Api/EncodeTest.php b/tests/Api/EncodeTest.php index 21f8d8b..0ece729 100644 --- a/tests/Api/EncodeTest.php +++ b/tests/Api/EncodeTest.php @@ -146,7 +146,13 @@ public function testRun(): void public function testGetFormat(): void { - $image = \Mockery::mock(ImageInterface::class, function (Mock $mock) { + /** + * @psalm-suppress MissingClosureParamType + */ + $image = \Mockery::mock(ImageInterface::class, function ($mock) { + /* + * @var Mock $mock + */ $this->assertMediaType($mock, 'image/jpeg')->once(); $this->assertMediaType($mock, 'image/png')->once(); $this->assertMediaType($mock, 'image/gif')->once(); @@ -240,12 +246,16 @@ protected function getMime(EncodedImageInterface $image): string /** * Creates an assertion to check media type. - */ - /** + * + * @param Mock $mock + * @param string $mediaType * @psalm-suppress MoreSpecificReturnType */ - protected function assertMediaType(Mock $mock, string $mediaType): Mockery\Expectation + protected function assertMediaType($mock, $mediaType): Mockery\CompositeExpectation { + /* + * @var Mock $mock + */ /** * @psalm-suppress LessSpecificReturnStatement, UndefinedMagicMethod */ From db231238046552bb1602248c9429cc39cd38e290 Mon Sep 17 00:00:00 2001 From: Konnng Date: Mon, 3 Jun 2024 12:34:26 -0300 Subject: [PATCH 15/20] test: fix lint issue --- tests/Api/EncodeTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Api/EncodeTest.php b/tests/Api/EncodeTest.php index 0ece729..6de02c4 100644 --- a/tests/Api/EncodeTest.php +++ b/tests/Api/EncodeTest.php @@ -249,6 +249,7 @@ protected function getMime(EncodedImageInterface $image): string * * @param Mock $mock * @param string $mediaType + * * @psalm-suppress MoreSpecificReturnType */ protected function assertMediaType($mock, $mediaType): Mockery\CompositeExpectation From c4211b1df9aee7e10b0f075c3c4a8336384aaf9c Mon Sep 17 00:00:00 2001 From: Konnng Date: Mon, 3 Jun 2024 15:04:02 -0300 Subject: [PATCH 16/20] chore: docs entry added --- docs/3.0/api/progressive-interlaced.md | 27 ++++++++++++++++++++++++++ docs/_data/menu.yml | 1 + 2 files changed, 28 insertions(+) create mode 100644 docs/3.0/api/progressive-interlaced.md diff --git a/docs/3.0/api/progressive-interlaced.md b/docs/3.0/api/progressive-interlaced.md new file mode 100644 index 0000000..e64c9a1 --- /dev/null +++ b/docs/3.0/api/progressive-interlaced.md @@ -0,0 +1,27 @@ +--- +layout: default +title: Progressive & Interlaced +--- + +## Progressive & Interlaced Images + + +## Interlace `interlace` + +The `interlaced` parameter controls whether an image is rendered in a progressive or interlaced format. This feature enhances the loading experience of images, making them appear gradually as they are downloaded, which can improve the user experience on slower connections. + +> Caution: for GIF/PNG, it can generate a sligher larger file size. + +### Supported Formats + +- **JPG**: The `Interlaced` parameter applies a progressive scan to JPG images. +- **PNG** and **GIF**: The `Interlaced` parameter enables interlacing for GIF/PNG images. + +> Note: Case `ext` is set to `.pjpg`, it will automatically generate a progressive JPG image, regardless of the `interlaced` parameter. + +~~~ html + + +~~~ + +[![© Photo Joel Reynolds](https://glide.herokuapp.com/1.0/kayaks.jpg?interlace=1)](https://glide.herokuapp.com/1.0/kayaks.jpg?h=500&flip=v) diff --git a/docs/_data/menu.yml b/docs/_data/menu.yml index 5aced23..9a7065c 100644 --- a/docs/_data/menu.yml +++ b/docs/_data/menu.yml @@ -25,6 +25,7 @@ Background: '/3.0/api/background/' Border: '/3.0/api/border/' Encode: '/3.0/api/encode/' + Progressive & Interlaced: '/3.0/api/progressive-interlaced/' '2.0': Getting Started: Introduction: '/' From ecb356e811650f5e4bd2c93eecb92fe2f50343c9 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sun, 27 Oct 2024 11:19:14 +0530 Subject: [PATCH 17/20] Fix docs --- docs/3.0/api/progressive-interlaced.md | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/docs/3.0/api/progressive-interlaced.md b/docs/3.0/api/progressive-interlaced.md index e64c9a1..e946a0f 100644 --- a/docs/3.0/api/progressive-interlaced.md +++ b/docs/3.0/api/progressive-interlaced.md @@ -5,23 +5,22 @@ title: Progressive & Interlaced ## Progressive & Interlaced Images - ## Interlace `interlace` -The `interlaced` parameter controls whether an image is rendered in a progressive or interlaced format. This feature enhances the loading experience of images, making them appear gradually as they are downloaded, which can improve the user experience on slower connections. +The `interlace` parameter controls whether an image is rendered in a progressive or interlaced format. This feature enhances the loading experience of images, making them appear gradually as they are downloaded, which can improve the user experience on slower connections. -> Caution: for GIF/PNG, it can generate a sligher larger file size. +> Caution: For GIF/PNG, it can generate a slightly larger file size. ### Supported Formats -- **JPG**: The `Interlaced` parameter applies a progressive scan to JPG images. -- **PNG** and **GIF**: The `Interlaced` parameter enables interlacing for GIF/PNG images. +- **JPG**: The `onterlace` parameter applies a progressive scan to JPG images. +- **PNG** and **GIF**: The `interlace` parameter enables interlacing for GIF/PNG images. -> Note: Case `ext` is set to `.pjpg`, it will automatically generate a progressive JPG image, regardless of the `interlaced` parameter. +> Note: When `ext` is set to `.pjpg`, it will automatically generate a progressive JPG image, regardless of the `interlace` parameter. ~~~ html - - + + ~~~ -[![© Photo Joel Reynolds](https://glide.herokuapp.com/1.0/kayaks.jpg?interlace=1)](https://glide.herokuapp.com/1.0/kayaks.jpg?h=500&flip=v) +[![© Photo Joel Reynolds](https://glide.herokuapp.com/1.0/kayaks.jpg?interlace=1)](https://glide.herokuapp.com/1.0/kayaks.jpg?interlace=1) From 6313aea35d3d241d6303df880760bb1146b320e4 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sun, 27 Oct 2024 11:30:12 +0530 Subject: [PATCH 18/20] Add missing property types --- src/Api/Encode.php | 2 +- tests/Api/EncodeTest.php | 57 ++++++---------------------------------- 2 files changed, 9 insertions(+), 50 deletions(-) diff --git a/src/Api/Encode.php b/src/Api/Encode.php index 735c253..de723d0 100644 --- a/src/Api/Encode.php +++ b/src/Api/Encode.php @@ -13,7 +13,7 @@ class Encode /** * The manipulation params. */ - private $params; + protected array $params; /** * Class constructor. diff --git a/tests/Api/EncodeTest.php b/tests/Api/EncodeTest.php index 6de02c4..09f58fc 100644 --- a/tests/Api/EncodeTest.php +++ b/tests/Api/EncodeTest.php @@ -7,59 +7,18 @@ use Intervention\Image\Interfaces\EncodedImageInterface; use Intervention\Image\Interfaces\ImageInterface; use Mockery; -use Mockery\Mock; use PHPUnit\Framework\TestCase; class EncodeTest extends TestCase { - /** - * @var Encode - * - * @psalm-suppress PropertyNotSetInConstructor - */ - private $encoder; - /** - * @var ImageInterface - * - * @psalm-suppress PropertyNotSetInConstructor - */ - private $jpg; - /** - * @var ImageInterface - * - * @psalm-suppress PropertyNotSetInConstructor - */ - private $png; - /** - * @var ImageInterface - * - * @psalm-suppress PropertyNotSetInConstructor - */ - private $gif; - /** - * @var ImageInterface - * - * @psalm-suppress PropertyNotSetInConstructor - */ - private $tif; - /** - * @var ImageInterface - * - * @psalm-suppress PropertyNotSetInConstructor - */ - private $webp; - /** - * @var ImageInterface - * - * @psalm-suppress PropertyNotSetInConstructor - */ - private $avif; - /** - * @var ImageInterface - * - * @psalm-suppress PropertyNotSetInConstructor - */ - private $heic; + private Encode $encoder; + private ImageInterface $jpg; + private ImageInterface $png; + private ImageInterface $gif; + private ImageInterface $tif; + private ImageInterface $webp; + private ImageInterface $avif; + private ImageInterface $heic; public function setUp(): void { From d62247637a199579ca47b79838e777ebd059a794 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sun, 27 Oct 2024 12:21:20 +0530 Subject: [PATCH 19/20] Rename class Encode to Encoder. --- src/Api/Api.php | 2 +- src/Api/{Encode.php => Encoder.php} | 2 +- tests/Api/{EncodeTest.php => EncoderTest.php} | 10 +++++----- 3 files changed, 7 insertions(+), 7 deletions(-) rename src/Api/{Encode.php => Encoder.php} (99%) rename tests/Api/{EncodeTest.php => EncoderTest.php} (97%) diff --git a/src/Api/Api.php b/src/Api/Api.php index 9df1f24..45be6f9 100644 --- a/src/Api/Api.php +++ b/src/Api/Api.php @@ -108,7 +108,7 @@ public function run(string $source, array $params): string */ public function encode(ImageInterface $image, array $params): string { - $encoder = new Encode($params); + $encoder = new Encoder($params); $encoded = $encoder->run($image); return $encoded->toString(); diff --git a/src/Api/Encode.php b/src/Api/Encoder.php similarity index 99% rename from src/Api/Encode.php rename to src/Api/Encoder.php index de723d0..bef473b 100644 --- a/src/Api/Encode.php +++ b/src/Api/Encoder.php @@ -8,7 +8,7 @@ /** * Encoder Api class to convert a given image to a specific format. */ -class Encode +class Encoder { /** * The manipulation params. diff --git a/tests/Api/EncodeTest.php b/tests/Api/EncoderTest.php similarity index 97% rename from tests/Api/EncodeTest.php rename to tests/Api/EncoderTest.php index 09f58fc..b3bae46 100644 --- a/tests/Api/EncodeTest.php +++ b/tests/Api/EncoderTest.php @@ -9,9 +9,9 @@ use Mockery; use PHPUnit\Framework\TestCase; -class EncodeTest extends TestCase +class EncoderTest extends TestCase { - private Encode $encoder; + private Encoder $encoder; private ImageInterface $jpg; private ImageInterface $png; private ImageInterface $gif; @@ -45,7 +45,7 @@ public function setUp(): void ); } - $this->encoder = new Encode(); + $this->encoder = new Encoder(); } public function tearDown(): void @@ -58,7 +58,7 @@ public function testCreateInstance(): void /** * @psalm-suppress ArgumentTypeCoercion */ - $this->assertInstanceOf('League\Glide\Api\Encode', $this->encoder); + $this->assertInstanceOf(Encoder::class, $this->encoder); } public function testRun(): void @@ -195,7 +195,7 @@ public function testSupportedFormats(): void 'heic' => 'image/heic', ]; - $this->assertSame($expected, Encode::supportedFormats()); + $this->assertSame($expected, Encoder::supportedFormats()); } protected function getMime(EncodedImageInterface $image): string From 4f2cf2437a9358eb5c3d8d196c10a61e1da5513a Mon Sep 17 00:00:00 2001 From: ADmad Date: Sun, 27 Oct 2024 12:56:56 +0530 Subject: [PATCH 20/20] Fix docs --- docs/3.0/config/setup.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/3.0/config/setup.md b/docs/3.0/config/setup.md index f52e212..e94728a 100644 --- a/docs/3.0/config/setup.md +++ b/docs/3.0/config/setup.md @@ -77,7 +77,6 @@ $manipulators = [ new League\Glide\Manipulators\Watermark($watermarks), new League\Glide\Manipulators\Background(), new League\Glide\Manipulators\Border(), - new League\Glide\Manipulators\Encode(), ]; // Set API