Image resize vs. crop

Hi!

Despite reading the reference https://getkirby.com/docs/guide/templates/resize-images-on-the-fly, I might misunderstand the resize and the crop function.

For instance, isn’t ->resize(500, 1000, 100) resizing an image of any size to 500x1000px and at the same time cropping off the unnecessary part of the image from the center? If not, what is the point in having the possibility to have both parameters set?

For instance, if I have a landscape image 3000x2000px and want to resize it to a portrait image with 500x1000px do I need to use ->resize(null, 1000, 100)->crop("center")?

If I just use ->resize() the filename actually changes to example-500x1000-q100.jpg but the actual pixel dimensions of the image stay the same.

I tried deleting the media folder too but it didn’t make a difference.

Thx!

resize() never crops an image, the image ratio is always preserved.

:woman_shrugging: (I doubt that that makes sense)

1 Like

Hm, thanks for the clarification. Yeah, I’m wondering why one should use both parameters at the same time to resize an image if setting only one parameter is resizing the image and preserving the aspect ratio anyways.

From the docs:

Bildschirmfoto 2020-07-15 um 16.50.28

So I guess I need a combination of resize and crop to achive the said result, or do you maybe know another trick?

Maybe the 200 means 200% quality, yeehaaa!

You cannot combine them, if you want to preserve the aspect ration use resize, if you want to crop, use crop.

1 Like

Ah ok, so cropping does also resizing not just cropping?

1 Like

Yes, exactly.

1 Like

Ah ok, thanks, I guess the function name "crop" was a bit confusing to me.

It’s still not working. Even using crop() instead of resize() the filename changes to the according crop size but the file itself stays the original size. :man_shrugging:

The actual code would be:
$example->images()->template("thumbnail")->first()->crop(375, 500, 100)->url()

Ah ok, the third parameter the crop function uses should be the crop origin not the quality %.

Keep in mind that your code is potentially error prone. Always test if you have an object before calling a method, in this case crop()- so your code should look something like this:

if ( $image = $example->images()->template("thumbnail")->first() ) {
 echo $image->crop(375, 500, 100)->url();
}

I would use https://getkirby.com/docs/reference/objects/file/thumb#example

and look there at the options for crop and size…

1 Like

You are free to use whatever you like. But since both crop() and resize() use the thumb() method internally there is absolutely no advantage in preferring thumb() over these shortcuts unless you want to use other options like blur etc. as well.

2 Likes