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

perf: add AVIF optimizer #2014

Merged
merged 1 commit into from
Jul 21, 2024
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
2 changes: 1 addition & 1 deletion config/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@
'dir' => 'thumbnails', // where resized images are stored (`thumbnails` by default)
],
'optimize' => [
'enabled' => false, // enables images optimization with JpegOptim, Optipng, Pngquant 2, SVGO 1, Gifsicle, cwebp (`false` by default)
'enabled' => false, // enables images optimization with JpegOptim, Optipng, Pngquant 2, SVGO 1, Gifsicle, cwebp, avifenc (`false` by default)
],
'quality' => 75, // image quality after optimization or resize (`75` by default)
'responsive' => [
Expand Down
6 changes: 3 additions & 3 deletions docs/4-Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ assets:
resize:
dir: thumbnails # where resized images are stored (`thumbnails` by default)
optimize:
enabled: false # enables images optimization with JpegOptim, Optipng, Pngquant 2, SVGO 1, Gifsicle, cwebp (`false` by default)
enabled: false # enables images optimization with JpegOptim, Optipng, Pngquant 2, SVGO 1, Gifsicle, cwebp, avifenc (`false` by default)
quality: 75 # image quality after optimization or resize (`75` by default)
responsive:
widths: [] # `srcset` widths (`[480, 640, 768, 1024, 1366, 1600, 1920]` by default)
Expand Down Expand Up @@ -925,11 +925,11 @@ optimize:
ext: [js] # supported files extensions
images:
enabled: true # enables images files optimization
ext: [jpeg, jpg, png, gif, webp, svg] # supported files extensions
ext: [jpeg, jpg, png, gif, webp, svg, avif] # supported files extensions
```

:::important
Images compressor will use these binaries if they are present in the system: [JpegOptim](https://github.com/tjko/jpegoptim), [Optipng](http://optipng.sourceforge.net/), [Pngquant 2](https://pngquant.org/), [SVGO](https://github.com/svg/svgo), [Gifsicle](http://www.lcdf.org/gifsicle/) and [cwebp](https://developers.google.com/speed/webp/docs/cwebp).
Images compressor will use these binaries if they are present in the system: [JpegOptim](https://github.com/tjko/jpegoptim), [Optipng](http://optipng.sourceforge.net/), [Pngquant 2](https://pngquant.org/), [SVGO](https://github.com/svg/svgo), [Gifsicle](http://www.lcdf.org/gifsicle/), [cwebp](https://developers.google.com/speed/webp/docs/cwebp) and [avifenc](https://github.com/AOMediaCodec/libavif).
:::

## Override configuration
Expand Down
11 changes: 11 additions & 0 deletions src/Assets/Image/Optimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Cecil\Assets\Image;

use Spatie\ImageOptimizer\OptimizerChain;
use Spatie\ImageOptimizer\Optimizers\Avifenc;
use Spatie\ImageOptimizer\Optimizers\Cwebp;
use Spatie\ImageOptimizer\Optimizers\Gifsicle;
use Spatie\ImageOptimizer\Optimizers\Jpegoptim;
Expand Down Expand Up @@ -56,6 +57,16 @@ public static function create(int $quality): OptimizerChain
'-pass 10',
'-mt',
'-q $quality',
]))
->addOptimizer(new Avifenc([
'-a cq-level=' . round(63 - $quality * 0.63),
'-j all',
'--min 0',
'--max 63',
'--minalpha 0',
'--maxalpha 63',
'-a end-usage=q',
'-a tune=ssim',
]));
}
}