PHP Thumbnail Generator
Currently I am working in PHP thumbnail generator, which basically creates thumbnail image of original image and store it in a particular folder. This functionality is very useful because some time we need to show thumbnail image to the user. Every time when we upload image we don’t want to try the actual height, width of the image. That’s why we have to use some code snippet which will generate the thumbnail image. Now follow these codes……
Suppose we will support only gif,jpeg,jpg and png.
$ext = end(explode(“.”,$name));
$name_arr = explode(“.”,$name);
$new_file_name = $name_arr[0].date(“Y-m-d-H-i-s”);
$new_file_name_with_ext = $new_file_name.”.”.$ext;
$add = “../upload/q_images/”.$new_file_name_with_ext; //original image folder directory
$upload_path = “../upload/q_thumb/”.$new_file_name_with_ext;//thumbnail image directory
$n_width=200;//width of the new image
$n_height=200;//height of the image
$error_msg = “”;
if($ext==”gif”)
{
$im=imagecreatefromgif($add);
$width=imagesx($im); // Original picture width is stored
$height=imagesy($im); // Original picture height is stored
$t_width = $n_width; $t_height = $n_height;
if($width>$height){
$ratio = $width / $height;
$t_width = $n_width;
$t_height = $n_height / $ratio;
}
if($width$height){
$ratio = $width / $height;
$t_width = $n_width;
$t_height = $n_height / $ratio;
}
if($width$height){
$ratio = $width / $height;
$t_width = $n_width;
$t_height = $n_height / $ratio;
}
if($width<$height){
$ratio = $width / $height;
$t_width = $n_width * $ratio ;
$t_height = $n_height;
}
$newimage=imagecreatetruecolor($t_width,$t_height);
imagecopyresized($newimage,$im,0,0,0,0,$t_width,$t_height,$width,$height);
imagepng($newimage,$upload_path);
chmod(“$upload_path”,0777);
}
else
{
$error_msg = “you have to upload gif,png,jpg,jpeg”;
}
return $error_msg;
}
Latest posts by Sahel Aktar
- SQL Query Optimization - September 21st, 2011
- How to create Google Charts - August 17th, 2011
- How to work with ckfinder and ckeditor incase of image upload - July 11th, 2011
- SQL Training - April 25th, 2011
- PHP Tips - October 10th, 2010
