• How to create thumb image with PHP

    In this tutorial, I will explain you how to create thumb image with PHP. Below following code should help beginner PHP developers who are looking for an easy way to Create thumb image with PHP.

    Let’s understand this from the start:
    You need to create following structure to run this script.
    Structure:
    Index.php (File)
    thumbnailCreator.php (File)
    Images (Folder)

    Code For index.php:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>How to create thumb image with PHP</title>
    </head>
    <body>
    <?php
    if(!isset($_GET['message']) && !isset($_GET['fileType']))
    {
    ?>
    <div>Error!! File Format not Supported.</div>
    <?php
    }
    ?>
    <?php
    if(!isset($_GET['thumb']) && isset($_GET['original']))
    {
    ?>
    <div>Photo uploaded and Thumbnail created Successfully !!</div>
    <?php
    }
    ?>
    
    <div style="width:730px; margin:0 auto;"> </div>
    <div>
    <div>
    <table width="100%" border="0" cellpadding="2" cellspacing="2">
    <tr>
    <td align="left" valign="middle" bgcolor="#008000"><div style="margin:0px 10px; font-weight:bold; color:#FFF; font-size:16px;">Upload and create thumbnail image using PHP</div></td>
    </tr>
    <tr>
    <td align="center" valign="middle"><div style="margin:0 auto; padding:8px 4px; text-align:center; width:460px;">
    <form action="thumbnailCreator.php" method="post" enctype="multipart/form-data">
    <table width="100%" border="0" cellspacing="2" cellpadding="2">
    <tr>
    <td width="28%" align="left" valign="middle"><strong>Browse Image</strong></td>
    <td width="72%" align="left" valign="middle"><label>
    <input type="file" name="thumb_img" id="thumb_img" />
    </label></td>
    </tr>
    <tr>
    <td align="left" valign="middle"> </td>
    <td align="left" valign="middle"><label>
    <input type="submit" name="uploadImage" id="uploadImage" value="Create Thumbnail Now" />
    </label></td>
    </tr>
    <tr>
    <td colspan="2" align="left" valign="middle"><strong>
    <?php
    if(!isset($_GET['thumb']) && isset($_GET['original']))
    {
    ?>
    </strong>
    <div><strong>Thumbnail Image</strong></div>
    <div><img src="images/<?php echo base64_decode($_GET['thumb']); ?>" alt="Create thumbnail image using PHP" /></div>
    <div style="padding:6px 0px;"><a href="<?php echo base64_decode($_GET['original']); ?>" target="_blank">Click here to view full size original Image</a></div>
    <?php
    }
    ?></td>
    </tr>
    </table>
    </form>
    </div></td>
    </tr>
    </table>
    </div>
    </div>
    </body>
    </html>
    									

    Code For thumbnailCreator.php:

    <?php
    if(!empty($_FILES)){
    //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']);
    
    //This application developed by www.webinfopedia.com
    //Check the image is small that 124px
    if($gotwidth>=124)
    {
    //if bigger set it to 124
    $newwidth=124;
    }else
    {
    //if small let it be original
    $newwidth=$gotwidth;
    }
    //Find the new height
    $newheight=round(($gotheight*$newwidth)/$gotwidth);
    //Creating thumbnail
    $tmp=imagecreatetruecolor($newwidth,$newheight);
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight, $width,$height);
    //Create thumbnail image
    $createImageSave=imagejpeg($tmp,$img_thumb,100);
    
    if($createImageSave)
    {
    //upload the original file
    $uploadOrginal=move_uploaded_file($_FILES['thumb_img']['tmp_name'],$originalImage);
    if($uploadOrginal)
    {
    //if successfull
    header("Location:index.php?thumb=".base64_encode($img_fileName)."&original=".base64_encode($originalImage)."");
    }
    }
    
    }
    else{
    //If file formet not supported show error
    header("Location:index.php?message=error&fileType=false");
    ?>
    <?php
    }
    }
    ?>
    									

    Now please run index.php on browser and upload your photo which you want to create in thumb.

    Kindly post your comments if you got any bug report.

    Enjoy!!!

Comments on this post

Leave a Reply

  • Security Code :


    nine × = 27