Generate json - absolute path to image

Hello,

how to get image absolute path ?

<?php

header('Content-type: application/json; charset=utf-8');

$data = $pages->find('tagesmenue')->children();
$json = array();
foreach($data as $article) {

$test= thumb($article->article_image()->toFile(), array('width' => 200))->url();

  $json[] = array(

    'date' => (string)$article->date_event(),
    'title' => (string)$article->title(),
    'text' => (string)$article->text(),
    'image' => (string)$article->article_image()->url()
  );

}

echo json_encode($json);
?>

What do you want as image? The thumb url or the real image url? You get the image in the same way you get it in your thumb function, with toFile().

But your code lacks a check if the image exists before calling any method on it. If you do it like this and the file does not exist: ERROR!

Thank you @texnixe for answer.

with the thumbnail path I’m doing it that way now

<?php

header('Content-type: application/json; charset=utf-8');

$data = $pages->find('tagesmenue')->children();
$json = array();
foreach($data as $article) {
	$article_image_thumb = 0;
	if($article->article_image()->isNotEmpty())
	{
		$article_image_thumb = thumb($article->article_image()->toFile(), array('width' => 1200))->url();
	}
	  $json[] = array(

    'date' => (string)$article->date_event(),
    'title' => (string)$article->title(),
    'text' => (string)$article->text(),
    'image' => (string)$article_image_thumb
  );

}

echo json_encode($json);

?>

how to get the file path for the real image url ?

Maybe I did not express myself well, so here again: you get the real image with the toFile() field method, the same way as you do it within the thumb helper.

$image = $article->article_image()->toFile();

And again: You have to check if the image exists before you call the url() method on it, for both the thumb and the real image.


if($image = $article->article_image()->toFile()):
$url = $image->url();
else:
$url = '';
endif;

And the same when you use the thumb helper. If $article->article_image()->toFile() is not an image object, the thumb helper will throw an error as well.

Checking if the field is empty or not is not necessary. Because if the field is empty, $image will be false anyway.

Instead of an if statement, you can do this shorter:

$url = ($image = $article->article_image()->toFile())? $image->url(): ''; // note that this is already a string, not need to cast to string again

And here the complete code:

<?php

header('Content-type: application/json; charset=utf-8');

$data = $pages->find('tagesmenue')->children();
$json = [];
foreach($data as $article):
    
    $image = $article->article_image()->toFile();
       
    $thumbURL = $image? $image->resize(1200)->url(): '';
    $imageURL = $image? $image->url(): '';
    
    $json[] = [

        'date' => (string)$article->date_event(),
        'title' => (string)$article->title(),
        'text' => (string)$article->text(),
        'image' => $imageURL,
        'thumb' => $thumbURL
    ];

endforeach;

echo json_encode($json);

Maybe this cookbook article is helpful for you if you are still struggling with images: https://getkirby.com/docs/cookbook/handling-images-in-kirby

Thank you @texnixe for the detailed explanation.:gift:
I have another question how to get the file path in a structur field?

i tried this:

  <article class="text-align-justify">
	        <div ><?php echo $page->text()->kirbytext() ?></div>
          <ul>
            <?php
            $links = $page->Links()->yaml();

            foreach($links as $link): ?>
            <?php
            $link_img = $link['link_img'];
            $image = $page->string($link['link_img'])->toFile();

            $thumbURL = $image? $image->resize(1200)->url(): '';
            $imageURL = $image? $image->url(): '';
            echo $imageURL;

            ?>

            <li>
              <a href=<?php echo $link['link_url'] ?> target="blank">
                <img src=<?php echo $link['link_text'] ?>>
              </a>
            </li>

            <?php endforeach ?>

          </ul>
        </article>

First of all, you can make your life much easier if you use toStructure() when working with a structure field. Secondly, it is all explained in the cookbook recipe I linked to above, see in particular the section about “Accessing images stored in a structure field”.

yes, sorry

    <article class="text-align-justify">
        <div ><?php echo $page->text()->kirbytext() ?></div>


		<?php
		$link_items = $page->Links()->toStructure(); ?>
		<li>
			<?php
			foreach($link_items as $link_item):?>
			<!--//link url-->
			<a href="<?php echo $link_item->link_url() ?>" target="blank">
			<!--Link image-->
			<?php 
			  $image = $link_item->link_img()->toFile();
			  if($image): ?>
			    <img src="<?= $image->url() ?>" 
			    alt="<?php echo $link_item->link_text() ?>"
			    title="<?php echo $link_item->link_text() ?>"
			    >
			  <?php endif ?>
			</a>
		</li>
		<?php endforeach ?> 

  </ul>
</article>