New images cause php error

good morning,

i’m using the following code to generate a image gallery. it worked as intended until yesterday when i tried to upload some new images. asdf

<?php if($page->gallery()->isNotEmpty()): ?>
  <div class="carousel-container">
    <div class="gallery">
      <div class="carousel">
      <?php $images = $page->gallery()->toStructure();
      foreach($images as $imagename): ?>
        <div class="carousel-cell">
          <?php  if ($imagename->startSlideshowImage()->isNotEmpty() ) { ?>
        <figure>
          <?= $imagename->image()->toFile()->resize(1500, null, 70) ?>
          <figcaption >
            <?= $imagename->image()->toFile()->caption() ?>
            <span class="carousel-status"></span>

          </figcaption>
        </figure>
        <?php } else { ?>
          <div class="gallery__video">
            <iframe id="videoplayer" src="https://player.vimeo.com/video/<?= $imagename->startSlideshowVideo() ?>?background=1&title=0&byline=0" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
            <div class="project__controls">
              <div class="project__play"></div>
              <div class="project__pause"></div>
            </div>
          </div>
        <?php } ?>
        </div>
      <?php endforeach ?>
      </div>
    </div>
  </div>
<?php endif ?>

the php error is: Call to a member function resize() on null on this line:
<?= $imagename->image()->toFile()->resize(1500, null, 70) ?>
when i remove this line the the caption lines causes an similar error.

with <?= $page->image() i can call the images of the page, so it’s not the upload wich is broken (the images are uploaded correctly as well).

all the old galleries work as intended until i upload a new image. so probably the ->resize() is somehow broken? but i never touched any ‘system’-file of kirby.

Your code is not correct, please see this cookbook recipe: https://getkirby.com/docs/cookbook/handling-images-in-kirby#accessing-images-in-templates, the section on handling structure items.

I’m not quite sure what the fieldname is that contains the image, if it is startSlideshowImage it should be

$image = $imagename->startSlideshowImage->toFile();
if($image) {
// do something
}
1 Like

that fixed it… (strangely enough didn’t touch this snipped for days :/)

but for better understanding:
the already resized images worked bc the were already stored. when i added new images kirby failed to resize them – because of the code error – and caused the php error?

thanks again.

I don’t know why that code worked before, it doesn’t make sense. Maybe there was something in the cache.

Keep in mind that apart from fixing the old code, the important thing here is to check if the image exists before you call any method on that object. Just checking if the field is empty or not doesn’t help you any, you can in fact remove that check altogether, because if the field is empty, $image will be false in any case.

1 Like