Wrapping element every 3 images

Hi,
I have a loop for images that have been added to a page, which looks like this (pseudo code to hopefully make things clearer):

<?php if($images = $page->imagesField()->toFiles()): ?>
<wrapper>
<?php foreach($images as $image): ?>
      <image>
<?php endforeach ?>
</wrapper>
<?php endif ?>

I now need to split the loop every 3 items, so it looks something like the following when it is output:

<wrapper>
      <image>
      <image>
      <image>
</wrapper>
<wrapper>
      <image>
      <image>
      <image>
</wrapper>
<wrapper>
      <image>
      <image>
</wrapper> <!-- last row can have less than three if needed -->

What is the best way of achieving this with Kirby?

And in case you need a piece of code (because the method has no example):

<?php
$images = $page->imagesField()->toFiles();
if ($images->isNotEmpty()):
  $chunks = $images->chunk(3);
  foreach ($chunks as $chunk => $images): ?>
    <div class="wrapper">
      <?php foreach ($images as $image): ?>
      <img src="<?= $image->url() ?>" alt="<?= $image->url() ?>">
      <?php endforeach ?>
    </div>
  <?php endforeach ?>
<?php endif ?>

On a side note:

This if statement is pretty useless, because it will always be true (because toFiles() always returns a files collection object, which might or might not be empty). I have replaced this condition with a check for an empty collection.

This is different fromtoFile()/toPage() which returns either a file/page object or null.

That’s great, thanks @texnixe.

Thanks also for the pointer on the empty check