lwip icon indicating copy to clipboard operation
lwip copied to clipboard

Resize with one dimension (that keep proportions)

Open gkorland opened this issue 10 years ago • 4 comments

Resize with one dimension (that keep proportions)

gkorland avatar Dec 23 '14 12:12 gkorland

I've ended with this small util functions: https://gist.github.com/ivan-kleshnin/9f63a8d3306efc046157

ivan-kleshnin avatar Jan 03 '15 12:01 ivan-kleshnin

+1

noducks avatar Feb 03 '15 21:02 noducks

You can use scale. You just need to calculate ratio

// image will be resized to fit the 1024x768 box
var maxW = 1024,
    maxH = 768,
    ratio = Math.min(maxW / image.width(), maxH / image.height());

// resized to be in square 100x100
var ratio = 100 / Math.min(image.width(), image.height());

// if you need it be applied only for downscaling
ratio = ratio < 1 ? ratio : 1;

image.scale(ratio)

enagorny avatar Feb 16 '15 12:02 enagorny

This seems to work. It feels uncomfortable to resize without pixel dimensions and there isn't a way to do it in a batch operation.

let width = 960;
let ratio = width / image.width();
image.scale(ratio, (err, image) => { console.log('done'); });

Kequc avatar Jun 01 '16 22:06 Kequc