Tuesday 13 March 2012

Image Uploading With Auto thumbnail generation In PHP

     Here i demonstrate how to create a form with file field , And upload image though this file field and automatically generate thumbnail for that particular image . Uploaded image stored in fullImage folder in root path and thumbnail images stored in thumbnail folder in root folder.
 
Step 1

   Create a HTML form for uploading image . 
   HTML form for uploading image .   profile.html

 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
         <head>
                 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
                 <title>mobile Application</title>
          </head>
          <body>

          <form action="AccountAdd.php" method="post" enctype="multipart/form-data" >
              <div>Frist Name</div><div><input type="text"  id="firstname" name="firstName"/></div>
              <div>Last Name</div><div><input type="text"  id="lastName" name="lastName"/></div>
              <div>Age</div><div><input type="text"  id="age" name="age"/></div>
              <div>Image</div><div><input type="file"  id="user" name="fullImage"/></div>
               <div style="margin: 14px 0 0 25px "><input type="submit" id="login" name="login" value="register"></div>
          </form>
          </body>    
</html> 


 Step 2:-

          Function for creating thumbnails. This function create thumbnails from source file ( name pass as $source argument ) and will create thumbnail image in the destination folder ( destination Folder name pass on $destination argument ) .

AccountAdd.php

function create_thumbnail($source,$destination , $thmb_width){
              $size = getimagesize($source);
              $width = $size[0];
              $height = $size[1];
              $x = 0;
              $y = 0;
              if($width > $height){
                  $x = ceil(($width-$height)/2);
                  $width = $height;
              } elseif ($height > $width){
                  $y = ceil(($height - $width)/2);
                  $height = $width;
              }
              $new_image = imagecreatetruecolor($thmb_width, $thmb_width) or die("cannot initialize new GD image stream");
              $extension = get_image_extension($source);
              if ($extension=='jpg' || $extension == 'jpeg'){
                  $image = imagecreatefromjpeg($source);
              }
              if ($extension == 'gif'){
                  $image = imagecreatefromgif($source);
              }
              if ($extension == 'png'){
                  $image = imagecreatefrompng($source);
              }
             
              imagecopyresampled($new_image,$image, 0, 0, $x, $y, $thmb_width, $thmb_width, $width, $height);
              if ($extension=='jpg' || $extension == 'jpeg'){
                  imagejpeg($new_image , $destination);
              }
              if ($extension == 'gif'){
                  imagegif($new_image, $destination);
              }
              if ($extension == 'png'){
                  imagepng($new_image , $destination);
              }
         
          }



Step 3:- Define variables like maxi_size of image , width of  thumb nail image , Actual image location , thumbnail image location . Here both width and height of image are take on same variable $thumb_width and use this value set as both width and height of the thumb image  (means square image are generate.)

 AccountAdd.php

              $image_location = "fullImage/";
              $thumbs_location = "thumbnail/";
              $thumb_width =100;
              $maximum_size = 50000;


Step 4:- Create function for taking extension for image for validating  image 

            AccountAdd.php

              function get_image_extension($name){
                
                  $name =strtolower($name);
                  $i = strpos($name, ".");
                  if(!$i){return ""; }
                $l = strlen($name) - $i;
                $exstension = substr($name, $i+1 , $l);
                return $exstension;
          }   



Step 5 :- Create function for generating random name for avoiding name conflict .

     AccountAdd.php

            function random_name($length){
              $character = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
              $name = "";
              for($i = 0; $i <$length ; $i++){
                  $name .= $character[mt_rand(0, strlen($character)-1)];
              }
              return "image-".$name;
             
          }



Step 6:-  Take information from HTML page and upload Actual image in  fullImage folder in the root path.
            
            
           AccountAdd.php

             if($_FILES['fullImage']['name'] == ''){
                       $results = "Image source cant not be empty. please
                       click the 'browse' button locate an image then click the 'upload Image'
                       button!!";
               } else{
                   $size = filesize($_FILES['fullImage']['tmp_name']);
                   $filename = stripslashes($_FILES['fullImage']['name']);
                   $extension = get_image_extension($filename);
                   if($size > $maximum_size){
                       $results = "Your file size exceeds the maximum size limit
                       please try again!";
                   }
                   else
                   if(($extension != "jpg") &&
                      ($extension != "jpeg") &&
                      ($extension != "png") &&
                      ($extension != "gif") ) {
                         
                          $results = "Image can only be with jpg, jpeg, png or gif exstension .
                          please try again !</center>";
                      
                    }else{
                        $image_random_name = random_name(15).".".$extension;
                        $copy = @copy($_FILES['fullImage']['tmp_name'], $image_location.$image_random_name);
                       
                        if(!$copy){
                            $results = "Error while uploading image! Please try again !";
                        } else {
                            create_thumbnail($image_location.$image_random_name, $thumbs_location.$image_random_name, $thumb_width);
                            $results = "Image has been uploaded";
                        }
                    }
               }



 


 Note:- I found these code when i search for my requirement on internet .
 

No comments:

Post a Comment