I’m following the responsive image cookbook Responsive images | Kirby CMS
I have the following code in the config.php file
<?php
return [
'debug' => true,
'thumbs' => [
'srcsets' => [
'heroimage' => [
'900w' => ['width' => 900, 'height' => 600, 'crop' => 'center', 'quality' => 80],
'1650w' => ['width' => 1650, 'height' => 1100, 'crop' => 'center', 'quality' => 80],
'2400w' => ['width' => 2400, 'height' => 1600, 'crop' => 'center', 'quality' => 80]
]
// more srcsets as needed
]
]
];
And then in my template:
<div class="hero-image">
<?php if($image = $page->hero_image()->toFile()): ?>
<img
alt="<?= $image->alt() ?>"
src="<?= $image->crop(2400, 1600)->url() ?>"
srcset="<?= $image->srcset('heroimage') ?>"
sizes="100vw"
width="<?= $image->resize(2400)->width() ?>" // what does this do? //
height="<?= $image->resize(2400)->height() ?>" // what does this do? //
>
<?php endif ?>
</div>
My question is, what do these two lines of code do?
width="<?= $image->resize(2400)->width() ?>" // what does this do? //
height="<?= $image->resize(2400)->height() ?>" // what does this do? //
I can delete them and everything still displays as it should. Also, I don’t want the image to be “resized” (and presumably maintain the aspect ratio of the original image) – I want the images to be “cropped”. So I’m hesitate about the word “resize” in these lines of code.
UPDATE
From another post:
I think it doesn’t really matter that much. The purpose of adding width and height is to prevent CLS by providing the correct aspect ratio: Optimize Cumulative Layout Shift | Articles | web.dev
I still don’t understand those two lines of code. The height is never going to be 2400px. Should I change this to?
width="<?= $image->resize(2400)->width() ?>"
height="<?= $image->resize(1600)->height() ?>"
or?
width="<?= $image->crop(2400)->width() ?>"
height="<?= $image->crop(1600)->height() ?>"
UPDATE 2
Hang on, am I correct in thinking those lines of code are not manipulating or changing the size of the images, but simply calculating and telling the browser the width of the image (when the image is 2400px wide??) and the height of the image when the image is 2400px wide? To prevent CLS by providing the correct aspect ratio? In which case I can use:
width="<?= $image->resize(2400)->width() ?>"
height="<?= $image->resize(2400)->height() ?>"