Friday, February 26, 2010

Create thumbnail image in php

This function will create the thumbnail image of the source file
other thing explain in function

function createthumbimage($source_path,$ext,$filename)
    {

          //$ext=extension of file

      
     if ($ext == 'jpg' || $ext == 'jpeg')
     {
      $img = @imagecreatefromjpeg($source_path);
     }
      else if ($ext == 'png')
      {
      $img = @imagecreatefrompng($source_path);
     # Only if your version of GD includes GIF support
     } else if ($ext == 'gif')
     {
      $img = @imagecreatefromgif($source_path);
     }
    
         if ($img)
     {

      # Get image size and scale ratio
      $width = imagesx($img);
      $height = imagesy($img);
      $scale = min(PRODUCT_MAX_WIDTH/$widthPRODUCT_MAX_WIDTH/$height);
     //PRODUCT_MAX_WIDTH and PRODUCT_MAX_WIDTH is predefine width and height of thumbnail image

      # If the image is larger than the max shrink it
     
       $new_width = floor($scale*$width);
       $new_height = floor($scale*$height);
    
       # Create a new temporary image
       $tmp_img = imagecreatetruecolor($new_width, $new_height);
    
       # Copy and resize old image into new image
       imagecopyresized($tmp_img, $img, 0, 0, 0, 0,
            $new_width, $new_height, $width, $height);
       imagedestroy($img);
       $img = $tmp_img;
    
      // $new_filename="";
       imagejpeg($img,PRODUCT_IMG_THUMB_PATH.$filename);

         // PRODUCT_IMG_THUMB_PATH.= this the define path that will put thumb file in define path

      }
   }

Have Dream Day

No comments:

Post a Comment