question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.resize() crops image when crop is unnecessary

See original GitHub issue

I’m having trouble with resize function cropping sides of the image when only resize is needed:

// assume expressjs middleware
const resizeTransform = sharp().resize(/* either numbers or undefined-s from querystring */);
const downloadStream = request(req.query.src);
req.pipe(downloadStream).pipe(resizeTransform).pipe(res);

passing along width=200 produces this image: image

However passing 199 or 201 produces expected result: image-2

original: arnold-transformation-challenge-1-600x399-1

Seems like some strange bug to me. I was expecting to always get a downscaled image in the original aspect ratio. I couldn’t find anything in the documentation regarding controlling the crop. How does one achieves resizing this without cropping, and only having crop when both width and height are present?

I already tried converting to JPEG first, passing both dimensions and playing with .max, .min methods, no luck.

Running node v6.10.2, sharp v0.18.0

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
suprMaxcommented, May 31, 2017

Just checked with v0.18.1 and it works great!

0reactions
lovellcommented, May 30, 2017

These fixes are in v0.18.1 now available via npm. Please re-open if you’re still having related problems.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamically Resizing and Cropping Images - Cloudinary
Resizes the image to the specified dimensions without necessarily retaining the original aspect ratio.
Read more >
Image resize vs. crop - Solved - Kirby forum
resize() never crops an image, the image ratio is always preserved. Tobias_L: f not, what is the point in having the possibility to...
Read more >
Part 2: Scale and Crop Images - SERC - Carleton
Two common solutions are scaling the images down and cropping them to a smaller size. Scaling, Interpolation, and Spatial Calibration. Scaling ...
Read more >
Resize/crop/pad a picture to a fixed size - Stack Overflow
This function creates a thumbnail that is exactly as big as the size you give it. The image is resized to best fit...
Read more >
Resize, crop and other common transformations
In the max-size crop strategy, whole image content is preserved (no cropping), the aspect ratio is preserved, but one of the dimensions (height...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found