Link to home
Start Free TrialLog in
Avatar of ellandrd
ellandrdFlag for Ireland

asked on

cannot upload images - allowed memory size exhausted

Ive created a upload script, but each time I try uploading images i get this message:

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 10304 bytes) in W:\Apache\plan-2\subdomain\htdocs\properties\functions\upload-photo.php on line 56

I my PHP.ini file I have the following set:

memory_limit = 10M
post_max_size = 10M
upload_max_filesize = 10M

However the largest file size of any of the images im trying to upload is 647KB so i'm not sure what is wrong.  I'm running Apache 2.2.3 with PHP 5.2.3 on a Windows box.

Here is my upload script:

function fcnUploadPhoto($sourse,$quality,$width,$directory,$filename)
{
      $params = array();
      $params = getimagesize($sourse);
        
         if($params)
      {
               if($params[0] < $width)
            {
                  copy($sourse,$directory.$filename);
            }
            else
            {
                  $height = round(($width * $params[1]) / $params[0]);
                  $tmp = imagecreatetruecolor($width,$height);
                        
                  switch($params[2])
                  {
                        case '1':
                              $src = imagecreatefromgif($sourse);
                              $textcolor = imagecolorat($src,0,0);
                              $tmp = imagecreate($width,$height);
                                    
                              imagepalettecopy($tmp,$src);
                                    imagecopyresized($tmp,$src,0,0,0,0,$width,$height,$params[0],$params[1]);
                                    
                              $pixel_over_black = imagecolorat($tmp,0,0);
                              $backgroundcolor = imagecolorallocate($tmp,255,255,255);
                                    
                              imagefilledrectangle($tmp,0,0,$width,$height,$backgroundcolor);
                                    imagecopyresampled($tmp,$src,0,0,0,0,$width,$height,$params[0],$params[1]);
                                    
                              $pixel_over_white = imagecolorat($tmp,0,0);
                                    
                              if ($pixel_over_black != $pixel_over_white)
                              {
                                    imagefilledrectangle($tmp,0,0,$width,$height,$textcolor);
                                          imagecopyresampled($tmp,$src,0,0,0,0,$width,$height,$params[0],$params[1]);
                                    imagecolortransparent($tmp,$textcolor);
                              }
                                    
                              imagegif($tmp,$directory.$filename,$quality);
            
                              break ;
                        case '2' :
                              $src = imagecreatefromjpeg($sourse);
                                    
                  imagecopyresampled($tmp,$src,0,0,0,0,$width,$height,$params[0],$params[1]);
                              imagejpeg($tmp,$directory.$filename,$quality);
                                    
                              break ;
                        case '3' :
                              $src = imagecreatefrompng($sourse);
                                    
                        imagecopyresampled($tmp,$src,0,0,0,0,$width,$height,$params[0],$params[1]) ;
                              imagesavealpha($tmp,true);
                              imagepng($tmp,$directory.$filename);
                                    
                              break ;
                        default :
                              $src = imagecreatefromjpeg($sourse);
                                    
                        imagecopyresampled($tmp,$src,0,0,0,0,$width,$height,$params[0],$params[1]);
                              imagejpeg($tmp,$directory.$filename,$quality);
                                    
                              break ;
                              
                        imagedestroy($src);
                        imagedestroy($tmp);
                  }      
            }
      }

      return true;
}
                                          
$photoextensions = array('image/gif','image/jpg','image/jpeg','image/png','image/pjpeg');
                                                            
foreach($_FILES as $file_name => $userfile)
{
      if(in_array($userfile['type'],$photoextensions))
      {
                fcnUploadPhoto($userfile['tmp_name'],70,159,$photothumbnailpath,$userfile['name']);
      }
}

ellandrd
SOLUTION
Avatar of m1tk4
m1tk4
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ellandrd

ASKER

OK i checked my phpinfo(); and it shows:

memory_limit      10M      10M
post_max_size      10M      10M
upload_max_filesize      10M      10M

so i think apache has picked up on my php.ini settings.

>>Don't forget that JPEG files are compressed, but once you start resampling or doing something with imagejpg they become unpacked and the resources that store image information can very easily be 10-15 times the size of the original JPEG.

OK i didnt know this, but im using the same upload script i build for another site over 12 months ago that also allows users upload images including JPEG files and i didnt have this problem.

ellandrd
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>And please note that different platforms has different memory management (also, software bugs and different software versions can produce the issue too). The same script could work perfectly on FreeBSD with the same php settings.

OK thanks for this great advise.  I do remember, the other script runs on a older version of PHP - PHP4 where now im running PHP5...

Im going to check if there is any bugs listed.  I will also try bumping the memory up to 32MB on both my localhost and live server.

ellandrd
OK i found out that this only happens with certain JPEGS and especially if they come straight from a camera which mine have done.

I tested a image that didn't come from a camera and it was worked fine.

The PHP bugs site also listed this as not a bug, that users should increase memory up to 20-30MB for image processing to avoid this problems...

I also read that memory is freed if you unset used variables. So im going to clear all variables after i don't need them any more via unset($variable).

thanks for the help

ellandrd