Skip to content

Commit

Permalink
Merge pull request #199 from 0xb4lint/fix-avifdec-options
Browse files Browse the repository at this point in the history
libavif version note
  • Loading branch information
freekmurze authored Jul 27, 2023
2 parents c67353b + 9b09c85 commit af17999
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ The package will use these optimizers if they are present on your system:
- [cwebp](https://developers.google.com/speed/webp/docs/precompiled)
- [avifenc](https://github.com/AOMediaCodec/libavif/blob/main/doc/avifenc.1.md)

Here's how to install all the optimizers on Ubuntu:
Here's how to install all the optimizers on Ubuntu/Debian:

```bash
sudo apt-get install jpegoptim
Expand All @@ -61,7 +61,7 @@ sudo apt-get install pngquant
sudo npm install -g svgo
sudo apt-get install gifsicle
sudo apt-get install webp
sudo apt-get install libavif-bin
sudo apt-get install libavif-bin # minimum 0.9.3
```

And here's how to install the binaries on MacOS (using [Homebrew](https://brew.sh/)):
Expand All @@ -75,6 +75,7 @@ brew install gifsicle
brew install webp
brew install libavif
```

And here's how to install the binaries on Fedora/RHEL/CentOS:

```bash
Expand Down
28 changes: 20 additions & 8 deletions src/Optimizers/Avifenc.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,41 @@ class Avifenc extends BaseOptimizer

public function canHandle(Image $image): bool
{
return $image->mime() === 'image/avif' || $image->extension() === 'avif';
if (version_compare(PHP_VERSION, '8.1.0', '<')) {
return $image->extension() === 'avif';
}

return $image->mime() === 'image/avif';
}

public function getCommand(): string
{
$this->tmpPath = tempnam(sys_get_temp_dir(), 'avifdec') . '.png';
return $this->getDecodeCommand().' && '
.$this->getEncodeCommand();
}

protected function getDecodeCommand()
{
$this->tmpPath = tempnam(sys_get_temp_dir(), 'avifdec').'.png';

$decodeOptionString = implode(' ', [
$optionString = implode(' ', [
'-j all',
'--ignore-icc',
'--no-strict',
'--png-compress 0',
]);
$encodeOptionString = implode(' ', $this->options);

$decode = "\"{$this->binaryPath}{$this->decodeBinaryName}\" {$decodeOptionString}"
return "\"{$this->binaryPath}{$this->decodeBinaryName}\" {$optionString}"
.' '.escapeshellarg($this->imagePath)
.' '.escapeshellarg($this->tmpPath);
}

protected function getEncodeCommand()
{
$optionString = implode(' ', $this->options);

$encode = "\"{$this->binaryPath}{$this->binaryName}\" {$encodeOptionString}"
return "\"{$this->binaryPath}{$this->binaryName}\" {$optionString}"
.' '.escapeshellarg($this->tmpPath)
.' '.escapeshellarg($this->imagePath);

return $decode . ' && ' . $encode;
}
}

0 comments on commit af17999

Please sign in to comment.