timnarr / kirby-imagex

Modern Images for Kirby CMS – This plugin helps you orchestrate modern, responsive and performant images in Kirby.
MIT License
65 stars 0 forks source link

Picture Source should also have the sizes attribute #1

Closed wdebusschere closed 8 months ago

wdebusschere commented 8 months ago

Does this plugin works correctly for WebP or AVIF format? It always shows the largest format.

For us it works correctly if we add the sizes to the source in imagex-picture.php

<source <?= attr($source) ?> sizes="<?= $imgAttributes['sizes'] ?>" />

timnarr commented 8 months ago

Hi @wdebusschere, I think the current behavior of imagex is correct and I intentionally made it not this strict so that it works with all kinds of setups and third party lazy loading libraries.

You can and should definitely set the "sizes" attribute. To add attributes to the <source> tags simply set them via sourcesAttributes, like this:

  'sourcesAttributes' => ['shared' =>  ['sizes' => 'your-sizes-value']],

Here you can find a good example: https://github.com/timnarr/kirby-imagex/blob/main/docs/examples/loading-modes-eager-lazy.md#snippet-options

You can also set the sizes attribute via sourcesArtDirected when you need to have different sizes attribute for art-directed sources:

'sourcesArtDirected' => [
  [
    'ratio' => '21/9',
    'media' => '(min-width: 800px)',
    'image' => $block->imagetwo()->toFile(),
    'attributes' => [
      'shared' => [
        'sizes' => 'your-sizes-value'
      ]
    ]
  ]
],

I hope this helps 🙂