Using if statement to generate image url

Hi all,

First time using Kirby and I am trying to do an optional field for the user. I’ve added the image to my control panel however it’s not getting the correct path.

  <?php if ($page->circleImg()->isNotEmpty()) : ?>
            <div class="row">
                <?= $page->circleText() ?>
                    <div class="imgWrap">
                        <img src="<?= $page->circleImg()->url() ?>" alt="<?= $page->circleImgAlt() ?>" />
                    </div>
            </div>
  <?php endif ?>

getting this error in devtools:
GET http://localhost/whats-on/-%20hotel.png 404 (Not Found)
whats-on/ is the directory of the pages parent
should be /whats-on/currentpage/hotel.png

Your code obviously has two problems:

  1. the variable “$page” does not lead to the page you need or want.
    It should contain “/whats-on/currentpage”, but it shows “/whats-on”.
    Temporarily add in front of the “if” line to check that:
<?php dump($page); ?>

or

<?php dump($page->url()); ?>

Tip:
Have you written “if ($page = ...)” instead of “if ($page == ...)” somewhere in the code before the code snippet (one equal sign instead of two)?

  1. the content of the variable “$page->circleImg()” obviously starts with “- ” (shows “-%20”)
    Try to change “$page->circleImg()” to “$page->circleImg()->toFile()”, if it contains only ONE file. Else use “toFiles()” in an “foreach” block.

Supplementary:
Try to change “->isNotEmpty()” to “->exists()” in the “if” line!

Good luck!

  <?php if ($page->circleImg()->isNotEmpty()) : ?>
            <div class="row">
                <?= $page->circleText() ?>
                    <div class="imgWrap">
                        <?php if ($image = $page->circleImg()->toFile()) : ?>
                        <img src="<?= $image->url() ?>" alt="<?= $page->circleImgAlt() ?>" />
                       <?php endif ?>
                    </div>
            </div>
  <?php endif ?>

Assuming that your circleImg field is a files field, you have to convert it to a files object first.

Is the alt text really stored in the page or the image metadata?

Ah working now, thank you both!
texnixe I originally was going to do the alt tag stored in the page, but out of curiousity how would I go about generating that with the image’s metadata?

You have to assign a file blueprint to the uploaded files. In a file blueprint, you can define the fields you want to have in your file metadata. For example, an image.yml could have a field for the alt attribute and maybe fields for the photographer etc. Then with the data filled in, you can call $image->alt() or whatever you call that field etc.

File blueprints: File blueprint | Kirby CMS

File blueprints can be assigned to files you upload via a files field’s upload option like this: Files | Kirby CMS

Very cool, thank you!

If you only want to display that row if the image exists, you can shorten your code like this:

<?php if ($image = $page->circleImg()->toFile()) : ?>
            <div class="row">
                <?= $page->circleText() ?>
                    <div class="imgWrap">
                        <img src="<?= $image->url() ?>" alt="<?= $image->alt() ?>" />
                    </div>
            </div>
  <?php endif ?>

And welcome to the Kirby community :slightly_smiling_face:!