lovell / sharp

High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP, AVIF and TIFF images. Uses the libvips library.
https://sharp.pixelplumbing.com
Apache License 2.0
29.33k stars 1.3k forks source link

Image resizing prioritizes image ratio higher than resize target dimensions #4226

Closed BernhardBehrendt closed 1 month ago

BernhardBehrendt commented 1 month ago

Question about an existing feature => resize(number, number, { fit: 'inside' })

Using the resize method to scale down an image into a target format

Existing and occurring dimensions written on filenames.

import sharp from 'sharp';

sharp('input-image-1650×1275.jpg')
  .resize(610, 854, { fit: 'inside' })
  .toFile('output-image-610×471.jpg');

What are you trying to achieve?

Keeping the desired dimension:

The resulting output image is 610 × 471 and desired taget height of 854 is not applied as value for the down sized image. I think especially with fit:insideoption set that the output image has a size of 610x854 with the image having a size of 610x471 vertically centered. Is this somehow achievable? Thanks very much for any guidance :-)

Please provide a minimal, standalone code sample, without other dependencies, that demonstrates this question

The code above is a working example that can be reproduced by e.g. using this image: Sample Image

lovell commented 1 month ago

Did you see https://sharp.pixelplumbing.com/api-resize#resize ? What you describe sounds more like fit: contain.

BernhardBehrendt commented 1 month ago

Thanks yes using fit contain it's working. Had another issue that made it appear contain was the wrong solution. Thank you so much for pointing me to the right direction.