Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: shrinkv: image has shrunk to nothing #1003

Closed
rexxars opened this issue Oct 24, 2017 · 6 comments
Closed

Error: shrinkv: image has shrunk to nothing #1003

rexxars opened this issue Oct 24, 2017 · 6 comments

Comments

@rexxars
Copy link
Contributor

rexxars commented Oct 24, 2017

Hi,

If you use an image that is 2000x4 and resize to 100px in width using img.max().resize(100), you will get this error: Error: shrinkv: image has shrunk to nothing.

I'm not sure if you consider this to be something that should be handled by the user before passing the size on to sharp, but I would have loved if it simply capped it at 1px in height to avoid the error.

Thoughts?

const sharp = require('sharp')

sharp('2000x4.png')
  .max()
  .resize(100)
  .toBuffer()
  .then(res => {
    console.log(res)
  }).catch(err => {
    console.error(err.stack)
  })
@lovell
Copy link
Owner

lovell commented Oct 24, 2017

Hello, this is an interesting scenario that has, ahem, cropped up briefly before in #767.

To support this we'd have to start ignoring the aspect ratio when either the x or y axis would reduce to <0.5px.

Feedback from other sharp users would be very much welcome here.

@rexxars
Copy link
Contributor Author

rexxars commented Oct 24, 2017

From a personal perspective, I've really enjoyed that sharp has really good default behavior in almost all cases I throw at it (thanks a ton!). This one caught me a little off guard.

The error doesn't give any stacktrace, and while the message was clear enough for me to eventually track down the error, I'd just love for it to give me a valid image regardless of whether the aspect ratio would be off.

For dynamic scripts, I feel having to calculate the expected output size would be a big hurdle. When implementing the different sizing methods, you would basically have to reimplement the calculations done internally in sharp (and keep them up to date with any changes), then check if any dimensions were less than 0.5px, and if so switch to a resizing mode that would ignore aspect ratio.

In other situations, perhaps you would want an error to be emitted, but if so, I would expect a clearer and more decorated error message.

So I'd either go with an option that set which behaviour to use (ignore aspect ratio on < 0.5px, or throw on invalid dimensions).

@rn4391
Copy link

rn4391 commented Nov 15, 2017

I would vote for a lower limit set at 1px for either dimension to accommodate for older screens. And beyond that limit to ignore the aspect ratio for the final resize.

@SebastiaanNijland
Copy link

It would be nice to have the ability to provide a flag when resizing an image, like preserveAspectRatio. If the flag is true, then resizing an image to nothing would result in an error. If the flag is false, then resizing is allowed to ignore aspect ratio, and no error would be thrown.

@lovell lovell added this to the v0.21.2 milestone Jan 3, 2019
@lovell
Copy link
Owner

lovell commented Jan 3, 2019

Commit 7c631c0 adds a couple of tests that highlight this problem along with the fix to ensure the length of the shortest edge never rounds below one pixel.

@lovell
Copy link
Owner

lovell commented Jan 13, 2019

v0.21.2 now available with this feature, thank you for the original suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants