PHP & GD Picture Upload and Auto Resize

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
Jack_155Q4
Forum Newbie
Posts: 3
Joined: Fri Aug 12, 2005 6:08 am
Location: London, UK

PHP & GD Picture Upload and Auto Resize

Post by Jack_155Q4 »

Hi, im new to forum and to php :oops:

I had a php script made for me but it seems a bit bugy with large pictures, It is supposed to take any size picture (in res or Mb) upload it and auto resize it to 640*X if the width is larger than 640 pixels, if under 640 it is left alone and just uploaded and the user is given the link.

I keep getting errors with the larger pics:

Code: Select all

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 2592 bytes) in /home/httpd/vhosts/cloverleaf4.co.uk/httpdocs/upload.php on line 76


This is my php.ini file:

php_value upload_max_filesize = 8M
php_value memory_limit = 25M

You can test it here:
http://www.cloverleaf4.co.uk/upload.php

I would really appreciate any help with this as it is driving me up the wall!

Cheers
Jack :D


This is my code:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Upload A File</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?
     if($_SERVER['REQUEST_METHOD']!='POST') {
?>
<form action="<? echo $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data" name="form1">
  <table width="350" style="background-color:#F6F6F6;border:1px solid black;" border="0" cellspacing="2" cellpadding="2">
    <tr>
      <td style="font-weight:bold;background-color:#DDD;">Upload a file </td>
    </tr>
    <tr>
      <td style="text-align:center"><input name="file" type="file" size="30"></td>
    </tr>
    <tr>
      <td style="text-align:center"><input type="submit" name="Submit" value="Upload"></td>
    </tr>
  </table>
</form>
<?
} else {
     $message = "";
     $validExtensions[] = "jpg";
     $validExtensions[] = "jpeg";
      $validExtensions[] = "gif";
      $validExtensions[] = "png";

     $sentFileSize = ceil($_FILES['file']['size']/1024);
      # this way is more accurate for the extension
     $sentFileExtension = explode(".", $_FILES['file']['name']);
      $number = count($sentFileExtension);
      $sentFileExtension = $sentFileExtension[$number-1];

      if(!in_array($sentFileExtension, $validExtensions)) {
           $message = "Your file has <span style=\"color:red\">NOT</span> been uploaded,";
           $message .= " because of a bad extension. We only accept JPG and BMP files.";
      } else {
           // SAVE THE FILE
           $uploaddir = 'images/uploads/';
           if (file_exists('uploadCount.txt')) {
                $handle = fopen('uploadCount.txt','r+');
                $newFileNumber = fread($handle,filesize('uploadCount.txt'))+1;
                fclose($handle);
                $handle = fopen('uploadCount.txt','w');
                fwrite($handle,$newFileNumber.'');
                fclose($handle);
           } else {
                $handle = fopen('uploadCount.txt','x');
                fwrite($handle,'1');
                $newFileNumber = 1;
           }
           $uploadfile = $uploaddir . basename($newFileNumber . "." . $sentFileExtension);


                $bron = $_FILES['file']['tmp_name'];
                $maxbreedte = 640;
                }

                if(!empty($bron)){
                     $dimensies = getimagesize($bron);
                     $breedte = $dimensies[0];
                     $hoogte = $dimensies[1];
                     if($breedte > $maxbreedte){
                          $nieuwebreedte = $maxbreedte;
                          $deelfactor = $breedte / $maxbreedte;
                          $nieuwehoogte = $hoogte / $deelfactor;

                          switch ($dimensies['mime']) {

                          case 'image/jpeg':

                               $image = imagecreatefromjpeg($bron);
                               $destination = imagecreatetruecolor($nieuwebreedte, $nieuwehoogte);
                               imagecopyresampled($destination, $image, 0, 0, 0, 0, $nieuwebreedte, $nieuwehoogte, $breedte, $hoogte);
                               imagejpeg($destination, $uploadfile);
                              imagedestroy($image);
                              imagedestroy($destination);
                          break;

                          case 'image/gif':

                               $image = imagecreatefromgif($bron);
                               $destination = imagecreate($nieuwebreedte, $nieuwehoogte);
                               imagecopyresampled($destination, $image, 0, 0, 0, 0, $nieuwebreedte, $nieuwehoogte, $breedte, $hoogte);
                               imagegif($destination, $uploadfile);
                              imagedestroy($image);
                              imagedestroy($destination);
                          break;

                          case 'image/png':

                               $image = imagecreatefrompng($bron);
                               $destination = imagecreate($nieuwebreedte, $nieuwehoogte);
                               imagecopyresampled($destination, $image, 0, 0, 0, 0, $nieuwebreedte, $nieuwehoogte, $breedte, $hoogte);
                               imagepng($destination, $uploadfile);
                              imagedestroy($image);
                              imagedestroy($destination);
                          break;
                          }
                     } else {
                          $uploadfile = $uploaddir . basename($newFileNumber. "." .$sentFileExtension);
                          if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
                               $message = "Your file has been successfully uploaded.<br>";
                               $message .= "Link to your file: [img]".$uploadfile."[/img]";
                          } else {
                               $message = "There was an error uploading the file, please try again!";
                          }
                     }
                }
           if (is_file($uploadfile)) {
                $message = "Your file has been successfully uploaded.<br>";
                $message .= "Link to your file, copy bold text to display picture in forum: <b>[img]http://www.cloverleaf4.co.uk/".$uploadfile."[/img]</b>";
           } else {
                $message = "There was an error uploading the file, please try again!";
           }
?>
<table width="350" style="background-color:#F6F6F6;border:1px solid black;" border="0" cellspacing="2" cellpadding="2">
  <tr>
    <td style="font-weight:bold;background-color:#DDD;">File upload result </td>
  </tr>
  <tr>
    <td style="text-align:center"><? echo $message ?></td>
  </tr>
</table>
<?
}
?>
</body>
</html>


feyd | Please use

Code: Select all

tags when posting php code, especially large chunks such as this.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

here's the general problem:

when loading an image, GD requires 4 bytes for every pixel the image has. So it's very easy to run out of space, especially since each image GD creates also requires a similar footprint. It's also quite possible imagecopyresampled() requires a bit of RAM too.

Some quick numbers:
800 x 600 requires 1,920,000 bytes
1024 x 768 requires 3,145,728 bytes
1280 x 1024 requires 5,242,880 bytes
1600 x 1200 requires 7,680,000 bytes
1920 x 1080 requires 8,294,400 bytes
1920 x 1200 requires 9,216,000 bytes
2560 x 1600 requires 16,384,000 bytes

I can easily see imagecopyresampled() and the other resizing functions could require a second buffer with the same dimensions.

I tried samples to 1280, which all worked fine. So it'd seem your settings cover the bulk of the population well enough.
Jack_155Q4
Forum Newbie
Posts: 3
Joined: Fri Aug 12, 2005 6:08 am
Location: London, UK

Post by Jack_155Q4 »

Hi, thanks very much,

What do you suggest I should do to enable all uploadable images?

The site is hosted by http://www.netpivotal.co.uk - anything more I can ask them to do, or does my code need editing?

Cheers
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd like to know specifics of when the script actually fails. What exact image size is failing, and what conditions are being used in the script.
Jack_155Q4
Forum Newbie
Posts: 3
Joined: Fri Aug 12, 2005 6:08 am
Location: London, UK

Post by Jack_155Q4 »

Dont know how much I can tell you about the specifics in the script, but the image uploads, then right at the end when im expecting it to show me the link it chucks out the error message, I am testing with 2592*1944, but this seems to happens on anything larger than 1280 it seems.

Cheers
Post Reply