1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-23 22:12:51 +02:00

Add dynamic Encoder options

This commit is contained in:
Oliver Vogel
2024-01-10 11:22:05 +01:00
parent 448c415d44
commit 30ed4a649c
15 changed files with 126 additions and 106 deletions

View File

@@ -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}
*

View File

@@ -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}
*

View File

@@ -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)
{
}
}

View File

@@ -2,6 +2,8 @@
namespace Intervention\Image\Encoders;
class BmpEncoder extends AbstractEncoder
use Intervention\Image\Interfaces\SpecializableInterface;
class BmpEncoder extends AbstractEncoder implements SpecializableInterface
{
}

View File

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

View File

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

View File

@@ -2,6 +2,8 @@
namespace Intervention\Image\Encoders;
class GifEncoder extends AbstractEncoder
use Intervention\Image\Interfaces\SpecializableInterface;
class GifEncoder extends AbstractEncoder implements SpecializableInterface
{
}

View File

@@ -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)
{
}
}

View File

@@ -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)
{
}
}

View File

@@ -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 . ').'),
};
}

View File

@@ -2,6 +2,8 @@
namespace Intervention\Image\Encoders;
class PngEncoder extends AbstractEncoder
use Intervention\Image\Interfaces\SpecializableInterface;
class PngEncoder extends AbstractEncoder implements SpecializableInterface
{
}

View File

@@ -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)
{
}
}

View File

@@ -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)
{
}
}

View File

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

View File

@@ -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;
}