Page 1 of 1

Resize Image then Upload to Server Directory

Posted: Sun Aug 16, 2009 12:36 pm
by simonmlewis
Hi.

I have a web page that uploads images (one by one) to a directory on the web server.

Trouble is, people could upload 2mp images and that is way too big for the site to display; it needs to be only 700 pixels wide, or 800 tops!

I have found a few scripts that do resizing, but none that appear to be straight forward, or that will work with an "upload" script.

My upload script is:

Code: Select all

$photo=$_POST['photo'];
$pic=($_FILES['photo']['name']);
$target = $_SERVER['DOCUMENT_ROOT']."/images/productphotos/";
srand(time());
$random = (rand()%99999999);
$pic=($_FILES['photo']['name']);
$newname="$random"."$pic";
$target = $target . $newname;
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
      {
echo "happy days";
}
With the $random system that renames files, I can't see how to plug in a resizer. The other problems is that many of the resizers are for JPG only. Some photos could easily be PNG or GIFs, depending on the camera used.

Can anyone please help?

Re: Resize Image then Upload to Server Directory

Posted: Sun Aug 16, 2009 3:36 pm
by cpetercarter
Here is a function which I wrote for another purpose which will resize an image (jpeg, gif, png). The user needs to specify a new width in px ($newWidth) and a new filename ($newpath) as well as the path of the image to be resized ($imagepath). The height adjusts automatically in proportion to the new width. My inclination would be to move the uploaded file with its original name, then resize it with a new ($random.$pic) name. I hope it is of some help to you.

Code: Select all

 
 function changesize($imagepath,$newWidth,$newpath)  {
 
   //changes the size of an image
 
   //the user can replace the old image with the new (resized) image
 
   //or can store the resized image with a new name
 
  $imagedata = getimagesize($imagepath);
 
  $ratio = $imagedata[1]/$imagedata[0];
 
  $newHeight = $newWidth*$ratio;
 
  $type = $imagedata[2];
 
  switch ($type) {
 
    case 1:
 
         $tempimage = imagecreatefromgif($imagepath);
 
         break;
 
    case 2;
 
         $tempimage = imagecreatefromjpeg($imagepath);
 
         break;
 
    case 3;
 
         $tempimage = imagecreatefrompng($imagepath);
 
         break;
 
    default:
 
         return false;
 
  }
 
  $newimage = imagecreatetruecolor($newWidth,$newHeight);
 
  imagecopyresampled($newimage,$tempimage,0,0,0,0,$newWidth,$newHeight,$imagedata[0],$imagedata[1]);
 
 
 
  if ($newpath ==  $imagepath)  {
 
    unlink ($imagepath);
 
  }
 
  switch($type)  {
 
     case 1:
 
     $return = imagegif($newimage,$newpath);
 
     break;
 
     case 2:
 
     $return = imagejpeg($newimage,$newpath);
 
     break;
 
     case 3:
 
     $return = imagepng($newimage,$newpath);
 
     break;
 
     default:
 
     return false;
 
  }
 
  //it is vital to destroy the temporary images before we leave!
 
  imagedestroy ($tempimage);
 
  imagedestroy ($newimage);
 
  return $return;
 
 }
 
 

Re: Resize Image then Upload to Server Directory

Posted: Sun Aug 16, 2009 3:45 pm
by simonmlewis
Thanks - nice script, though I cannot see how I can plug it into my code.

I'm pretty good with PHP, but I'm no expert (which clearly, you are).

Re: Resize Image then Upload to Server Directory

Posted: Sun Aug 16, 2009 7:05 pm
by mikem562
instead of "echo "happy days";" when the script is complete, call the changesize function

changesize($imagepath,$newWidth,$newpath) < and insert the vars you want

Re: Resize Image then Upload to Server Directory

Posted: Mon Aug 17, 2009 3:03 am
by simonmlewis
But if I do that after all the other code, it's too late.
The other code will have collected the file, renamed it and uploaded it.