Default TemplateGreen TemplateBlue TemplateRed TemplateGold TemplateBlue Gloss Template
Article Categories

Mycities Network
PHP Scripts Examples and Tutorials
Upload Image and resize proportionally using PHP

Monday Aug 21, 2006
This example will show you how to Upload a Image and resample the image proportionally with 100% quality, I have made a function it will do the needful, so you don't have to change the function, If you want to change you can change it, I am not going to charge anyway.

Function
<?
function SafMakeThumb($img_src, $img_th,$quality=100,$new_w=150,$new_h=150){
      $img_size = GetImageSize ($img_src);
      $img_in = ImageCreateFromJPEG ($img_src);
      list($old_x, $old_y) = getimagesize($img_src);

      if ($old_x > $old_y) {
            $img_x=$new_w;
            $img_y=$old_y*($new_h/$old_x);
      }

      if ($old_x < $old_y) {
            $img_x=$old_x*($new_w/$old_y);
            $img_y=$new_h;
      }

      if ($old_x == $old_y) {
            $img_x=$new_w;
            $img_y=$new_h;
      }

      $img_out = ImageCreateTrueColor($img_x, $img_y);
      ImageCopyResampled ($img_out, $img_in, 0, 0, 0, 0, $img_x, $img_y, $img_size[0], $img_size[1]);
      ImageJPEG ($img_out, $img_th, $quality);
      ImageDestroy ($img_out);
      ImageDestroy ($img_in);
}
?>

When you are uploading the file from form use "fldFile" as file field

Calling the function
<?
$uploadDir = "$DOCUMENT_ROOT/uploadfiles/";

      $fldFileUploadFile = $uploadDir . $_FILES['fldFile']['name'];
      if (move_uploaded_file($_FILES['fldFile']['tmp_name'], $fldFileUploadFile)){
            $img_th = $fldFileUploadFile. '.thumb.jpg';
            make_thumb($fldFileUploadFile,$img_th,100,120,120);
      }
?>

Author: Sri Lankan



Comments : What do you think about this news?
No Comments yet.

POST COMMENT
Name
Message
  Post will be visible after admins approval
 
Members Login
User ID
Password
    Register
Forgot password?

SriLankaWebHosting.com
Just for (Rs.500) $5 per month

Your own domain name, 1GB Space, Unlimited Email Address, Sri Lankan Support & for more info call us.
077-989-5368, 072-829-9077



Terms And Conditions | Link Directory

Join Sri Lanka Banner Exchange | Resources - Free Link Exchange