From f4637ebce31b8ed256323a23c92fea6c24b3cfd4 Mon Sep 17 00:00:00 2001 From: Arnaud Ligny Date: Sat, 20 Jul 2024 01:06:32 +0200 Subject: [PATCH] perf: add AVIF optimizer --- config/default.php | 2 +- docs/4-Configuration.md | 6 +++--- src/Assets/Image/Optimizer.php | 11 +++++++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/config/default.php b/config/default.php index 2450d0164..a986de5e5 100644 --- a/config/default.php +++ b/config/default.php @@ -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' => [ diff --git a/docs/4-Configuration.md b/docs/4-Configuration.md index 8dc904cec..537b6fe2a 100644 --- a/docs/4-Configuration.md +++ b/docs/4-Configuration.md @@ -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) @@ -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 diff --git a/src/Assets/Image/Optimizer.php b/src/Assets/Image/Optimizer.php index 2e62c6056..6f94c54f7 100644 --- a/src/Assets/Image/Optimizer.php +++ b/src/Assets/Image/Optimizer.php @@ -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; @@ -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', ])); } }