Sharp: .resize() with .max() still crops the image

Created on 22 May 2014  ยท  11Comments  ยท  Source: lovell/sharp

Sorry to bother you again, dunno - maybe its something on my side. Given an image - for example this one

image

I would like to resize it to max 100x100 which means the resulting image should fit within those bounds while maintaing the aspect ratio (no cropping). Here's my call:

sharp(file).resize(100, 100).max().toBuffer(function(err, buffer) {
});

And the image I get is this one (resized and cropped to 57x100):

image

Shouldn't sharp simply resize input image to 57x100 without any cropping?

Thanks.

bug

Most helpful comment

Hmmm,
I tried on an OSX Mav with sharp 0.4.1, libvips 7.38 and both node 0.10/0.11.
Here's what i get
out

var sharp = require('sharp');
var fs = require('fs');

sharp('f2bc5c0e-e1ec-11e3-844f-28e9fded8f8b.png').resize(100, 100).max().toBuffer(function(err, buffer) {
  fs.writeFileSync('out.jpg', buffer);
});

What's your platform ?

All 11 comments

That definitely doesn't look right. The max() option was only added very recently.

@pierreinglebert are you able to help Przemek with this?

+1 I see similar behavior too.

I'll look at it.

Hmmm,
I tried on an OSX Mav with sharp 0.4.1, libvips 7.38 and both node 0.10/0.11.
Here's what i get
out

var sharp = require('sharp');
var fs = require('fs');

sharp('f2bc5c0e-e1ec-11e3-844f-28e9fded8f8b.png').resize(100, 100).max().toBuffer(function(err, buffer) {
  fs.writeFileSync('out.jpg', buffer);
});

What's your platform ?

It works flawlessly for PNGs but the problem is with JPEGs. This is the original image (JPEG) which fails as described in this issue:

http://upload.wikimedia.org/wikipedia/de/2/24/Burgon_Portrait.jpg

@pierreinglebert: I get the same results as you with that image, though I do see unexpected cropping with a different image. Perhaps Github is reencoding attachments and that changes something.

Here is a source image I can use to repro: https://mega.co.nz/#!UUNSCJxB!lBzVsklJX0VqtsOTYPi7NhGeOoZrqywMwPqUuLoMTIs

This is what the unexpected cropping looks like (100x100, max):
out2

My setup:

$ sw_vers -productVersion
10.9.3  # Mavericks

$ node --version
v0.10.27

$ npm list
...
โ”œโ”€โ”ฌ [email protected]

$ brew info vips
vips: stable 7.38.5
http://www.vips.ecs.soton.ac.uk/
/usr/local/Cellar/vips/7.38.5 (188 files, 13M) *
  Built from source with: --with-webp, --with-graphicsmagick
From: https://github.com/homebrew/homebrew-science/commits/master/vips.rb
==> Dependencies
Build: pkg-config โœ”
Required: fontconfig โœ”, gettext โœ”, glib โœ”
Recommended: libpng โœ”, jpeg โœ”, orc โœ”, libtiff โœ”, fftw โœ”, little-cms โœ”, pango โœ”, libexif โœ”
Optional: openslide โœ˜, imagemagick โœ”, graphicsmagick โœ”, openexr โœ˜, cfitsio โœ˜, webp โœ”

If you're seeing differing behaviour between PNG and JPEG images then my best guess would be that this is due to the use of libjpeg's shrink-on-load feature. If used, there's a recalculation of the further transformations that might need some max-specific logic.

Thanks, as always, for the fix @pierreinglebert .

@matylla and @ide are you able to verify the max logic in the master branch behaves as you expect? If so, I'll publish this fix to to npm. Cheers!

@lovell - the output from master looks correct to me:
out

Thank you guys for the prompt fix!

Yes, it looks correct on my side. Can u bump up the version and push to npm?

Thanks all - v0.4.2 now published to npm - https://www.npmjs.org/package/sharp

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zhump picture zhump  ยท  3Comments

emmtte picture emmtte  ยท  3Comments

henbenla picture henbenla  ยท  3Comments

Andresmag picture Andresmag  ยท  3Comments

AVVS picture AVVS  ยท  3Comments