Skip to content

Commit

Permalink
perf: add AVIF optimizer (#2014)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny authored Jul 21, 2024
1 parent f096169 commit acf3538
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
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',
]));
}
}

0 comments on commit acf3538

Please sign in to comment.