我正在尝试使用Nodejs的sharp软件包调整图像大小/转换为png格式,但不裁剪。根据文档,应该使用.max()方法,但令人遗憾的是,当调整大小为png时,该方法不起作用(图像被裁剪)。有什么解决方法吗?
我正在尝试使用Nodejs的sharp软件包调整图像大小/转换为png格式,但不裁剪。根据文档,应该使用.max()方法,但令人遗憾的是,当调整大小为png时,该方法不起作用(图像被裁剪)。有什么解决方法吗?
sharp()
.resize(400, 400, {
fit: sharp.fit.inside,
withoutEnlargement: true, // if image's original width or height is less than specified width and height, sharp will do nothing(i.e no enlargement)
})
inside
:将图像缩放至宽度和高度均不超过400,在不裁剪图像任何部分的情况下,也不添加黑色填充来调整输出图像的尺寸。嘿,如果你还在犹豫不决,这就是你可以做到的方法。
await sharp(postImage).resize(800, 900, {fit:"contain"}).toFile("/output/path");