Skip to content

Commit

Permalink
Add quality parameter to driver ambiguous decoders
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Dec 23, 2023
1 parent 097bfed commit ea13f2e
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 23 deletions.
10 changes: 10 additions & 0 deletions src/Encoders/AutoEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

class AutoEncoder extends MediaTypeEncoder
{
/**
* Create new encoder instance
*
* @param int $quality
* @return void
*/
public function __construct(protected int $quality = 75)
{
}

/**
* {@inheritdoc}
*
Expand Down
11 changes: 6 additions & 5 deletions src/Encoders/FileExtensionEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand Down Expand Up @@ -47,13 +48,13 @@ protected function encoderByFileExtension(?string $extension): EncoderInterface
}

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 . ').'),
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/Encoders/FilePathEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand Down
11 changes: 6 additions & 5 deletions src/Encoders/MediaTypeEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand Down Expand Up @@ -43,13 +44,13 @@ public function encode(ImageInterface $image): EncodedImageInterface
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 . ').'),
};
}
Expand Down
16 changes: 8 additions & 8 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,11 @@ public function encode(EncoderInterface $encoder = new AutoEncoder()): EncodedIm
*
* @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;
}
Expand Down Expand Up @@ -766,29 +766,29 @@ public function drawLine(callable $init): ImageInterface
*
* @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));
}

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

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

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Interfaces/ImageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function encode(EncoderInterface $encoder): EncodedImage;
* @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
Expand Down Expand Up @@ -573,7 +573,7 @@ public function drawLine(callable $init): ImageInterface;
* @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
Expand All @@ -583,7 +583,7 @@ public function encodeByMediaType(?string $type = null): EncodedImageInterface;
* @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
Expand All @@ -593,7 +593,7 @@ public function encodeByExtension(?string $extension = null): EncodedImageInterf
* @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
Expand Down

0 comments on commit ea13f2e

Please sign in to comment.