node.jsimagemagickimage-resizinggm

Resize image to exact size by maintaining aspect ratio nodejs


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.


Solution

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

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

    Input:

    enter image description here

    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
    


    enter image description here