Crop() then thumb()?

I have two separated pieces of code that I’d like to remain separated, one uses crop() the other thumb(). But chaining these two does not seem to work, as in nothing seems to be returned from thumb().

Both crop() and thumb() supposedly returns Kirby\Cms\FileVersion | Kirby\Cms\File

My cropping code is similar to this, and it works standalone:

		<?php $portrait = $member->portrait()->toFile() ?>
		<?php $minSide = min($portrait->dimensions()->width(), $portrait->dimensions()->height()); ?>
		<img src="<?= $portrait->crop($minSide, $minSide, 'center')->url() ?>">

My thumbing code is similar to this and it also works standalone:

		<?php $portrait = $member->portrait()->toFile() ?>
        <img src="<?= $portrait->thumb(['width' => 1])->url() ?>">

But chaining both methods as below does not return any url or anything for that matter:

		<?php $portrait = $member->portrait()->toFile() ?>
		<?php $minSide = min($portrait->dimensions()->width(), $portrait->dimensions()->height()); ?>
		<img src="<?= $portrait->crop($minSide, $minSide, 'center')->thumb(['width' => 1])->url() ?>">

I am sure the ‘why’ is simple, but I can’t wrap my head around it.

Thank you

You cant chain like this because once you do an operation like crop or resize, the file becomes a file version and lands in the media folder. The next operation, the one chained, no longer has a src to work with because the previous process ended and produced a result in the media folder. So whan you chain the second one on to it, the conection with the orginal source file from the content folder is lost.

I hope that makes sense :slight_smile:

I see hmm…

So I have to merge both methods, and crop within thumb I guess?

Thank you

Yes, thats right, you need to use the crop option within thumb() or use something like the focusCrop plugin.

1 Like