Search code examples
resizeimagemagickthumbnailsaspect-ratio

Using ImageMagick to Thumbnail an Image depending on Aspect Ratio


So I have done some research on this and I couldn't find an appropriate answer and wondered if there was even a way to do this:

Basically I need to create thumbnails for large files (adobe cs files, image files, videos, etc) after they are being uploaded to a website. I have done this part and it actually works great using some multithreading techniques to not make the user wait after upload is done.

Currently I am resizing all thumbnails to a square size of 250x250. This is the IM command:

convert -quiet examplefile.extn 
-thumbnail x250 
-resize 250x< 
-background white 
-gravity center 
-crop 250x250+0+0 
+repage 
-unsharp 0x.5 
-quality 80 
-flatten 
thumbnail.jpg

Looks awesome on videos and psd files ;) Ok so it turns out some website requirements have changed (dontya love it when that happens?) and now I need to resize the image based on their aspect ratios. The conditions are:

If height/width > 1, set the thumbnail WIDTH to 250 with freedom to height (max 500). If height/width <= 1, set the thumbnail HEIGHT to 250 with freedom to width (max 500).

Is this possible? Any pointers, suggestions, alternatives greatly appreciated as I continue to research myself...


Solution

  • The resize options are here: http://www.imagemagick.org/script/command-line-processing.php#geometry

    convert -quiet examplefile.extn -thumbnail 250x250 +repage -unsharp 0x.5 -quality 80 thumbnail.jpg
    

    Your code seems a bit over complicated - try the above which will keep the aspect ratio and the maximium dimension will be 250px.

    If you want to do something more complicated I would do it outside IM and pass the width and height dimension to the command as variables.

    -thumbnail {$width}x{$height}