【问题标题】:Resize image to exact size by maintaining aspect ratio nodejs通过保持纵横比nodejs将图像调整为精确大小
【发布时间】:2018-11-23 07:07:26
【问题描述】:

我想通过保持纵横比并使用 nodejs 用透明度填充空白空间来将图像调整为精确大小。

我可以使用画布来做到这一点。但由于一些服务器操作系统问题,我无法使用画布。

我尝试使用 imageMagick 和 gm。但是找不到像这样的任何选项。请告诉我一个方法来做到这一点。

提前致谢。

【问题讨论】:

    标签: node.js imagemagick image-resizing gm


    【解决方案1】:

    在 ImageMagick 中,您可以调整大小并填充到确切的大小

    convert image -resize WxH -background none -gravity center -extent WxH output
    

    输入:

    这里我将背景设置为黑色,这样你就可以看到如果填充它。

    convert lena.jpg -resize 400x300 -background black -gravity center -extent 400x300 lena1.jpg
    


    【讨论】:

    • 改变背景颜色,如果你愿意,比如白色,代替黑色填充。
    【解决方案2】:

    如果你已经成功使用画布调整图像大小,你可以查看https://github.com/Automattic/node-canvas这个repo。

    如前所述,您还可以通过 NodeJS 中的进程使用 ImageMagick 调整图像大小 (http://www.imagemagick.org/Usage/resize/)

    convert dragon.gif  -resize 64x64  resize_dragon.gif
    

    如果您有很多图像,我建议您编写一个终端脚本(NodeJS 也可以实现)。

    希望对您有所帮助,如果您有更多疑问,请随时提问。

    【讨论】:

      猜你喜欢
      • 2012-10-02
      • 2013-06-26
      • 2012-04-15
      • 2012-05-01
      • 2012-11-15
      相关资源
      最近更新 更多