function thumb_makethumb() {
switch($_FILES['image_file']['type']){
case 'image/png':
case 'image/x-png':
$type = 'png';
break;
case 'image/jpeg':
case 'image/pjpeg':
$type = 'jpg';
break;
case 'image/gif':
$type = 'gif';
break;
//case 'image/tiff':
//$type = 'tif';
//break;
//case 'image/x-photoshop':
// $type = 'psd';
// break;
default:
$type = 'none';
break;
}
// Make temporary file name and location a variable
$temp_file_name = '$_FILES['image_file']['name'];
// Get image attributes of temporary file
$image_attribs = getimagesize($temp_file_name);
// Make temporary file a resource
if($type == 'png')
{
$im_temp = imageCreateFromPNG($temp_file_name);
}
elseif($type == 'jpg')
{
$im_temp = imageCreateFromJPEG($temp_file_name);
}
elseif($type == 'gif')
{
$im_temp = imageCreateFromGIF($temp_file_name);
}
// Set max width and height. Largest dimension will be set the this size either by
// shrinking or enlarging the image
$th_max_width = 150;
$th_max_height = 150;
// Find ration of dimensions
$ratio = ($image_attribs[0] > $image_attribs[1]) ? $th_max_width/$image_attribs[0] : $th_max_height/$image_attribs[1];
// Scale to max width/height using ratio
$th_width = $image_attribs[0] * $ratio;
$th_height = $image_attribs[1] * $ratio;
// Create a true colour image and use anti-aliasing
$im_new = imagecreatetruecolor($th_width,$th_height);
imageAntiAlias($im_new,true);
// Set thumb folder and file name
$th_file_name = 'thumbs/' . $_FILES['image_file']['name'];
// And copy the original image resized onto the new thumbnail.
imageCopyResampled($im_new,$im_temp,0,0,0,0,$th_width,$th_height, $image_attribs[0], $image_attribs[1]);
// Output to file/browser as a PNG. Can also output as JPEG using the imageJPEG() function.
header("content-type: image/png");
imagePNG($im_new);
// Clean up image resource
imagedestroy($im_new);
}
?>
This function can be called to act on an uploaded image and output a thumbnail of it to the browser. Can only operate on GIF, PNG and JPEG files as these are the only filetypes the GD library can work with. Requires the GD library to be installed on the webserver.