【问题标题】:Resize image keeping aspect ratio and fit in bounds nodejs调整图像大小保持纵横比并适应边界nodejs
【发布时间】:2022-10-25 23:08:46
【问题描述】:
我需要调整图像大小以适合特定尺寸。我想保持纵横比。
例如
original image:
w:634
h:975
resize to max:
w:50
h:100
result:
w:50
h:85
我还没有找到可以做到这一点的任何东西(w 和 h 的计算)
我太笨了,无法自己弄清楚
副驾驶向我建议了一些只保持纵横比的东西
如果你想使用包。我更喜欢 jimp 进行图像编辑。
【问题讨论】:
标签:
node.js
image
image-resizing
aspect-ratio
【解决方案1】:
Jimp.read('image.jpg')
.then((lenna) => {
const ratio = lenna.getWidth() / lenna.getHeight();
const width = 375; // set the width you want
const height = width / ratio;
return lenna
.resize(width, height)
.quality(60)
.write('image.jpg');
})
.catch((err) => {
console.error(err);
});