I\'m trying to create a Thumbnail Generator in PHP with GD that will take an image and reduce it to a fixed width/height. The square it takes from the original image (based on m
How about this: It's take an image of any size and proportionately resize it down (or up) to fill a canvas of any dimensions.
E.g. Source image is: 400w x 600h and thumbnail size needs to be 100w x 225h - the output image will be 100w x 225h and the images will be vertically centered with a width of 100 (the maximum) and height 150 (with a top and bottom border of 37.5 pixels)
Here's the function
function resize_to_canvas($filename,$canvas_w=100,$canvas_h=225){
list($width, $height, $type) = getimagesize($filename);
$original_overcanvas_w = $width/$canvas_w;
$original_overcanvas_h = $height/$canvas_h;
$dst_w = round($width/max($original_overcanvas_w,$original_overcanvas_h),0);
$dst_h = round($height/max($original_overcanvas_w,$original_overcanvas_h),0);
$dst_image = imagecreatetruecolor($canvas_w, $canvas_h);
$background = imagecolorallocate($dst_image, 255, 255, 255);
imagefill($dst_image, 0, 0, $background);
$src_image = imagecreatefromjpeg($filename);
imagecopyresampled($dst_image, $src_image, ($canvas_w-$dst_w)/2, ($canvas_h-$dst_h)/2, 0, 0, $dst_w, $dst_h, $width, $height);
imagegif($dst_image, $filename);
imagedestroy($dst_image);}
This function will replace the original file but is easily modified to create a new thumbnail image. Just change the file name the line imagegif($dst_image, $filename);