<?php
   ob_start
();

   function 
calculate_width_height($width$height,$width_needed$height_needed)
   {
      if (
$width <= $width_needed and $height <= $height_needed)
      {
         
//Estimate if both Width and Height of the image are smaller then
         //the given values for Width and Height
         //if this is true dan keep the original size of the image
         
$l_new_width=$width;
         
$l_new_height=$height;
      }
      else
      {
         if (
$height $height_needed)
         {
            
//If the value of the height of the image is greater then the given value for height
            //then estimate the percentage of the given value for height in relation to
            //the original height value of the image
            //Use this percentage to calculate the new width value for the image
            //in relation to original width value of the image

            
$l_percentage_height =$height_needed/($height/100);
            
$l_new_width=($width/100)*$l_percentage_height;
            
$l_new_height=$height_needed;

            if (
$l_new_width $width_needed)
            {
               
//After calculation the new width value can stil be greater then the needed width value
               //do the same calculation as above but then for the width needed

               
$l_percentage_width =$width_needed/($width/100);
               
$l_new_height=($height/100)*$l_percentage_width;
               
$l_new_width=$width_needed;
            }
         }
         else
         {
            
//The height value of the image is not greater then the needed height value
            //but original width value of the image is
            //calculate the new values for width and height

            
$l_percentage_width =$width_needed/($width/100);
            
$l_new_height=($height/100)*$l_percentage_width;
            
$l_new_width=$width_needed;

            if (
$l_new_height $height_needed)
            {
               
$l_percentage_height =$height_needed/($height/100);
               
$l_new_width=($width/100)*$l_percentage_height;
               
$l_new_height=$height_needed;
            }
         }


      }
   
$calculated_width_height[] = round($l_new_width);
   
$calculated_width_height[] = round($l_new_height);
   return 
$calculated_width_height;
   }


   function 
resize($filename$dest$width$height$type=''){
       if(@
imagecreatefromgif($filename)){
           
$img=imagecreatefromgif($filename);
           
$type_r='gif';
       }
       elseif(@
imagecreatefromjpeg($filename)){
           
$img=imagecreatefromjpeg($filename);
           
$type_r='jpg';
       }
       elseif(@
imagecreatefrompng($filename)){
           
$img=imagecreatefrompng($filename);
           
$type_r='png';
       }
       elseif(@
imagecreatefromwbmp($filename)){
           
$img=imagecreatefromwbmp($filename);
           
$type_r='bmp';
       }
       else{
           return 
'Cannot open file';
       }
       
$type=($type=='')?$type_r:'';
       
$img_n= @ imagecreatetruecolor ($width$height);
       @ 
imagecopyresampled($img_n$img0000$width$heightimagesx($img), imagesy($img));
       if(
$type=='gif'){
           
imagegif($img_n$dest);
       }
       elseif(
$type=='jpg'){
          @ 
imagejpeg($img_n$dest);
       }
       elseif(
$type=='png'){
          @ 
imagepng($img_n$dest);
       }
       elseif(
$type=='bmp'){
          @ 
imagewbmp($img_n$dest);
       }
       Return 
true;
   }



   function 
