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

Add quality parameter to driver ambiguous decoders

This commit is contained in:
Oliver Vogel
2023-12-23 10:10:26 +01:00
parent 097bfed504
commit ea13f2e0bb
6 changed files with 36 additions and 23 deletions

View File

@@ -7,6 +7,16 @@ use Intervention\Image\Interfaces\ImageInterface;
class AutoEncoder extends MediaTypeEncoder class AutoEncoder extends MediaTypeEncoder
{ {
/**
* Create new encoder instance
*
* @param int $quality
* @return void
*/
public function __construct(protected int $quality = 75)
{
}
/** /**
* {@inheritdoc} * {@inheritdoc}
* *

View File

@@ -13,9 +13,10 @@ class FileExtensionEncoder extends AutoEncoder
* Create new encoder instance to encode to format of given file extension * Create new encoder instance to encode to format of given file extension
* *
* @param null|string $extension * @param null|string $extension
* @param int $quality
* @return void * @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) { return match ($extension) {
'webp' => new WebpEncoder(), 'webp' => new WebpEncoder($this->quality),
'avif' => new AvifEncoder(), 'avif' => new AvifEncoder($this->quality),
'jpeg', 'jpg' => new JpegEncoder(), 'jpeg', 'jpg' => new JpegEncoder($this->quality),
'bmp' => new BmpEncoder(), 'bmp' => new BmpEncoder(),
'gif' => new GifEncoder(), 'gif' => new GifEncoder(),
'png' => new PngEncoder(), 'png' => new PngEncoder(),
'tiff', 'tif' => new TiffEncoder(), 'tiff', 'tif' => new TiffEncoder($this->quality),
default => throw new EncoderException('No encoder found for file extension (' . $extension . ').'), default => throw new EncoderException('No encoder found for file extension (' . $extension . ').'),
}; };
} }

View File

@@ -11,9 +11,10 @@ class FilePathEncoder extends FileExtensionEncoder
* Create new encoder instance to encode to format of file extension in given path * Create new encoder instance to encode to format of file extension in given path
* *
* @param null|string $path * @param null|string $path
* @param int $quality
* @return void * @return void
*/ */
public function __construct(protected ?string $path = null) public function __construct(protected ?string $path = null, protected int $quality = 75)
{ {
} }

View File

@@ -13,9 +13,10 @@ class MediaTypeEncoder implements EncoderInterface
* Create new encoder instance to encode given media (mime) type * Create new encoder instance to encode given media (mime) type
* *
* @param null|string $type * @param null|string $type
* @param int $quality
* @return void * @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 protected function encoderByMediaType(string $type): EncoderInterface
{ {
return match ($type) { return match ($type) {
'image/webp' => new WebpEncoder(), 'image/webp' => new WebpEncoder($this->quality),
'image/avif' => new AvifEncoder(), 'image/avif' => new AvifEncoder($this->quality),
'image/jpeg' => new JpegEncoder(), 'image/jpeg' => new JpegEncoder($this->quality),
'image/bmp' => new BmpEncoder(), 'image/bmp' => new BmpEncoder(),
'image/gif' => new GifEncoder(), 'image/gif' => new GifEncoder(),
'image/png' => new PngEncoder(), '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 . ').'), default => throw new EncoderException('No encoder found for media type (' . $type . ').'),
}; };
} }

View File

@@ -256,11 +256,11 @@ final class Image implements ImageInterface, Countable
* *
* @see ImageInterface::save() * @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; $path = is_null($path) ? $this->origin()->filePath() : $path;
$this->encodeByPath($path)->save($path); $this->encodeByPath($path, $quality)->save($path);
return $this; return $this;
} }
@@ -766,9 +766,9 @@ final class Image implements ImageInterface, Countable
* *
* @see ImageInterface::encodeByMediaType() * @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() * @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() * @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));
} }
/** /**

View File

@@ -74,7 +74,7 @@ interface ImageInterface extends IteratorAggregate, Countable
* @param null|string $path * @param null|string $path
* @return ImageInterface * @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 * Apply given modifier to current image
@@ -573,7 +573,7 @@ interface ImageInterface extends IteratorAggregate, Countable
* @param null|string $type * @param null|string $type
* @return EncodedImageInterface * @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 * 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 * @param null|string $extension
* @return EncodedImageInterface * @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 * 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 * @param null|string $path
* @return EncodedImageInterface * @return EncodedImageInterface
*/ */
public function encodeByPath(?string $path = null): EncodedImageInterface; public function encodeByPath(?string $path = null, int $quality = 75): EncodedImageInterface;
/** /**
* Encode image to JPEG format * Encode image to JPEG format