Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libavif version note #199

Merged
merged 3 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}