My PHP script to automatically create cropped square thumbnails (like flickr does).
Making Square Thumbnails With ImageMagick
The command I use for 100 pixel square thumbnails is (all on one line):
convert input.jpg -thumbnail x200 -resize '200x<' -resize 50% -gravity center -crop 100x100+0+0 +repage -format jpg -quality 91 square.jpg
Below is a PHP script to generate square thumbnails from all images in a directory.
#!/usr/local/php5/bin/php
<?php
$SQUARESIZE = 108; // the size in pixels for your square images
$convertPath = "/usr/local/bin/convert"; // full path to ImageMagick's convert tool
$path = getcwd()."/"; // path to folder full of jpg files to convert
$n = 0;
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && strpos($file,"jpg") !== false) {
echo $file."\n";
$originalname = $file;
$filenamePart = $n++;
$squarename = $filenamePart."_sq.jpg";
$squareCommand = $convertPath." \"".$path.$originalname."\" -thumbnail x".($SQUARESIZE*2)." -resize '".($SQUARESIZE*2)."x<' -resize 50% -gravity center -crop ".$SQUARESIZE."x".$SQUARESIZE."+0+0 +repage -format jpg -quality 91 \"".$path."sq/".$squarename."\"";
system($squareCommand);
}
}
closedir($handle);
}
?>
