I was interested to read the article linked to in the Kirby Kosmos email newsletter – Looking Forward to (Hopefully) Not Needing Responsive Images One Day | Alex MacArthur
In summary the article says that with AVIF and WebP image formats providing substantially smaller images, there is considerable less need to provide responsive images (the same image at different sizes). And that maybe we only need to provide one image in a modern image format.
I’m looking to explore this idea and asses the file sizes of the different image formats. But how do I write the code to provide AVIF images, that fallback to WebP, that fallback to JPG?
The code below seems to work, and I don’t need to do anything with the config.php file. But it’s rather verbose. I was hoping the code would be short and sweet. Any thoughts or pointers welcomed
<?php
if ($image = $page->hero_image()->toFile()): ?>
<picture>
<source
srcset="<?= $image->thumb(['width' => 2400, 'height' => 1600, 'crop' => 'center', 'quality' => 90, format' => 'avif',])->url() ?>"
type="image/avif"
>
<source
srcset="<?= $image->thumb(['width' => 2400, 'height' => 1600, 'crop' => 'center', 'quality' => 90, 'format' => 'webp',])->url() ?>"
type="image/webp"
>
<img
alt="<?= $image->alt() ?>"
src="<?= $image->thumb(['width' => 2400, 'height' => 1600, 'crop' => 'center', 'quality' => 90, 'format' => 'jpg',])->url() ?>"
width="2400"
height="1600"
>
</picture>
<?php endif ?>