Why the code needs toFile()

Why does this code work correctly

<img src="<?= $page->image('back.webp')->url() ?>">.

and this code must include toFile() in the path for it to work

<?php foreach ($page->containerB()->toStructure() as $test) : ?>
    <img src="<?= $test->image()->toFile()->thumb(['crop' => true, 'width' => 260, 'height' => 180, 'format' => 'webp'])->url() ?>"
<?php endforeach ?>

I would very much appreciate an explanation

image() is a method of the Kirby\Cms\Page class. It returns an image object, if the image exists.

In this case, image() refers to your field name in the structure, here $test->image() returns a field object. You need to convert this field object value to a file object.

1 Like

Thank you very much!