diff --git a/src/Encoders/AbstractEncoder.php b/src/Encoders/AbstractEncoder.php index 6bf0d047..72165f97 100644 --- a/src/Encoders/AbstractEncoder.php +++ b/src/Encoders/AbstractEncoder.php @@ -5,10 +5,36 @@ namespace Intervention\Image\Encoders; use Intervention\Image\Interfaces\EncodedImageInterface; use Intervention\Image\Interfaces\EncoderInterface; use Intervention\Image\Interfaces\ImageInterface; -use Intervention\Image\Interfaces\SpecializableInterface; -abstract class AbstractEncoder implements EncoderInterface, SpecializableInterface +abstract class AbstractEncoder implements EncoderInterface { + public const DEFAULT_QUALITY = 75; + public const DEFAULT_COMPRESSION = 0; + public const DEFAULT_BIT_DEPTH = 8; + + public int $quality = self::DEFAULT_QUALITY; + public int $compression = self::DEFAULT_COMPRESSION; + public int $bitDepth = self::DEFAULT_BIT_DEPTH; + + /** + * Create new encoder instance + * + * @param mixed $options + * @return void + */ + public function __construct(mixed ...$options) + { + if (is_array($options) && array_is_list($options)) { + $this->quality = $options[0] ?? self::DEFAULT_QUALITY; + $this->compression = $options[1] ?? self::DEFAULT_COMPRESSION; + $this->bitDepth = $options[2] ?? self::DEFAULT_BIT_DEPTH; + } else { + $this->quality = $options['quality'] ?? self::DEFAULT_QUALITY; + $this->compression = $options['compression'] ?? self::DEFAULT_COMPRESSION; + $this->bitDepth = $options['bitDepth'] ?? self::DEFAULT_BIT_DEPTH; + } + } + /** * {@inheritdoc} * diff --git a/src/Encoders/AutoEncoder.php b/src/Encoders/AutoEncoder.php index 8ef68563..614b2e6a 100644 --- a/src/Encoders/AutoEncoder.php +++ b/src/Encoders/AutoEncoder.php @@ -7,16 +7,6 @@ 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/AvifEncoder.php b/src/Encoders/AvifEncoder.php index dfa64854..e9e91c48 100644 --- a/src/Encoders/AvifEncoder.php +++ b/src/Encoders/AvifEncoder.php @@ -2,9 +2,8 @@ namespace Intervention\Image\Encoders; -class AvifEncoder extends AbstractEncoder +use Intervention\Image\Interfaces\SpecializableInterface; + +class AvifEncoder extends AbstractEncoder implements SpecializableInterface { - public function __construct(public int $quality = 75) - { - } } diff --git a/src/Encoders/BmpEncoder.php b/src/Encoders/BmpEncoder.php index 4cf0c6e6..787df9af 100644 --- a/src/Encoders/BmpEncoder.php +++ b/src/Encoders/BmpEncoder.php @@ -2,6 +2,8 @@ namespace Intervention\Image\Encoders; -class BmpEncoder extends AbstractEncoder +use Intervention\Image\Interfaces\SpecializableInterface; + +class BmpEncoder extends AbstractEncoder implements SpecializableInterface { } diff --git a/src/Encoders/FileExtensionEncoder.php b/src/Encoders/FileExtensionEncoder.php index 5637792e..58a67662 100644 --- a/src/Encoders/FileExtensionEncoder.php +++ b/src/Encoders/FileExtensionEncoder.php @@ -13,11 +13,12 @@ class FileExtensionEncoder extends AutoEncoder * Create new encoder instance to encode to format of given file extension * * @param null|string $extension - * @param int $quality + * @param mixed $options * @return void */ - public function __construct(protected ?string $extension = null, protected int $quality = 75) + public function __construct(protected ?string $extension = null, mixed ...$options) { + parent::__construct(null, ...$options); } /** diff --git a/src/Encoders/FilePathEncoder.php b/src/Encoders/FilePathEncoder.php index d93892aa..572a0dd4 100644 --- a/src/Encoders/FilePathEncoder.php +++ b/src/Encoders/FilePathEncoder.php @@ -11,11 +11,15 @@ 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 + * @param mixed $options * @return void */ - public function __construct(protected ?string $path = null, protected int $quality = 75) + public function __construct(protected ?string $path = null, mixed ...$options) { + parent::__construct( + is_null($path) ? $path : pathinfo($path, PATHINFO_EXTENSION), + ...$options + ); } /** diff --git a/src/Encoders/GifEncoder.php b/src/Encoders/GifEncoder.php index 5fd836b4..0f566f3e 100644 --- a/src/Encoders/GifEncoder.php +++ b/src/Encoders/GifEncoder.php @@ -2,6 +2,8 @@ namespace Intervention\Image\Encoders; -class GifEncoder extends AbstractEncoder +use Intervention\Image\Interfaces\SpecializableInterface; + +class GifEncoder extends AbstractEncoder implements SpecializableInterface { } diff --git a/src/Encoders/Jpeg2000Encoder.php b/src/Encoders/Jpeg2000Encoder.php index 0eb60a54..a83b61b8 100644 --- a/src/Encoders/Jpeg2000Encoder.php +++ b/src/Encoders/Jpeg2000Encoder.php @@ -2,9 +2,8 @@ namespace Intervention\Image\Encoders; -class Jpeg2000Encoder extends AbstractEncoder +use Intervention\Image\Interfaces\SpecializableInterface; + +class Jpeg2000Encoder extends AbstractEncoder implements SpecializableInterface { - public function __construct(public int $quality = 75) - { - } } diff --git a/src/Encoders/JpegEncoder.php b/src/Encoders/JpegEncoder.php index 3a63de5c..93a22659 100644 --- a/src/Encoders/JpegEncoder.php +++ b/src/Encoders/JpegEncoder.php @@ -2,9 +2,8 @@ namespace Intervention\Image\Encoders; -class JpegEncoder extends AbstractEncoder +use Intervention\Image\Interfaces\SpecializableInterface; + +class JpegEncoder extends AbstractEncoder implements SpecializableInterface { - public function __construct(public int $quality = 75) - { - } } diff --git a/src/Encoders/MediaTypeEncoder.php b/src/Encoders/MediaTypeEncoder.php index 1fb6751b..be9c6027 100644 --- a/src/Encoders/MediaTypeEncoder.php +++ b/src/Encoders/MediaTypeEncoder.php @@ -7,17 +7,11 @@ use Intervention\Image\Interfaces\EncodedImageInterface; use Intervention\Image\Interfaces\EncoderInterface; use Intervention\Image\Interfaces\ImageInterface; -class MediaTypeEncoder implements EncoderInterface +class MediaTypeEncoder extends AbstractEncoder 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, protected int $quality = 75) + public function __construct(protected ?string $type = null, ...$options) { + parent::__construct(...$options); } /** @@ -44,14 +38,14 @@ class MediaTypeEncoder implements EncoderInterface protected function encoderByMediaType(string $type): EncoderInterface { return match (strtolower($type)) { - 'image/webp' => new WebpEncoder($this->quality), - 'image/avif' => new AvifEncoder($this->quality), - 'image/jpeg' => new JpegEncoder($this->quality), + 'image/webp' => new WebpEncoder(quality: $this->quality), + 'image/avif' => new AvifEncoder(quality: $this->quality), + 'image/jpeg' => new JpegEncoder(quality: $this->quality), 'image/bmp' => new BmpEncoder(), 'image/gif' => new GifEncoder(), 'image/png' => new PngEncoder(), - 'image/tiff' => new TiffEncoder($this->quality), - 'image/jp2', 'image/jpx', 'image/jpm' => new Jpeg2000Encoder($this->quality), + 'image/tiff' => new TiffEncoder(quality: $this->quality), + 'image/jp2', 'image/jpx', 'image/jpm' => new Jpeg2000Encoder(quality: $this->quality), default => throw new EncoderException('No encoder found for media type (' . $type . ').'), }; } diff --git a/src/Encoders/PngEncoder.php b/src/Encoders/PngEncoder.php index 1b8660d1..507488f2 100644 --- a/src/Encoders/PngEncoder.php +++ b/src/Encoders/PngEncoder.php @@ -2,6 +2,8 @@ namespace Intervention\Image\Encoders; -class PngEncoder extends AbstractEncoder +use Intervention\Image\Interfaces\SpecializableInterface; + +class PngEncoder extends AbstractEncoder implements SpecializableInterface { } diff --git a/src/Encoders/TiffEncoder.php b/src/Encoders/TiffEncoder.php index 7bab4397..89862fd3 100644 --- a/src/Encoders/TiffEncoder.php +++ b/src/Encoders/TiffEncoder.php @@ -2,9 +2,8 @@ namespace Intervention\Image\Encoders; -class TiffEncoder extends AbstractEncoder +use Intervention\Image\Interfaces\SpecializableInterface; + +class TiffEncoder extends AbstractEncoder implements SpecializableInterface { - public function __construct(public int $quality = 75) - { - } } diff --git a/src/Encoders/WebpEncoder.php b/src/Encoders/WebpEncoder.php index 0c63e630..a24af3dd 100644 --- a/src/Encoders/WebpEncoder.php +++ b/src/Encoders/WebpEncoder.php @@ -2,9 +2,8 @@ namespace Intervention\Image\Encoders; -class WebpEncoder extends AbstractEncoder +use Intervention\Image\Interfaces\SpecializableInterface; + +class WebpEncoder extends AbstractEncoder implements SpecializableInterface { - public function __construct(public int $quality = 75) - { - } } diff --git a/src/Image.php b/src/Image.php index 9237c0cd..1be15e9d 100644 --- a/src/Image.php +++ b/src/Image.php @@ -257,7 +257,7 @@ final class Image implements ImageInterface * * @see ImageInterface::save() */ - public function save(?string $path = null, int $quality = 75): ImageInterface + public function save(?string $path = null, ...$options): ImageInterface { $path = is_null($path) ? $this->origin()->filePath() : $path; @@ -267,10 +267,10 @@ final class Image implements ImageInterface try { // try to determine encoding format by file extension of the path - $encoded = $this->encodeByPath($path, $quality); + $encoded = $this->encodeByPath($path, ...$options); } catch (EncoderException) { // fallback to encoding format by media type - $encoded = $this->encodeByMediaType(quality: $quality); + $encoded = $this->encodeByMediaType(null, ...$options); } $encoded->save($path); @@ -779,9 +779,9 @@ final class Image implements ImageInterface * * @see ImageInterface::encodeByMediaType() */ - public function encodeByMediaType(?string $type = null, int $quality = 75): EncodedImageInterface + public function encodeByMediaType(?string $type = null, ...$options): EncodedImageInterface { - return $this->encode(new MediaTypeEncoder($type, $quality)); + return $this->encode(new MediaTypeEncoder($type, ...$options)); } /** @@ -789,9 +789,9 @@ final class Image implements ImageInterface * * @see ImageInterface::encodeByExtension() */ - public function encodeByExtension(?string $extension = null, int $quality = 75): EncodedImageInterface + public function encodeByExtension(?string $extension = null, mixed ...$options): EncodedImageInterface { - return $this->encode(new FileExtensionEncoder($extension, $quality)); + return $this->encode(new FileExtensionEncoder($extension, ...$options)); } /** @@ -799,9 +799,9 @@ final class Image implements ImageInterface * * @see ImageInterface::encodeByPath() */ - public function encodeByPath(?string $path = null, int $quality = 75): EncodedImageInterface + public function encodeByPath(?string $path = null, mixed ...$options): EncodedImageInterface { - return $this->encode(new FilePathEncoder($path, $quality)); + return $this->encode(new FilePathEncoder($path, ...$options)); } /** @@ -809,20 +809,20 @@ final class Image implements ImageInterface * * @see ImageInterface::toJpeg() */ - public function toJpeg(int $quality = 75): EncodedImageInterface + public function toJpeg(mixed ...$options): EncodedImageInterface { - return $this->encode(new JpegEncoder($quality)); + return $this->encode(new JpegEncoder(...$options)); } /** * Alias of self::toJpeg() * - * @param int $quality + * @param mixed $options * @return EncodedImageInterface */ - public function toJpg(int $quality = 75): EncodedImageInterface + public function toJpg(mixed ...$options): EncodedImageInterface { - return $this->toJpeg($quality); + return $this->toJpeg(...$options); } /** @@ -830,20 +830,20 @@ final class Image implements ImageInterface * * @see ImageInterface::toJpeg() */ - public function toJpeg2000(int $quality = 75): EncodedImageInterface + public function toJpeg2000(mixed ...$options): EncodedImageInterface { - return $this->encode(new Jpeg2000Encoder($quality)); + return $this->encode(new Jpeg2000Encoder(...$options)); } /** * ALias of self::toJpeg2000() * - * @param int $quality + * @param mixed $options * @return EncodedImageInterface */ - public function toJp2(int $quality = 75): EncodedImageInterface + public function toJp2(mixed ...$options): EncodedImageInterface { - return $this->toJpeg2000($quality); + return $this->toJpeg2000(...$options); } /** @@ -851,9 +851,9 @@ final class Image implements ImageInterface * * @see ImageInterface::toPng() */ - public function toPng(): EncodedImageInterface + public function toPng(mixed ...$options): EncodedImageInterface { - return $this->encode(new PngEncoder()); + return $this->encode(new PngEncoder(...$options)); } /** @@ -861,9 +861,9 @@ final class Image implements ImageInterface * * @see ImageInterface::toGif() */ - public function toGif(): EncodedImageInterface + public function toGif(mixed ...$options): EncodedImageInterface { - return $this->encode(new GifEncoder()); + return $this->encode(new GifEncoder(...$options)); } /** @@ -871,9 +871,9 @@ final class Image implements ImageInterface * * @see ImageInterface::toWebp() */ - public function toWebp(int $quality = 75): EncodedImageInterface + public function toWebp(mixed ...$options): EncodedImageInterface { - return $this->encode(new WebpEncoder($quality)); + return $this->encode(new WebpEncoder(...$options)); } /** @@ -881,9 +881,9 @@ final class Image implements ImageInterface * * @see ImageInterface::toBitmap() */ - public function toBitmap(): EncodedImageInterface + public function toBitmap(mixed ...$options): EncodedImageInterface { - return $this->encode(new BmpEncoder()); + return $this->encode(new BmpEncoder(...$options)); } /** @@ -891,9 +891,9 @@ final class Image implements ImageInterface * * @return EncodedImageInterface */ - public function toBmp(): EncodedImageInterface + public function toBmp(mixed ...$options): EncodedImageInterface { - return $this->toBitmap(); + return $this->toBitmap(...$options); } /** @@ -901,9 +901,9 @@ final class Image implements ImageInterface * * @see ImageInterface::toAvif() */ - public function toAvif(int $quality = 75): EncodedImageInterface + public function toAvif(mixed ...$options): EncodedImageInterface { - return $this->encode(new AvifEncoder($quality)); + return $this->encode(new AvifEncoder(...$options)); } /** @@ -911,20 +911,20 @@ final class Image implements ImageInterface * * @see ImageInterface::toTiff() */ - public function toTiff(int $quality = 75): EncodedImageInterface + public function toTiff(mixed ...$options): EncodedImageInterface { - return $this->encode(new TiffEncoder($quality)); + return $this->encode(new TiffEncoder(...$options)); } /** * Alias of self::toTiff() * - * @param int $quality + * @param mixed $options * @return EncodedImageInterface */ - public function toTif(int $quality = 75): EncodedImageInterface + public function toTif(mixed ...$options): EncodedImageInterface { - return $this->toTiff($quality); + return $this->toTiff(...$options); } /** diff --git a/src/Interfaces/ImageInterface.php b/src/Interfaces/ImageInterface.php index 6b033a1e..e483f07d 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, int $quality = 75): ImageInterface; + public function save(?string $path = null, ...$options): 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, int $quality = 75): EncodedImageInterface; + public function encodeByMediaType(?string $type = null, ...$options): 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, int $quality = 75): EncodedImageInterface; + public function encodeByExtension(?string $extension = null, mixed ...$options): EncodedImageInterface; /** * Encode the image into the format represented by the given extension of @@ -593,66 +593,70 @@ interface ImageInterface extends IteratorAggregate, Countable * @param null|string $path * @return EncodedImageInterface */ - public function encodeByPath(?string $path = null, int $quality = 75): EncodedImageInterface; + public function encodeByPath(?string $path = null, mixed ...$options): EncodedImageInterface; /** * Encode image to JPEG format * - * @param int $quality + * @param mixed $options * @return EncodedImageInterface */ - public function toJpeg(int $quality = 75): EncodedImageInterface; + + public function toJpeg(mixed ...$options): EncodedImageInterface; /** * Encode image to Jpeg2000 format * - * @param int $quality + * @param mixed $options * @return EncodedImageInterface */ - public function toJpeg2000(int $quality = 75): EncodedImageInterface; + public function toJpeg2000(mixed ...$options): EncodedImageInterface; /** * Encode image to Webp format * - * @param int $quality + * @param mixed $options * @return EncodedImageInterface */ - public function toWebp(int $quality = 75): EncodedImageInterface; + public function toWebp(mixed ...$options): EncodedImageInterface; /** * Encode image to PNG format * + * @param mixed $options * @return EncodedImageInterface */ - public function toPng(): EncodedImageInterface; + public function toPng(mixed ...$options): EncodedImageInterface; /** * Encode image to GIF format * + * @param mixed $options * @return EncodedImageInterface */ - public function toGif(): EncodedImageInterface; + public function toGif(mixed ...$options): EncodedImageInterface; /** * Encode image to Bitmap format * + * @param mixed $options * @return EncodedImageInterface */ - public function toBitmap(): EncodedImageInterface; + public function toBitmap(mixed ...$options): EncodedImageInterface; /** * Encode image to AVIF format * - * @param int $quality + * @param mixed $options * @return EncodedImageInterface */ - public function toAvif(int $quality = 75): EncodedImageInterface; + public function toAvif(mixed ...$options): EncodedImageInterface; /** * Encode image to TIFF format * - * @param int $quality + * @param mixed $options * @return EncodedImageInterface */ - public function toTiff(int $quality = 75): EncodedImageInterface; + public function toTiff(mixed ...$options): EncodedImageInterface; }