How to use srcset() method?

Hello,

I don’t know how to use the srcset() method.

I tried with different samples that are written in the doc but it produces an empty srcset attribute each time.
To be more precise, I tried :

  • $image->srcset([800, 1600])
  • $image->srcset([ 800 => '1x', 1600 => '1.5x']);

What should it produces precisely ? Two resized version of the given image and the associated srcset attribute ? That would be great ! But I can’t make it work. Did I miss something ?

Here is my full code :

<?php 
            $allBlocks = [];
            
            foreach($site->children()->listed() as $project) {

                if ($project->blocks()->isNotEmpty()) {
                    $pictures = $project->blocks()->toStructure();

                    foreach ($pictures as $picture) {
                        if ($picture->home() == "true") {
                            $block = [
                                "pic" => $picture->pic()->toFile()->resize(2000),
                                "projectTitle" => $project->title(),
                                "caption" => $picture->caption(),
                                "link" => $project->url(),
                                "ref" => $project->ref()
                            ];
                            
                            array_push($allBlocks, $block);
                        }
                    }
                }
            };

            if ($site->orderprojects() == 'false') shuffle($allBlocks);
            foreach($allBlocks as $block) {
                echo '<a class="toProject toProject--wait" data-title="' . $block["projectTitle"] . '" href="' . $block["link"] . '">';
                echo '<figure class="block hide">';
                echo "<img class=\"block__image\" src=\"" 
                    . $block["pic"]->url() 
                    . "\" srcset=\"" . $block["pic"]->srcset([
                        800  => '1x',
                        1600 => '1.5x'
                    ]) 
                    . "\" />";
                echo '<figcaption class="block__caption">' . '<span class="block__ref">' . $block["ref"] . '</span> ' . $block["caption"] . '</figcation>';
                echo '</figure>';
                echo "</a>";
            }
        ?>

You are trying to call srcset on a thumb which is not possible. You have to call it on the original image.