Resize image to exact size by maintaining aspect ratio nodejs

你。 提交于 2021-01-28 09:21:00

问题


I want to resize image to an exact size by maintaining aspect ratio and filling the empty space with transparency using nodejs.

I am able to do it using canvas. But due to some server os issues I can not use canvas.

I tried with imageMagick and gm. But couldn't find any option like these. Please show me a way to do this.

Thanks in advance.


回答1:


In ImageMagick, you can resize and fill to the exact size by

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

Input:

Here I will make the background black so you can see that if fills it out.

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





回答2:


In case you have been successful using the canvas for resizing images, you can check out https://github.com/Automattic/node-canvas this repo.

As already mentioned you also resize images using ImageMagick by processes in NodeJS ( http://www.imagemagick.org/Usage/resize/ )

convert dragon.gif  -resize 64x64  resize_dragon.gif

In case you have a lot of images, I would suggest that you write a terminal script ( NodeJS can achieve that as well ).

I hope it helps, in case you have more queries, feel free to ask.



来源:https://stackoverflow.com/questions/53442060/resize-image-to-exact-size-by-maintaining-aspect-ratio-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!