How about this: It takes an image of any size and resizes it proportionally (or up) to fill the canvas of any size.
eg. Original image: 400w x 600h and the thumbnail size should be 100w x 225h - the output image will be 100w x 225h and the images will be vertically centered with a width of 100 (maximum) and a height of 150 (with an upper and lower border of 37.5 pixels)
Here is 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 will be easily modified to create a new thumbnail. Just change the file name to the string imagegif ($ dst_image, $ filename);
source share