2

In Lightroom, I can resize images to a pixel value for the long side. That way if, for example, an image is five times as tall as it is wide, and I asked for a 2,000px (long side) image, the result will be 2000x400px. Not 10,000x2,000px.

In other words, the aspect ratio is maintained, and 2,000px is a limiter.

Can sharp do this, and maintain the aspect ratio -- when I don't know in advance whether the height or the width will be greater?

Otherwise I will have to pull the exif data from the images so that I know in advance which is greater, and can resize based on the greater side (width or height). I am trying to avoid this step.

Below is my current code and it creates 300x300 images, ignoring the aspect ratio. However the desired result is:

300x200 images, when given images whose aspect ratio is 3:2

200x300 images, when given images whose aspect ratio is 2:3.

etc.

exports.resizeResolutions = {
    mapThumbnail: {x: 300, y: 300},
}
exports.fitMethods = {
    inside: "inside",
}
        proc = sharp(path.join(pathToLocalFSGalleries, folder, file),
            { fit: fitMethods.inside }).resize(resizeResolutions.mapThumbnail.x, resizeResolutions.mapThumbnail.y);
        await proc.toFile(path.join(pathToThumbnails, folder, file));
icor103
  • 193
  • 1
  • 8
  • Have you read the dos on [Resizing Images](https://sharp.pixelplumbing.com/api-resize#resize) with Sharp? – Metro Smurf Jan 22 '23 at 01:28
  • @MetroSmurf I did, and it seems that 'inside' should accomplish what I want. But it doesn't. 3:2 images end up being 1:1 and cropped. – icor103 Jan 22 '23 at 01:34
  • You'll need to use the image metadata to retrieve the width/height, then use the largest value to set either `.x` or `.y`, but not both; the other value should be `null` or `undefined` to preserve the aspect ratio. I'd experiment w/ `contain` and `inside`. By providing both x and y, Sharp will size to those dimensions. – Metro Smurf Jan 22 '23 at 01:42
  • 1
    In other words, the very step that I said I'm trying to avoid isn't avoidable. Okay, I'll do it that way. – icor103 Jan 22 '23 at 01:47

0 Answers0