Does srcset optimizes image file size?


I have a files section to upload images. Some of the ones uploaded by the editor have been very big, around 9.000 x 5.000px and 2.5MB size. They have been told already that the images should be smaller but I tried to use srcsets to decrease the image size.

Here is my config.php file:

return [
    'debug'  => true,
    'languages' => true,
    'thumbs' => [
    'srcsets' => [
      'default' => [
        '800w' => ['width' => 800, 'quality' => 80],
        '1024w' => ['width' => 1024, 'quality' => 80],
        '1440w' => ['width' => 1440, 'quality' => 80],
        '2048w' => ['width' => 2048, 'quality' => 80]

And I call them in the template just like this:

$html .= '<img src="' . $image->resize(1024)->url() . '"';
$html .= 'srcset="' . $image->srcset() . '">';

I realized the page size in the frontend is not decreasing, downloading a huge amount of assets. Every image has almost the same original size of 2.5MB.

I tried many different things in case it is not picking up the correct images but seems like that is working. Then I checked the media folder and the images are there followed by their corresponding extension regarding the quality and width but looked at their specs they seem to be identical.

I am not sure if this is the normal srcset behavior and it is not a good option to reduce the file size of an image but I al also surprised the generated images are the same resolution.

If the image have the exact same pixel sizes but just different files names, then for some reason Kirby cannot create the thumbs.

Are you using the default gd thumbs driver?

Yes, I haven’t configured any so it is using the default.

Is gd enabled on your server? Check with phpinfo().

If yes, check if you get any error messages in your server error or php error logs.

I’m not sure if there is a limit to what files sizes gd can handle.

It might be worth checking if normal size images are resized correctly.

On a side note:
It might be worth noting that unless you use full-width images only, srcset must always be accompanied by a sizes attribute, otherwise the wrong image sizes will be chosen. See also:

In the phpinfo() was enabled.
Then I checked the error logs and I found several errors like this:

[06-Sep-2022 13:18:33 UTC] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 32768 bytes) in /Applications/MAMP/htdocs/websitename/kirby/vendor/filp/whoops/src/Whoops/Util/Misc.php on line 1

That means the size of the images is too big for the server memory to handle that image scaling process?

Thanks for pointing about the sizes attribute!

That might well be. If you can install ImageMagick or if it is available, I’d switch to that, more performant. You might have to check your memory settings as well.