Here's a function I've used with pretty good results. Resizes jpg's really nicely.
I think I got it from PHP.net's docs...
Code:
/*
Function createthumb($name,$filename,$new_w,$new_h)
creates a resized image
variables:
$name Original filename
$filename Filename of the resized image
$new_w width of resized image
$new_h height of resized image
*/
function createthumb($name,$filename,$new_w,$new_h){
//global $gd2;
$system=explode(".",$name);
if (preg_match("/jpg|jpeg/",$system[1])){$src_img=imagecreatefromjpeg($name);}
if (preg_match("/png/",$system[1])){$src_img=imagecreatefrompng($name);}
$old_x=imageSX($src_img);
$old_y=imageSY($src_img);
if ($old_x > $old_y) {
$thumb_w=$new_w;
$thumb_h=$old_y*($new_h/$old_x);
}
if ($old_x < $old_y) {
$thumb_w=$old_x*($new_w/$old_y);
$thumb_h=$new_h;
}
if ($old_x == $old_y) {
$thumb_w=$new_w;
$thumb_h=$new_h;
}
//if ($gd2==""){
//$dst_img=ImageCreate($thumb_w,$thumb_h);
//imagecopyresized($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
//}else{
$dst_img=ImageCreateTrueColor($new_w,$new_h);//create canvas picture
$thumbBgColor = imagecolorallocate($dst_img, 255, 255, 255);
imagefilledrectangle($dst_img, 0, 0, $new_w, $new_w, $thumbBgColor);
$xoffset = 0;
$yoffset = 0;
//calculate image offset to center the thumbnail
if ($new_w > $thumb_w){
$xoffset = (int) (($new_w - $thumb_w) / 2);
}
if ($new_h > $thumb_h){
$yoffset = (int) (($new_h - $thumb_h) / 2);
}
imagecopyresampled($dst_img,$src_img,$xoffset,$yoffset,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
//}
if (preg_match("/png/",$system[1])){
imagepng($dst_img,$filename);
} else {
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}
You can see how the thumbs look at this site's product pages -
Well Beings. There's a link from the thumbnails to popup a window with the original sized image.
-Jeffery
