Google SEO | PHP | Mysql | Ajax | Jquery | Photoshop | Illustrator | Database | Web Design | Web Development | Sitemap |

In most of cases we need to display the thumbnail image in website. In this article am going to explain how to create the thumbnail image in PHP. I have already posted an article called Create thumbnail image dynamically using PHP where we are creating thumbnail on the fly.

Upload and create thumbnail image in PHP

The disadvantage of that type of thumbnail system is whenever you refreshing the page the thumbnail will generate freshly. But in this type of PHP thumbnail system we have to maintain two separate images will occupy more space.

Download FileDemo

//Image Storage Directory
$img_dir="images/";
$img = explode('.', $_FILES['thumb_img']['name']);
//Original File
$originalImage=$img_dir.'Original_'.$_FILES['thumb_img']['name'];
//Thumbnail file name File
$image_filePath=$_FILES['thumb_img']['tmp_name'];
$img_fileName=$img[0].time().'_Thumb.'.$img[1];
$img_thumb = $img_dir . $img_fileName;
$extension = strtolower($img[1]);

//Check the file format before upload
if(in_array($extension , array('jpg','jpeg', 'gif', 'png', 'bmp'))){

//Find the height and width of the image
list($gotwidth, $gotheight, $gottype, $gotattr)= getimagesize($image_filePath); 	

//---------- To create thumbnail of image---------------
if($extension=="jpg" || $extension=="jpeg" ){
$src = imagecreatefromjpeg($_FILES['thumb_img']['tmp_name']);
}
else if($extension=="png"){
$src = imagecreatefrompng($_FILES['thumb_img']['tmp_name']);
}
else{
$src = imagecreatefromgif($_FILES['thumb_img']['tmp_name']);
}
list($width,$height)=getimagesize($_FILES['thumb_img']['tmp_name']);

if($gotwidth>=124)
{
$newwidth=124;
}else
{
$newwidth=$gotwidth;
}
$newheight=round(($gotheight*$newwidth)/$gotwidth);
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight, $width,$height);
//Create thumbnail image
$createImageSave=imagejpeg($tmp,$img_thumb,100);

Here you need to change the default width of thumbnail image in $newwidth variable. The height of the image will calculate automatically. I did some calculation the get the new width like below.
$newheight = round (($gotheight*$newwidth)/$gotwidth);
By doing this the image quality and the size will be changed accordingly, and both the original image ans well as the thumbnail image will store in the images folder. If you want you can move the thumbnail image to separate folder. hope that this will help you.

Top Λ

Comments (3)

  • Niteen

    Could you please tell me how to create thumbnail of the bmp image?
    Friday-Nov-2013 | 04:01:26 PM
  • vivek

    Hi,
    good tutorial :)

    how to change the original image size??? to keep all the original image in same size to fit in the screen...
    Monday-Jul-2013 | 09:54:08 PM
  • Kasun

    Good tutorial thanks
    Monday-Feb-2013 | 12:04:40 PM

Post Comment

Name:
Email:
Divider