Skip to content

Commit

Permalink
Add support for webp_lossless to GD and GMagick
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi committed Feb 17, 2024
1 parent 922e032 commit 39efd33
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Gd/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,9 @@ private function finalizeOptions(Format $format, array $options)
$options['webp_quality'] = $options['quality'];
}
}
if (isset($options['webp_quality'])) {
if (!empty($options['webp_lossless'])) {
$result[] = defined('IMG_WEBP_LOSSLESS') ? IMG_WEBP_LOSSLESS : 100;
} elseif (isset($options['webp_quality'])) {
if ($options['webp_quality'] < 0 || $options['webp_quality'] > 100) {
throw new InvalidArgumentException('webp_quality option should be an integer from 0 to 100');
}
Expand Down
7 changes: 7 additions & 0 deletions src/Gmagick/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,13 @@ private function applyImageOptions(\Gmagick $image, array $options, $path)
if (isset($options['webp_quality'])) {
$image->setCompressionQuality($options['webp_quality']);
}
if (isset($options['webp_lossless'])) {
if (method_exists($image, 'setimageoption')) {
$image->setimageoption('webp', 'lossless', $options['webp_lossless'] ? 'true' : 'false');
} elseif ($options['webp_lossless']) {
$image->setCompressionQuality(100);
}
}
break;
}
if (isset($options['resolution-units']) && isset($options['resolution-x']) && isset($options['resolution-y'])) {
Expand Down
1 change: 1 addition & 0 deletions tests/tests/Image/AbstractImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,7 @@ public function imageCompressionQualityProvider()
array(Format::ID_JPEG, array('jpeg_quality' => 0), array('jpeg_quality' => 100)),
array(Format::ID_PNG, array('png_compression_level' => 9), array('png_compression_level' => 0)),
array(Format::ID_WEBP, array('webp_quality' => 0), array('webp_quality' => 100)),
array(Format::ID_WEBP, array('webp_quality' => 0), array('webp_lossless' => true)),
array(Format::ID_AVIF, array('avif_quality' => 0), array('avif_quality' => 100)),
array(Format::ID_AVIF, array('avif_quality' => 0), array('avif_lossless' => true)),
array(Format::ID_HEIC, array('heic_quality' => 0), array('heic_quality' => 100)),
Expand Down

0 comments on commit 39efd33

Please sign in to comment.