To create a thumbnail image based on the image type is quite simple give a few key functions. The magic here is done by using the function imagecreatefromstring() instead of imagecreatefromjpeg() and other similar functions for other image formats.
The imagecreatefromstring() is not dependant on file type so any image type can be read into the script and a thumbnail canvas created and then the image string is “mapped” onto the thumbnail canvas with imagecreatetruecolor().
This example also provides variables for setting the thumbnail height to make galleries nice and tidy and also a variable for the thumbnail prefix. This allows the user to prepend an identifier for the thumbnail image. It can also be set to an empty string if it is not required.
The thumbnail filename itself is automatically generated from the original filename and the aspect ratio is also maintained with the use some simple math.
Finally the file is written to disk with the use of imagejpeg(), imagepng)(), imagegif(), or imagewbmp() depending on the file type. Some further checks could be added to make sure the permissions are correct to write the file, or perhaps a check to see if the file aready exists. These tasks are left to the user to decide.
<?php
error_reporting(E_ALL);
/*** the source image file to thumbnail ***/
$source_image = ‘spork.jpg’;
/*** maximum height ***/
$thumb_height = 40;
/*** thumb prefix ***/
$thumb_prefix = ‘thumb_’;
/*** the thumbnail quality ***/
$quality = 100;
/*** check if the file exists ***/
if(!file_exists($source_image))
{
echo ‘No file found’;
}
else
{
/*** supported types ***/
$supported_types = array(1, 2, 3, 7);
/*** get the image info ***/
list($width_orig, $height_orig, $image_type) = getimagesize($source_image);
/** check for supported type ***/
if(!in_array($image_type, $supported_types))
{
echo ‘Unsupported Image Type: ‘ . $image_type;
}
else
{
/*** get the filename ***/
$path_parts = pathinfo($source_image);
$filename = $path_parts['filename'];
/*** calculate the aspect ratio ***/
$aspect_ratio = (float) $width_orig / $height_orig;
/*** calulate the thumbnail width based on the height ***/
$thumb_width = round($thumb_height * $aspect_ratio);
/*** imagecreatefromstring will automatically detect the file type ***/
$source = imagecreatefromstring(file_get_contents($source_image));
/*** create the thumbnail canvas ***/
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
/*** map the image to the thumbnail ***/
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width_orig, $height_orig);
/*** destroy the source ***/
imagedestroy($source);
/*** write thumbnail based on file type ***/
switch ( $image_type )
{
case 1:
imagegif($im, $fileName);
$thumbnail = $thumb_prefix.$filename . ‘.gif’;
break;
case 2:
$thumbnail = $thumb_prefix.$filename . ‘.jpg’;
imagejpeg($thumb, $thumbnail, $quality);
break;
case 3:
imagepng($im, $fileName);
$thumbnail = $thumb_prefix.$filename . ‘.png’;
break;
case 7:
imagewbmp($im, $fileName);
$thumbnail = $thumb_prefix.$filename . ‘.bmp’;
break;
}
}
}
?>
Source: phpro.org



Posted in
Tags:
really great stuff…
hi pa………
really very useful site…..
i clarify my doubt from this site…
really super site
Are you from San Diego?
No, I am from chennai city, India. Keep Reading my blog. Daily one post will be written.
You know, I didn’t even see Casper’s comment when I made mine (how could I miss that?). Sorry Casper, you had it first!