Default TemplateGreen TemplateBlue TemplateRed TemplateGold TemplateBlue Gloss Template
Sri Lanka News 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?

Sri Lanka Toolbar
Listen to live Sri Lanka Radio, Watch Sri Lanka TV, Search Sri Lanka, Search Google, Sri Lanka New
Download Sri Lanka Toolbar FREE




Join Sri Lanka Banner Exchange | Text Link | Resources - Top Link Exchange