lwip
lwip copied to clipboard
Resize with one dimension (that keep proportions)
Resize with one dimension (that keep proportions)
I've ended with this small util functions: https://gist.github.com/ivan-kleshnin/9f63a8d3306efc046157
+1
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)
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'); });