Hi all
I’m trying to create responsive images based on the Responsive images cookbook recipe.
I can get the images to display in different sizes based on the viewport however I can’t get the image height & width displayed from those thumb images. I am still trying to get my head around this so I may be doing it wrong, but I want to prefill width and height based on the image size.
Below is my code,
config
'srcsets' => [
'default' => [
'300w' => ['width' => 300, 'height' => 169, 'crop' => 'center', 'format' => 'webp'],
'600w' => ['width' => 600, 'height' => 338, 'crop' => 'center', 'format' => 'webp'],
'900w' => ['width' => 900, 'height' => 506, 'crop' => 'center', 'format' => 'webp'],
'1200w' => ['width' => 1200, 'height' => 675, 'crop' => 'center', 'format' => 'webp'],
'1800w' => ['width' => 1800, 'height' => 1013, 'crop' => 'center', 'format' => 'webp']
],
'4x3webp' => [
'300w' => ['width' => 300, 'height' => 225, 'crop' => 'center', 'format' => 'webp'],
'600w' => ['width' => 600, 'height' => 450, 'crop' => 'center', 'format' => 'webp'],
'900w' => ['width' => 900, 'height' => 675, 'crop' => 'center', 'format' => 'webp'],
'1200w' => ['width' => 1200, 'height' => 900, 'crop' => 'center', 'format' => 'webp'],
'1800w' => ['width' => 1800, 'height' => 1350, 'crop' => 'center', 'format' => 'webp']
],
// more srcsets as needed
]
]
template
<?php
$sizes = "(min-width: 1200px) 30vw,
(min-width: 900px) 50vw,
(min-width: 600px) 50vw,
100vw";
if ($image = $product->cover()->toFile()):?>
<picture>
<source
srcset="<?= $image->srcset('16x9webp') ?>"
sizes="<?= $sizes ?>"
type="image/webp"
>
<img
alt="Window <?= $product->title() ?> Canberra"
src="<?= $image->resize(300)->url() ?> ?>"
srcset="<?= $image->srcset() ?>"
sizes="<?= $sizes ?>"
loading="lazy"
>
</picture>
<?php endif ?>
As you can see there is a gap between sizes and loading within the template code, it’s missing the image width and height. If I add the width and height elements as per the recipe, then my image is always displayed in 1800 width.
height="<?= $image->resize(1800)->height() ?>"
What am I doing wrong, or what am I not understanding?