Page 1 of 1

Uploading and resizing an image... any image...

Posted: Sun Mar 23, 2008 6:29 pm
by pyromaster114
Okay so I need to figure out how to make this image resize script work for stuff other than jpeg files...
I realize there are several things in there that might be an issue, but I have no clue where to start...

Code: Select all

 
<form action="<?php echo $_server['php-self'];  ?>" method="post" enctype="multipart/form-data" id="something" class="uniForm">
        <input name="new_image" id="new_image" size="30" type="file" class="fileUpload" />
        <button name="submit" type="submit" class="submitButton">Upload/Resize Image</button>
</form>
<?php
        if(isset($_POST['submit'])){
          if (isset ($_FILES['new_image'])){
              $imagename = $_FILES['new_image']['name'];
              $source = $_FILES['new_image']['tmp_name'];
              $target = "images/".$imagename;
              move_uploaded_file($source, $target);
 
              $imagepath = $imagename;
              $save = "images/" . $imagepath; //This is the new file you saving
              $file = "images/" . $imagepath; //This is the original file
 
              list($width, $height) = getimagesize($file) ; 
 
              $modwidth = 150; 
 
              $diff = $width / $modwidth;
 
              $modheight = $height / $diff; 
              $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($file) ; 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
 
              imagejpeg($tn, $save, 100) ; 
 
              $save = "images/sml_" . $imagepath; //This is the new file you saving
              $file = "images/" . $imagepath; //This is the original file
 
              list($width, $height) = getimagesize($file) ; 
 
              $modwidth = 80; 
 
              $diff = $width / $modwidth;
 
              $modheight = $height / $diff; 
              $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($file) ; 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
 
              imagejpeg($tn, $save, 100) ; 
            echo "Large image: <img src='images/".$imagepath."'><br>"; 
            echo "Thumbnail: <img src='images/sml_".$imagepath."'>"; 
 
          }
        }
?>
 

Re: Uploading and resizing an image... any image...

Posted: Sun Mar 23, 2008 7:28 pm
by devbro
you need to modify your code around the following function:
imagecreatefromjpeg()

this function only reads jpg files. to include png and gif you need to use a similar function.
for example look up:
http://ca.php.net/imagecreatefromgif

also to correctly detect a file type check the following:
http://ca3.php.net/mime-content-type


i hope it helps

Re: Uploading and resizing an image... any image...

Posted: Sun Mar 23, 2008 9:24 pm
by it2051229
Ok i have this function which I use whenever i make a PHP project.
just copy and paste the function to your code then if you want to resize an image, just call the function passing
the three parameters. And then after calling the function, check again the image you wanted to resize, and BAMMM!! you got your old big image resized. Ff you want the full working script like Upload and Resize?? just email me and i'll send you the working script.

Code: Select all

 
<?php
/**
* Resize Image Method
* $imagePath = "where is the image to be resized?"
* $width = "what width would you like the image to be resized?"
* $height =  "what height would you like the image to be resized?"
* Note: To use this function your PHP GD LIBRARY should be enabled
*/
function resizeImage($imagePath, $width, $height)
{
     // get the extention of the image like for example, if the image path is "/images/hello.jpg".. the "jpg" will be stored in the $ext variable
     $ext = explode(".", $imagePath);
     $ext = $ext[count($ext)-1];
 
     // check the extention type of the image if it is valid.. only JPG, JPEG, PNG, and GIF is allowed
     if($ext == "jpg" || $ext == "jpeg")
     {
          // copy the image which will be resized in JPG or JPEG format
          $im = imagecreatefromjpeg($imagePath);
     }
     elseif($ext == "png")
     {
          // copy the image which will be resized in PNG format
          $im = imagecreatefrompng($imagePath);
     }
     elseif($ext == "gif")
     {
          // copy the image which will be resized in GIF format
          $im = imagecreatefromgif($this -> imagePath);
     }
 
     // get the width and height of the image that is to be resized
     $x = imagesx($im);
     $y = imagesy($im);
 
     // check if the width and height of the image is COMPATIBLE with the given or specific width and height to be resized
     if($x <= $width && $y <= $height)
     {
          // if not valid, then do not resize the image, instead, just return and do nothing
          return $im;
     }
 
     // compute for the new width and new height of the image to be resized
     if($x >= $y)
     {
          $newx = $width;
          $newy = $newx * $y / $x;
      }
     else
     {
          $newy = $height;
          $newx = $x / $y * $newy;
      }
 
      // create the new resized image
      $im2 = imagecreatetruecolor($newx, $newy);
      imagecopyresized($im2, $im, 0, 0, 0, 0, floor($newx), floor($newy), $x, $y);
 
      // over write the old image and replace it with the new resized image
      // check again the extension of the image that is to be resized
      if($ext == "jpg" || $ext == "jpeg")
      {
          // create the new resized image in JPEG and overwrite the old image where quality of image is 100
          imagejpeg($im2, $imagePath,100);
      }
      else if($ext == "png")
      {
           // create the new resized image in PNG and overwrite the old image where quality of image is 100
           imagepng($im2, $imagePath,100);
      }
      else if($ext == "gif")
      {
           // create the new resized image in GIF and overwrite the old image. GIFs does not have image quality settings
           imagegif($im2, $imagePath);
       }
 
      return;
}
?>
 

Re: Uploading and resizing an image... any image...

Posted: Mon Mar 24, 2008 5:38 am
by frosty16
it2051229

i was waondering if you could send me the full script as i am having the same type of problem i think. i have posted on here on a new topic if you would like to see the problem that i have.


viewtopic.php?f=1&t=80429

frosty16