Search code examples
imageimagemagickimage-manipulation

How can I resize to fixed dimensions?


I searched a lot and I couldn't figure out how to achieve the following:

I have a news aggregation service so I am getting images of various dimensions. I have to create a 3 versions of the aggregated story image:

50x50, 150x100 and 278x209.

The dimensions must be absolutely what's mentioned above.

I currently have this:

convert {input} -resize {dimensions} {output}

But sometimes my specified dimensions are not being strictly adhere to, I don't know why.

I don't want to end up with deformed images either if I prevent images from scaling proportionally.

What are my options?


Solution

  • Since you have different ratios 1:1, 1.5:1 and 1.33:1.. you will either have to throw data away or create a canvas to paste the image on to.

    Crop to fit:

    convert <inFile> -resize 50x50^ -gravity center \
            -extent 50x50 <outFile>
    

    Fit within:

    convert -size 50x50 xc:white \( <inFile> -resize 50x50 \) \
            -gravity center -composite <outFile>