Sharp: Resize if image is bigger than a specific size

Created on 22 Dec 2017  Â·  10Comments  Â·  Source: lovell/sharp

With ImageMagic you can run:

mogrify -resize '800x800>' *

Which will resize a file only if its width OR height is bigger than 800. The image's original aspect ration will also be maintained. Is there such a "shortcut" option for sharp? Or, should I call metadata() first and then resize accordingly, depending on what I get?

question

Most helpful comment

sharp().withoutEnlargement().resize(800, 800)...

All 10 comments

sharp().withoutEnlargement().resize(800, 800)...

Do not enlarge the output image if the input image width or height are already less than the required dimensions. This is equivalent to GraphicsMagick's > geometry option: "change the dimensions of the image only if its width or height exceeds the geometry specification".

My most humble apologies... I thought I had read the whole -- I must have missed the very last bit, which had exactly what I needed.
Really, sorry... and thanks for the fantastic library!

One note: I had success with:

withoutEnlargement().resize(800, 800).max()

Without that .max(), an image that is normally 1600x3200 is resized to 800x800 rather than 400x800 (which is what I want, and that's what mogrify -resize '800x800>' does

Interesting observation. It's reproducible with sharp 0.18.4:

$ curl -s0 'https://static.zerochan.net/Kouki.%28Pok%C3%A9mon%29.full.2038439.jpg' |\
  node -e "process.stdin.pipe(require('sharp')().withoutEnlargement().resize(800, 800)).pipe(process.stdout);" |\
  gm identify -verbose - | grep Geometry
  Geometry: 800x800

... and the image is centre cropped.

Whereas adding .max() fixes it:

$ curl -s0 'https://static.zerochan.net/Kouki.%28Pok%C3%A9mon%29.full.2038439.jpg' |\
  node -e "process.stdin.pipe(require('sharp')().withoutEnlargement().resize(800, 800).max()).pipe(process.stdout);" |\
  gm identify -verbose - | grep Geometry
  Geometry: 400x800

Seems like a bug in withoutEnlargement, or at least a surprising, undocumented side effect.

Hmm, wait, the resize docs say:

Resize image to width x height. By default, the resized image is centre cropped to the exact size specified

... so this is per design and has nothing to do with withoutEnlargement(). I'm sorry for the confusion. It appears that I've hardwired .max() when a resize() is performed in my own library, and thus the confusion.

I do believe that a bounding box resize would be a better default, but it's probably too late to change that now :)

@lovell Maybe this behaviour should be documented in the docs...? Also, maybe you should document the mix of withoutEnlargement() and max() as together they do what GraphicsMagick's > geometry option does...

PRs to improve the JSDocs always happily accepted.

Done

On 24 December 2017 at 04:01, Lovell Fuller notifications@github.com
wrote:

PRs to improve the JSDocs always happily accepted.

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/lovell/sharp/issues/1075#issuecomment-353745208, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ACB7Xs7L-2ROsDstJda2V13avrxZzYVHks5tDVwXgaJpZM4RLI7F
.

deprecated max() for { fit: sharp.fit.inside }.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tomercagan picture tomercagan  Â·  3Comments

terbooter picture terbooter  Â·  3Comments

zhump picture zhump  Â·  3Comments

natural-law picture natural-law  Â·  3Comments

emmtte picture emmtte  Â·  3Comments