Passing a value to crop()

Hello - I’m a PHP newb and hacking my way thru, but this one is throwing me a bit -

I have a page where I’m displaying cover images from all the pages in that section of the site. I want to crop each image to a 2:1 ratio(easy enough), but I also want to be able to have each page(or image) tell the crop() where to position the crop - say either top, center or bottom.

So I’m calling the image with:

<?= $post->image()->crop(600, 300, 'center')?>

this works fine…but I want to have the position argument lookup a field in the $post, something like:

<?= $post->image()->crop(600, 300, $post->mycropposition())?>

…I’m just not sure how to call mycropposition as an argument in this example.

also - is it not possible to chain image()->resize()->crop()? I’ve tried, but my images go white.

Many thanks -

Hello and welcome to our forum!

Yes, you can do that. You might have to use the field value instead of the field object as argument:

<?= $post->image()->crop(600, 300, $post->mycropposition()->value()) ?>

No, that’s not possible. Resize(), crop() etc. are File class methods. When called on a file, they return a FileVersion object, on which you cannot call a File method.

And what would be the purpose of calling crop on top of resize, since crop already does the resizing?

Thank you!

the resize to crop was due to the full res of the images being 1k square. I wanted to generate a thumbnail (of sorts) for this particular page, so I was looking for Kirby to resize the images down to 600x600, then crop vertically to 600x300 (with the crop position being set by a field value…)

You could generate a thumbnail of 600X600 which you then store as a normal file in the folder. This can then be cropped again. Similar to what this plugin does:

thanks, I will check this out. Appreciate the help!