Thumbnail from Image URL

Hi everyone! I’d like to pick several images from different folders, so I’m using the Controlled List plugin to create checkboxes in the panel. The callable looks like this:


class ScreenshotCollection {
    static function screenshotlist($field) {
        $kirby = kirby();
        $site = $kirby->site();
        $pages = $site->pages();
        $screenshotPage = $pages->find('screenshots');
        $games = $screenshotPage->children()->visible();
        $screenshots = new Collection();
        foreach ($games as $g) {
         foreach ($g->images() as $i) {
             $screenshots->data[] = $i;
        $screenshots = $screenshots->filterBy('ratio', '>=', '1.5')->sortBy('modified', 'desc')->limit(16);

        $result = array();

        foreach ($screenshots as $screenshot) {
              $result[$screenshot->url()] = $screenshot->title() . ' / ' . $screenshot->page()->title();
            } else{
              $result[$screenshot->url()] = $screenshot->name() . ' / ' . $screenshot->page()->title();

        return $result;

After that, I’d like to get a random image from this list and create a thumbnail for it . Currently the code looks like this:

<?php $screenshotlist=page('screenshots')->featuredscreenshots()->split();$key=array_rand($screenshotlist);$screenshot=$screenshotlist[$key];?>

Now, for the problem: Since $screenshot only outputs the URL (as specified in the callable), I can’t seem to use thumb to create a thumbnail. Is there anything I can do to save the $result in a way that allows me to do this? Any help would be greatly appreciated!

One way would be to store the diruri instead of the URL and then create a new Asset

$image = new Asset('content/'.$screenshot);
echo thumb($image, ['width' => 200]);

You could also store the file uri, and then get the image in the same way as in the KirbytagAbstract Class, wrapped in a function. I’d actually prefer this way.

function getFileFromUri($uri) {

    $path     = dirname($uri);
    $filename = basename($uri);

   if($page = page($path) and $file = $page->file($filename)) {
     return $file;
   } else {
     return false;

$image = getFileFromUri($screenshot); // where screenshot is now the file uri
if($image) {
  echo $image->resize(200);

If you have to store the URL, you have to get the path from the URL.

1 Like

Thank you for the quick answer, both methods worked perfectly. I’ll probably use the second one though, seems to be the more “proper” way to do this.