node.js - 使用node.js和Sharp包将非裁剪调整为png

标签 node.js png image-resizing sharp

我正在尝试使用 Nodejs Sharp 包调整图像大小/转换为 png,而不进行裁剪。根据文档,它是 .max() 方法,遗憾的是,在调整大小为 png 时,该方法不起作用(图像被裁剪)。有什么解决方法吗?

最佳答案

如果您使用sharp ,那么您可以使用达到您想要的结果

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 和高度 400 内,而不裁剪图像的任何部分,也不添加黑色填充 用于调整输出图像的尺寸
  • 从技术上讲,它会将图像大小调整为尽可能大,同时确保其尺寸小于或等于指定的尺寸。

关于node.js - 使用node.js和Sharp包将非裁剪调整为png,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47802542/

相关文章:

javascript - 如何压缩一系列PNG以供加载?

objective-c - UIImage 调整大小并裁剪以适合

image - 批量缩小 jpeg 图像的最快方法

python - 在python中调整图像大小算法

javascript - Node.js async.each - "callback was already called"

node.js - Node.js 还是 "bleeding-edge"吗?

node.js - Socket.io 通过 id 断开客户端

javascript - 无法将函数部署到 Firebase 函数

c# - 如何将 .png 文件用作 Windows 窗体中的按钮? (在 C#-visual studio 2013 中)

php - 使用 PHP 将 ICO 转换为 PNG 问题