Page Model Not Loaded via "page()" Helper

Hi all,

I have created a page model for an ‘article’ page. This model adds a function, which automatically generates a square preview version of the article’s main image. The simple code is as follows:

class ArticlePage extends Page {

	public function square_img() {
		$img = image($this->main_image());
		$options = array(
			'width' => 330,
			'height' => 330,
			'crop' => true
		return thumb($img, $options);


In the article template, I can access $article->square_img() without any problems, and it all works like a charm.

Now, the problem I’m finding is that at the end of every article, I have a list of “related articles”. That list is kept as a structured field - each entry containing just a ‘page’ field, titled ‘article_uri’. When I display this list at the end of my article, I’d like to show the “square_img” of all related articles. I’m trying to do this in a foreach() loop, where I’m creating a page using the page() helper, and then trying to fetch the square_img() for that article page:

    foreach ($page->related_articles()->toStructure() as $article): 
    $related = page($article->article_uri());
    <a href="<?php echo $related->url() ?>">
        <h3><?php echo $related->title()->html() ?></h3>
        <img src="<?php echo $related->square_img()->url() ?>">
<?php endforeach; ?>

It fetches $related->url() and $related->title() without any problems, but when it tries to fetch $related->square_img() it generates a fatal error:

Fatal error: Uncaught The given image is invalid thrown in [...]/kirby/toolkit/lib/thumb.php on line 68

It seems to me, that when I fetch each article in my foreach loop using the page() helper, Kirby is NOT loading the required page model. This doesn’t seem right, as according to the docs, it should be.

The workaround is to put all the thumb code inside the template, creating the appropriate image objects from manually-composed uris inside the loop. This, however, would really be better placed inside the model for the article - keeping the template simpler, which is the whole purpose of the model…

Am I missing something, or is this a bug?..

If you change this line:


$img = $this->main_image()->toFile();
it should work.

The problem is not the page helper but the object that is passed to the thumb method (not sure why, though).

The image helper needs the full path including the page URI. Your main_image field probably only contains the filename, so it will only work if the page is the current page (Kirby will default to the current page if only a filename is given), but if you use the page method on another page, this information is missing. So if you use the code @texnixe posted, it should always work as the connection to the page will be there.

Thank you @texnixe and @lukasbestle! Indeed, the problem was that the helper function image() requires a full uri to the image file, while the function $page->image() assumes the page’s uri as the base location. I tried the following, and it works:

    $img = $this->image($this->main_image());

Once again, many, many thanks!