Blog of Raivo Laanemets

Stories about web development, consulting and personal computers.

ImageMagick fixed-size no-padding undistorted thumbnails

On 2015-06-16

Many projects require fixed-size undistorted thumbnails without padding. This can be achieved with a single ImageMagick command using the special geometry switches.


convert input.jpg -resize 300x200^ -gravity center -extent 300x200 -strip output.jpg

This produces images that:

  • have the fixed size of 300x200;
  • have the maximal area taken from the center of the original image;
  • are not stretched out in any direction.

This is achieved by using the Fill Area Flag (^) in the -resize command. It resizes the image to completely fill the given dimensions. The image can overflow one of the dimensions. This is corrected with the -extent command which cuts off the overflowing sides. The -strip command removes the unnecessary metadata.

The original image can have dimensions smaller than 300x200. In that case, the input file is scaled to fit the required dimensions. No image stretching occurs.

The Fill Area Flag (^) requires ImageMagick version at least 6.3.8-3. The installed version can be checked with the convert --version command.