Skip to content

Commit

Permalink
Imagick now supports near-lossless. Closes #299
Browse files Browse the repository at this point in the history
  • Loading branch information
rosell-dk committed Oct 12, 2021
1 parent 5bb3608 commit 85130c2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
21 changes: 17 additions & 4 deletions src/Convert/Converters/ImageMagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class ImageMagick extends AbstractConverter
protected function getUnsupportedDefaultOptions()
{
return [
'near-lossless',
'size-in-percentage',
];
}
Expand Down Expand Up @@ -138,9 +137,10 @@ public function checkOperationality()
/**
* Build command line options
*
* @param string $versionNumber. Ie "6.9.10-23"
* @return string
*/
private function createCommandLineOptions()
private function createCommandLineOptions($versionNumber = 'unknown')
{
// PS: Available webp options for imagemagick are documented here:
// https://imagemagick.org/script/webp.php
Expand Down Expand Up @@ -194,6 +194,12 @@ private function createCommandLineOptions()
$commandArguments[] = '-define webp:use-sharp-yuv=true';
}

if (version_compare($versionNumber, '7.0.10-54', '>=')) {
$commandArguments[] = '-define webp:near-lossless=' . escapeshellarg($options['near-lossless']);
} else {
$this->logLn('Note: near-lossless is not supported in your version of ImageMagick. ImageMagic >= 7.0.10-54 is required', 'italic');
}

// TODO: Imagick now supports near-lossless !

$commandArguments[] = '-define webp:method=' . $options['method'];
Expand All @@ -206,9 +212,16 @@ private function createCommandLineOptions()

protected function doActualConvert()
{
$this->logLn($this->getVersion());
$version = $this->getVersion();

$this->logLn($version);

preg_match('#\d+\.\d+\.\d+[\d\.\-]+#', $version, $matches);
$versionNumber = (isset($matches[0]) ? $matches[0] : 'unknown');

$this->logLn('Extracted version number: ' . $versionNumber);

$command = $this->getPath() . ' ' . $this->createCommandLineOptions() . ' 2>&1';
$command = $this->getPath() . ' ' . $this->createCommandLineOptions($versionNumber) . ' 2>&1';

$useNice = (($this->options['use-nice']) && self::hasNiceSupport()) ? true : false;
if ($useNice) {
Expand Down
18 changes: 17 additions & 1 deletion src/Convert/Converters/Imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class Imagick extends AbstractConverter
protected function getUnsupportedDefaultOptions()
{
return [
'near-lossless',
'size-in-percentage',
'use-nice'
];
Expand Down Expand Up @@ -106,6 +105,13 @@ protected function doActualConvert()
// This might throw - we let it!
$im = new \Imagick($this->source);

$version = \Imagick::getVersion();
$this->logLn('Version: ' . $version['versionString']);

preg_match('#\d+\.\d+\.\d+[\d\.\-]+#', $version['versionString'], $matches);
$versionNumber = (isset($matches[0]) ? $matches[0] : 'unknown');
$this->logLn('Extracted version number: ' . $versionNumber);

//$im = new \Imagick();
//$im->pingImage($this->source);
//$im->readImage($this->source);
Expand Down Expand Up @@ -134,6 +140,12 @@ protected function doActualConvert()
$im->setOption('webp:low-memory', $options['low-memory'] ? 'true' : 'false');
$im->setOption('webp:alpha-quality', $options['alpha-quality']);

if (version_compare($versionNumber, '7.0.10-54', '>=')) {
$im->setOption('webp:near-lossless', $options['near-lossless']);
} else {
$this->logLn('Note: near-lossless is not supported in your version of ImageMagick. ImageMagic >= 7.0.10-54 is required', 'italic');
}

if ($options['auto-filter'] === true) {
$im->setOption('webp:auto-filter', 'true');
}
Expand All @@ -142,6 +154,10 @@ protected function doActualConvert()
$im->setOption('webp:use-sharp-yuv', 'true');
}

if ($options['near-lossless'] != 100) {
$im->setOption('webp:near-lossless', $options['near-lossless']);
}

if ($options['metadata'] == 'none') {
// To strip metadata, we need to use the stripImage() method. However, that method does not only remove
// metadata, but color profiles as well. We want to keep the color profiles, so we grab it now to be able
Expand Down

0 comments on commit 85130c2

Please sign in to comment.