Cropping an image without upscaling?

Hey everybody,

building my first Kirby project and stumbled upon a strange behavior: When I set width, height and crop the images are beeing upscaled. Is there any way to prevent this?

thumb($file, 
    array(
        'width' => 555,
        'height' => 376,
        'crop' => true)
    )

Thanks for your time :clap:

Hello @alexanderhahn
What’s the size of the image you’re trying to crop ?

Sometimes the images are smaller, sometimes bigger.
My question regards the smaller ones.

My test image is 354 x 266

A workaround could be an If statement to check If the image is too small?

Or crop and then resize back to a smaller size again.

There is an upscale option for exactly that and it is set to false by default. So if it doesn’t work, then that’s a bug. I will look into this tomorrow.

On my local XAMPP on Windows the default (upscale option) works.

@alexanderhahn:

  • Did you change some defaults of Kirby?
  • What is your webserver, his OS, your provider and your changes to its defaults?

@lukasbestle thanks. I tried to set the upscale option (even if it is already false) but couldn’t stop the upscaling.

@anon77445132 It’s a fresh install of XAMPP on a untouched Windows 10. The behavior is the same on my clients webserver (PHP 5.4).

Just to check the obvious, which version of Kirby are you using ?

1 Like

Version is 2.3.1 :robot:

1 Like

Maybe you upscale your images with CSS by mistake?

Have you tried cropping via the crop() method yet? If so, do you get the same effect?

$file->crop(555, 376);

Looks more elegant but has the same upscale.

@jenstornell no, they are 555 pixels.

Ok, thanks for testing :slight_smile:. @lukasbestle promised to look into this, so let’s wait for his feedback, then.

1 Like

I have tested this and this is not a bug, it’s intended behavior:

The upscale option worked for me for the resize operation, but not for the crop operation. The reason is that resizing an image always keeps the aspect ratio, so if you don’t upscale a small image, it doesn’t matter because the aspect ratio of the small image and the upscaled image would be the same. This is why Kirby doesn’t upscale in this case.

Cropping however is more difficult as it changes the aspect ratio, so upscaling is required to reach exactly the intended size and aspect ratio (I made a temporary fix and even pushed it already but then deleted it because the result was always a few pixels off).

We might reconsider this for a future release, but for now you should resize the images only if you don’t want upscaling.

@alexanderhahn:

Or you code an “if … else …” to separate this two cases in your code…

Good luck!

What exactly would that help with? If it was that easy, we could also do it in Kirby directly. :slight_smile:

@lukasbestle I worked too long with TYPO3 but they got a global setting “do not upscale” and when you set a crop on an image it only touches the image if it is too big (either width or height).

For my case, I let it rest because it is a very small site and in the future, images will be much bigger (old data from the old website caused this problem).

I agree with @anon77445132

Although it’s not perfect, you could check if the height or the width is smaller than the crop with something like:
*Assuming $file is an image object

<?php 
$width = 555;
$height = 376;
if $file->width() > $width AND $file->height() > $height {
	$file = $file->crop($width,$height);
}; ?>
<img src="<?= $file->url() ?>" />

*NOTE: Updated code. Thanks Sonja

I’m not sure this will work. What if the width is > $width, but the height isn’t? And shouldn’t it be “>” not “<”?

That might be a temporary fix but please note that (as I wrote above) not cropping the image at all will lead to layout issues as the images won’t have the expected aspect ratio.