From ea13f2e0bb7c30329712d2f90a6c4dcdd12d628d Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sat, 23 Dec 2023 10:10:26 +0100 Subject: [PATCH] Add quality parameter to driver ambiguous decoders --- src/Encoders/AutoEncoder.php | 10 ++++++++++ src/Encoders/FileExtensionEncoder.php | 11 ++++++----- src/Encoders/FilePathEncoder.php | 3 ++- src/Encoders/MediaTypeEncoder.php | 11 ++++++----- src/Image.php | 16 ++++++++-------- src/Interfaces/ImageInterface.php | 8 ++++---- 6 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/Encoders/AutoEncoder.php b/src/Encoders/AutoEncoder.php index 614b2e6a..8ef68563 100644 --- a/src/Encoders/AutoEncoder.php +++ b/src/Encoders/AutoEncoder.php @@ -7,6 +7,16 @@ use Intervention\Image\Interfaces\ImageInterface; class AutoEncoder extends MediaTypeEncoder { + /** + * Create new encoder instance + * + * @param int $quality + * @return void + */ + public function __construct(protected int $quality = 75) + { + } + /** * {@inheritdoc} * diff --git a/src/Encoders/FileExtensionEncoder.php b/src/Encoders/FileExtensionEncoder.php index e22e34c6..ffc50571 100644 --- a/src/Encoders/FileExtensionEncoder.php +++ b/src/Encoders/FileExtensionEncoder.php @@ -13,9 +13,10 @@ class FileExtensionEncoder extends AutoEncoder * Create new encoder instance to encode to format of given file extension * * @param null|string $extension + * @param int $quality * @return void */ - public function __construct(protected ?string $extension = null) + public function __construct(protected ?string $extension = null, protected int $quality = 75) { } @@ -47,13 +48,13 @@ class FileExtensionEncoder extends AutoEncoder } return match ($extension) { - 'webp' => new WebpEncoder(), - 'avif' => new AvifEncoder(), - 'jpeg', 'jpg' => new JpegEncoder(), + 'webp' => new WebpEncoder($this->quality), + 'avif' => new AvifEncoder($this->quality), + 'jpeg', 'jpg' => new JpegEncoder($this->quality), 'bmp' => new BmpEncoder(), 'gif' => new GifEncoder(), 'png' => new PngEncoder(), - 'tiff', 'tif' => new TiffEncoder(), + 'tiff', 'tif' => new TiffEncoder($this->quality), default => throw new EncoderException('No encoder found for file extension (' . $extension . ').'), }; } diff --git a/src/Encoders/FilePathEncoder.php b/src/Encoders/FilePathEncoder.php index f0f1dc62..d93892aa 100644 --- a/src/Encoders/FilePathEncoder.php +++ b/src/Encoders/FilePathEncoder.php @@ -11,9 +11,10 @@ class FilePathEncoder extends FileExtensionEncoder * Create new encoder instance to encode to format of file extension in given path * * @param null|string $path + * @param int $quality * @return void */ - public function __construct(protected ?string $path = null) + public function __construct(protected ?string $path = null, protected int $quality = 75) { } diff --git a/src/Encoders/MediaTypeEncoder.php b/src/Encoders/MediaTypeEncoder.php index 3a05b0d7..c8fab76a 100644 --- a/src/Encoders/MediaTypeEncoder.php +++ b/src/Encoders/MediaTypeEncoder.php @@ -13,9 +13,10 @@ class MediaTypeEncoder implements EncoderInterface * Create new encoder instance to encode given media (mime) type * * @param null|string $type + * @param int $quality * @return void */ - public function __construct(protected ?string $type = null) + public function __construct(protected ?string $type = null, protected int $quality = 75) { } @@ -43,13 +44,13 @@ class MediaTypeEncoder implements EncoderInterface protected function encoderByMediaType(string $type): EncoderInterface { return match ($type) { - 'image/webp' => new WebpEncoder(), - 'image/avif' => new AvifEncoder(), - 'image/jpeg' => new JpegEncoder(), + 'image/webp' => new WebpEncoder($this->quality), + 'image/avif' => new AvifEncoder($this->quality), + 'image/jpeg' => new JpegEncoder($this->quality), 'image/bmp' => new BmpEncoder(), 'image/gif' => new GifEncoder(), 'image/png' => new PngEncoder(), - 'image/tiff' => new TiffEncoder(), + 'image/tiff' => new TiffEncoder($this->quality), default => throw new EncoderException('No encoder found for media type (' . $type . ').'), }; } diff --git a/src/Image.php b/src/Image.php index c7f09f77..d01415e0 100644 --- a/src/Image.php +++ b/src/Image.php @@ -256,11 +256,11 @@ final class Image implements ImageInterface, Countable * * @see ImageInterface::save() */ - public function save(?string $path = null): ImageInterface + public function save(?string $path = null, int $quality = 75): ImageInterface { $path = is_null($path) ? $this->origin()->filePath() : $path; - $this->encodeByPath($path)->save($path); + $this->encodeByPath($path, $quality)->save($path); return $this; } @@ -766,9 +766,9 @@ final class Image implements ImageInterface, Countable * * @see ImageInterface::encodeByMediaType() */ - public function encodeByMediaType(?string $type = null): EncodedImageInterface + public function encodeByMediaType(?string $type = null, int $quality = 75): EncodedImageInterface { - return $this->encode(new MediaTypeEncoder($type)); + return $this->encode(new MediaTypeEncoder($type, $quality)); } /** @@ -776,9 +776,9 @@ final class Image implements ImageInterface, Countable * * @see ImageInterface::encodeByExtension() */ - public function encodeByExtension(?string $extension = null): EncodedImageInterface + public function encodeByExtension(?string $extension = null, int $quality = 75): EncodedImageInterface { - return $this->encode(new FileExtensionEncoder($extension)); + return $this->encode(new FileExtensionEncoder($extension, $quality)); } /** @@ -786,9 +786,9 @@ final class Image implements ImageInterface, Countable * * @see ImageInterface::encodeByPath() */ - public function encodeByPath(?string $path = null): EncodedImageInterface + public function encodeByPath(?string $path = null, int $quality = 75): EncodedImageInterface { - return $this->encode(new FilePathEncoder($path)); + return $this->encode(new FilePathEncoder($path, $quality)); } /** diff --git a/src/Interfaces/ImageInterface.php b/src/Interfaces/ImageInterface.php index bd5affda..8621db19 100644 --- a/src/Interfaces/ImageInterface.php +++ b/src/Interfaces/ImageInterface.php @@ -74,7 +74,7 @@ interface ImageInterface extends IteratorAggregate, Countable * @param null|string $path * @return ImageInterface */ - public function save(?string $path = null): ImageInterface; + public function save(?string $path = null, int $quality = 75): ImageInterface; /** * Apply given modifier to current image @@ -573,7 +573,7 @@ interface ImageInterface extends IteratorAggregate, Countable * @param null|string $type * @return EncodedImageInterface */ - public function encodeByMediaType(?string $type = null): EncodedImageInterface; + public function encodeByMediaType(?string $type = null, int $quality = 75): EncodedImageInterface; /** * Encode the image into the format represented by the given extension. If no @@ -583,7 +583,7 @@ interface ImageInterface extends IteratorAggregate, Countable * @param null|string $extension * @return EncodedImageInterface */ - public function encodeByExtension(?string $extension = null): EncodedImageInterface; + public function encodeByExtension(?string $extension = null, int $quality = 75): EncodedImageInterface; /** * Encode the image into the format represented by the given extension of @@ -593,7 +593,7 @@ interface ImageInterface extends IteratorAggregate, Countable * @param null|string $path * @return EncodedImageInterface */ - public function encodeByPath(?string $path = null): EncodedImageInterface; + public function encodeByPath(?string $path = null, int $quality = 75): EncodedImageInterface; /** * Encode image to JPEG format