Kirby

Kirby 3 Alternative to $thumb->dataUri()

Hello :slight_smile: I’m somewhat stuck with a feature of my website I want to upgrade from Kirby 2 to v3.

Intro
I’m a photographer and I have a built in client release form inside any of my clients gallery. The idea is, that I make an selection of images I want to release to the public and the client can login and edit this selection and finally fill out a release form, which will generate a pdf contract. This is where I’m stuck.

The form is posting to the corresponding controller, which handles form submission an loads up the HTML2PDF library (https://html2pdf.fr) for generating the pdf. The pdf contains the form inputs as well as the selected images listed in columns.

How I did it in v2

Since pasting the thumb url as image source inside the pdf generation seemed to be way to slow (talking about 60 to 200 pictures), I switched to using the dataUri function in Kirby 2 which worked perfectly.

thumb($selectedimage, array('width' => 300,'height' => 300, 'crop' => false, 'quality' => 40))->dataUri()

This has been deprecated in v3 so I used the Toolkits pendants F::uri() (or F::base64()) with the root path of the thumbnail. Unfortunately, the thumb is not yet created when trying to encode it to base64, returning an empty string.

What I’m trying out now

Thumb settings
$thumb = $selectedimage->thumb(['width' => 200,'height' => 200, 'crop' => false, 'quality' => 40]);

$thumb->url() does work when opening it with the browser or importing it in pdf (but is very slow)

dump(file_get_contents($thumb->root())); Returning error file not found
dump(F::uri($thumb->root())); returning empty base64 string

Is there a way I can get the source of the thumbnail in base64 encoding or force-generate those thumbnails?

Thanks in advance!
Justus

I think the problem is that you are trying to work with a thumb once it has being created. It’s not normally possible to further manipulate a thumb after the first shot. You need to pick up the resultant thumbnail and work with that.

I think the easier way would be to use a hook to make a copy of the image of the correct size when you upload, so that you have a correctly sized image already in the content folder (you can filter this out in the panel so you dont end up with a untidy panel).

Then you can pick that up and base64 it…

$thumb = 'data:image/jpeg;base64,'.base64_encode($thumb)
1 Like

Well an upload hook is a great idea. Haven’t thought about it! I will have a look into those hooks.

Hooks are awesome. File create after is probably the one you want. It would only take a couple of lines to manipulate it, and write it back. The autoresize plugin hook is almost what you want… just alter it slightly so it keeps the original.

Quick followup:

As a workaround for the deprecated dataUri function, you could use the build-in kirby()->thumb('old root', 'new media root', ['width' => 200, 'height' => 200, 'quality' => 40]); to generate a thumbnail and write it to the media root. The function returns the path, so you could then base64 encode the file.

Generating those thumbs via Hook on upload is obviously faster.