Resize Image then Upload to Server Directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Resize Image then Upload to Server Directory

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Resize Image then Upload to Server Directory

Post 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;
 
 }
 
 
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Resize Image then Upload to Server Directory

Post 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).
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
mikem562
Forum Newbie
Posts: 5
Joined: Sun Aug 16, 2009 3:21 pm

Re: Resize Image then Upload to Server Directory

Post 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
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Resize Image then Upload to Server Directory

Post 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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply