What can i get cropped-image-preview-urls on pages with draft-status?

Hi,

I am trying to create a cropped image via the api to display it within a custom image block.

It works fine for published and hidden pages, but for draft-pages it seems that the ‘parent’ is missing and I get the following error:

GET https://website.test/api/get-clipped-image?page=team/hans-dieter&id=original.png&width=1125&height=457&top=125&left=0&uuid=t21JFk9GiFWQIWDj 500 (Internal Server Error)

{
    "status": "error",
    "message": "The property \"parent\" is required",
    "code": 500,
    "exception": "Exception",
    "key": null,
    "file": "Properties.php",
    "line": 132,
    "details": [],
    "route": "get-clipped-image"
}

I am using the findPageAndDraft() method in my route:


'api' => [
    'routes' => function ($kirby) {
        return [
            [
              'pattern' => 'get-clipped-image',
              'action' => function() use ($kirby) {
                $page = get('page');
                $id = get('id');
                $clip = [
                    'width' => (int)get('width'),
                    'height' => (int)get('height'),
                    'top' => (int)get('top'),
                    'left' => (int)get('left')
                ];
                $filename = basename($id);
                $site = $this->kirby()->site();
                $parent = $site->findPageOrDraft($page);
                $file = new File([
                  'filename' => $filename,
                  'parent' => $parent
                ]);
                $thumb = $file->thumb([
                  'width' => 666,
                  'clip' => $clip,
                ]);
                return [
                  'src' => $thumb->url(),
                ];
              }
            ],
       ...

What can i do to get the cropped-image-preview-url on pages with draft-status?

Best
Peter

Try $kirby->page($page) instead of $site->findPageOrDraft($page). And if you already have $kirby defined, not need to get the site object via $site = $this->kirby()->site(); again.

1 Like

Thank you @texnixe!