One of the many things I love about the PHP scripting language is its support for image manipulation through the GD library. You want thumbnails? You got thumbnails. You want watermarks? You got watermarks. You want to create duplicates of 2000+ images in a folder with a slightly different filename, and a limited width/height? Use this script:
function resizeImage($source, $target, $width, $height){
$quality = 65;
$size = getimagesize($source);
// scale evenly
$ratio = $size[0] / $size[1];
if ($ratio >= 1){
$scale = $width / $size[0];
} else {
$scale = $height / $size[1];
}
// make sure its not smaller to begin with!
if ($width >= $size[0] && $height >= $size[1]){
$scale = 1;
}
$im_in = imagecreatefromjpeg ($source);
$im_out = imagecreatetruecolor($size[0] * $scale, $size[1] * $scale);
imagecopyresampled($im_out, $im_in, 0, 0, 0, 0, $size[0] * $scale, $size[1] * $scale, $size[0], $size[1]);
imagejpeg($im_out, $target, $quality);
imagedestroy($im_out);
imagedestroy($im_in);
}
foreach (glob("images/products/*_l.jpg") as $filename) {
$medium = str_replace("_l.jpg", "_m.jpg", $filename);
resizeImage($filename, $medium, 125, 100);
}
Notice the neat ‘glob’ function? That, my dear readers, is *nix brevity at its best.