Using ImageMagick & PHP to Resize / Crop Images to flickr style squares.

March 24th 2008

Here is a quick snippet of code I use frequently to get the flickr style square cropped thumbnails using php and imagemagick.


<?php

function sqThm($src,$dest,$size=75){
   
   list($w,$h) = getimagesize($src);

   if($w > $h){
      exec("convert ".$src." -resize x".$size." -quality 100 ".$dest);
   }else{
      exec("convert ".$src." -resize ".$size." -quality 100 ".$dest);
   }

   exec("convert ".$dest." -gravity Center -crop ".$size."x".$size."+0+0 ".$dest);

}

sqThm('gfx/tree.jpg','gfx/tree_squared.jpg');

?>

1,763 downloads

8 COMMENTS

Erica says...
This is exactly what I have been looking for. I have been trying to use GD lib for the longest time and it just hasn't been working for me. This is just so simple and easy to use. Thank you so much!
peeka-boo says...
awesome!! just awesome..
JERRY says...
i used something like this before.. image magick rocks
john says...
yes yes yes! use it love it
pooh says...
Thanks, this what I have been looking for!
Restaurant says...

very good, thank you!
Bernhard says...
Thank you, that was really useful.
Sandeep says...
awesome..........

ADD YOUR COMMENT

(optional)
(optional)
Use (Gravatar) for your avatars.