.resize() crops image when crop is unnecessary
See original GitHub issueI’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:

However passing 199 or 201 produces expected result:

original:

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:
- Created 6 years ago
- Comments:6 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Just checked with v0.18.1 and it works great!
These fixes are in v0.18.1 now available via npm. Please re-open if you’re still having related problems.