upload_file($root_dir,$file_name,$file_name_tmp,$lc_width,$lc_height)
   {

      
$lc_max_filesize=2001000;
      
$filesize=filesize($file_name_tmp);
      
$sub_server=substr($_SERVER["HTTP_REFERER"],7,9);
      
$uploaddir "/get_on_top/image_resize/uploads";

      if (
         !
file_exists($HTTP_SERVER_VARS["DOCUMENT_ROOT"].$uploaddir) ||
         
!= (fileperms($HTTP_SERVER_VARS["DOCUMENT_ROOT"].$uploaddir) & 2) ||
         
!= (fileperms($HTTP_SERVER_VARS["DOCUMENT_ROOT"].$uploaddir) & 1)
         )
      {
         
$error="tets";
      }

      
$fileext substr($file_name, -4);

      if (
strtolower($fileext) != ".jpg" and strtolower($fileext) != ".gif")
      {
         print 
"<b>For security reasons only the uploading of .jpg and .gif files is allowed.</b><br>\n";
         print 
"<a href=\"javascript:history.back();\">Click here to go back.</a><br>";
         exit;
      };

      
$loc $uploaddir."/".$file_name;
      
$fullloc $root_dir.$loc;
      
$upfile $file_name_tmp;

      if (
$filesize>$lc_max_filesize)
      {
         
$lc_message"File is too big, may not be bigger then 2MB";
      }
      else
      {
         if(
move_uploaded_file($upfile$fullloc))
         {
            
chmod($fullloc0644);
         }
         else
         {
            die(
"Error moving \"$upfile\" to \"$fullloc\".");
         }

         
$test =filesize($fullloc);
      }


      list(
$original_width$original_height$type$attr) = getimagesize($fullloc);
      list(
$l_new_width,$l_new_heigth) = calculate_width_height($original_width$original_height,$lc_width,$lc_height);

      
resize($fullloc$root_dir.$uploaddir."/".$lc_width."_".$lc_height."_".$file_name$l_new_width$l_new_heigth);

   
$upload_info[] = $lc_message;
   
$upload_info[] = $original_width;
   
$upload_info[] = $original_height;
   
$upload_info[] = $l_new_width;
   
$upload_info[] = $l_new_heigth;

   return 
$upload_info;

   }



?>
<body>
<?php
   $root_dir 
=$HTTP_SERVER_VARS["DOCUMENT_ROOT"];

   if (
$_POST["resize_submit"] == "resize")
   {

      
$lc_height           =$_POST["height"];
      
$lc_width            =$_POST["width"];

      
$day getdate();
      
$minutes $day['minutes'];
      
$temp=gettimeofday();
      
$sec=(int)$temp["sec"];

      if (!empty(
$_FILES["userfile"]["name"]))
      {
         
$fileext substr($_FILES["userfile"]["name"], -4);

         
$ffile_name="1_".$minutes."_".$sec.$fileext;

         
$tfile_name_tmp=$_FILES["userfile"]["tmp_name"];

         list(
$lc_upload_message,$original_width,$original_height,$l_new_width,$l_new_height) = upload_file($root_dir,$ffile_name,$tfile_name_tmp,$lc_width,$lc_height);

         if (
$lc_upload_message!="")
         {
            echo 
"error_message".$lc_upload_message;
         }
      }

      
?>
      <form ENCTYPE="multipart/form-data"  action="resize_image.php" method="post">
      <table>
      <tr><td>height              :</td><td><input type="TEXT" name="height" size="10" value="<?php echo $lc_height ?>"></td></tr>
      <tr><td>width               :</td><td><input type="TEXT" name="width"  size="10" value="<?php echo $lc_width  ?>"></td></tr>
      <tr><td>image to upload     :</td><td><INPUT NAME="userfile" TYPE="file"></td></tr>
      <tr><td><input type="submit" name="resize_submit" value="resize"></td>
      </form>
      </table>

      <table>
      <tr>
      <td><IMG border=0 src="uploads/<?php echo $ffile_name ?>" ></td>
      <td  valign=bottom><IMG border=0 src="uploads/<?php echo $lc_width.'_'.$lc_height.'_'.$ffile_name ?>" ></td>
      </tr>
      <tr>
      <td><?php echo "org_height:".$original_height." org_width:".$original_width?></td>
      <td><?php echo "new_height_".$l_new_height." new_width_".$l_new_width?></td>
      </tr>
      </table>

      <?php


   
}
   else
   {

      
?>
      <form ENCTYPE="multipart/form-data"  action="resize_image.php" method="post">
      <table>
      <tr><td>height              :</td><td><input type="TEXT" name="height" size="10" ></td></tr>
      <tr><td>width               :</td><td><input type="TEXT" name="width"  size="10" ></td></tr>
      <tr><td>image to upload     :</td><td><INPUT NAME="userfile" TYPE="file"></td></tr>
      <tr><td><input type="submit" name="resize_submit" value="resize"></td>
      </form>
      </table>
      <?php
   
}
?>
</body>
</html>
<?php
ob_end_flush
();
?>
?>