Uploading images - Memory limit error

Hi guys,

having a hassle with a php script I’m working on - it uses the standard upload function (I think it’s standard anyway) with the ImageCopyResampled, ImageJPEG, ImageDestroy etc.

I used a custom php.ini override (don’t have root access) with the following settings:

include_path = “.:/usr/local/lib/php”

upload_max_filesize = 20M
memory_limit = 110M
post_max_size = 25M

Yet I get an error like this when uploading a 6.9MB file - everything less than 4MB seems to work perfectly:

Fatal error: Allowed memory size of 115343360 bytes exhausted (tried to allocate 16128 bytes) in /homepages/7/d234302292/htdocs/brastop-competition/photo-upload.php on line 11

Tried playing about with the values but still get an error.

We need to upload files of at least 10MB max and wondering how to fix?

Many thanks,
J

Yeah, but you’ll need more memory available to PHP.

To work out how much memory you’ll need for an image try this:


$imgAttrs = getimagesize('path/to/file.jpg');
$estimatedMemory = $imgAttrs[0] * $imgAttrs[1] * 4;

(that’s image width * image height * 4)

It’s not too accurate, but for very large images should work.

Use this as a guide. This is from a script I wrote, so has a little more than what oyu are using but I am sure you can pull something out of it that you need. No need to raise the mem limit with this btw.

Upload action:

 if ($a == 'upload') {
  if (empty($_REQUEST['affirm'])) {
   redirect($path_to.'photos.dash.php?m=true');
  }
  set_time_limit(120);
  ini_set('memory_limit', '32M');
  $folder =        $docroot . 'UserFiles/Photos/' . $user->user_id . '/';
  $max_pp_size =   1229;  // in Kb, will be shrunk down.
  $max_pp_h =   800;  // Pixels height
  $max_pp_w =   800;  // Pixels width
  $shrink_h =   150;   // Max pixels height when resized
  $shrink_w =   150;   // Max pixels width when resized
  $shrink_h_tiny = 50;   // Max pixels height when resized
  $shrink_w_tiny = 50;   // Max pixels width when resized
  $image_types =   'jpg,jpeg,gif,png';
  $randNum = rand(1, 15);
  $filenameFull = md5($user->user_id.time().$randNum);
  $filenameThumb = $filenameFull.'_tn';
  $temp = $_FILES['photo']['tmp_name'];
  if( filesize($temp) > ($max_pp_size * 1000) ) {
   $main = '<div class="col2InnerRow">Photo must be under '.$max_pp_size.' KB. <a href="'.$path_to.'photos.dash.php">Try again</a> or  <a href="http://www.resizr.com/" target="_blank">Resize with Resizr for free.</a></div>';
   require($docroot.'system/tparse/dash.exit.php');
  }
  $size = getimagesize($temp);
  $extension = getFileExt($_FILES['photo']['name']);
  $extension = strtolower($extension);
  $extension = explode('.', $extension);
  $ext = strtolower($extension[0]);
  $goode = strtolower($image_types);
  $goode = explode(',', $goode);
  if( !in_array( $ext, $goode ) ) {
   $main = 'File type not allowed.';
   require($docroot.'system/tparse/dash.exit.php');
  }
  $file = $filenameFull . '.' . $ext;
  if (file_exists($docroot . 'UserFiles/Photos/' . $user->user_id . '/'.$file)) {
   unlink($docroot . 'UserFiles/Photos/' . $user->user_id . '/'.$file);
  }
  if (file_exists($docroot . 'UserFiles/Photos/' . $user->user_id . '/'.$filenameThumb.'.'.$ext)) {
   unlink($docroot . 'UserFiles/Photos/' . $user->user_id . '/'.$filenameThumb.'.'.$ext);
  }
  if (move_uploaded_file($temp,$docroot . 'UserFiles/Photos/' . $user->user_id . '/'.$file)) {
   mysql_query('INSERT INTO `profile_pics` SET `picsTimestamp`="'.time().'",`picsFullSize`="'.$file.'", `picsThumbnail`="'.$filenameThumb.'.'.$ext.'",`user_id`="'.$user->user_id.'"') or errorHandling(mysql_error(),$thisScript.' : Photo Upload');
  }
  $orig_im = $docroot . 'UserFiles/Photos/' . $user->user_id . '/'.$file;
  $originalImageSize = getimagesize($orig_im);
  if ($ext == 'gif') {
   //PIC
   $width = $shrink_w;
   $height = $shrink_h;
   list($width_orig, $height_orig) = $originalImageSize;
   if ($width && ($width_orig < $height_orig)) {
    $width = ($height / $height_orig) * $width_orig;
   } else {
    $height = ($width / $width_orig) * $height_orig;
   }
   $image_p = imagecreatetruecolor($width, $height);
   $image = imagecreatefromgif($orig_im);
   imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
   imagegif($image_p, $docroot . 'UserFiles/Photos/' . $user->user_id . '/'.$filenameThumb.'.gif');
   ImageDestroy($image_p);
  }
  if ($ext == 'png') {
   //PIC
   $width = $shrink_w;
   $height = $shrink_h;
   list($width_orig, $height_orig) = $originalImageSize;
   if ($width && ($width_orig < $height_orig)) {
    $width = ($height / $height_orig) * $width_orig;
   } else {
    $height = ($width / $width_orig) * $height_orig;
   }
   $image_p = imagecreatetruecolor($width, $height);
   $image = imagecreatefrompng($orig_im);
   imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
   imagepng($image_p, $docroot . 'UserFiles/Photos/' . $user->user_id . '/'.$filenameThumb.'.png', 9);
   ImageDestroy($image_p);
  }
  if ($ext == 'jpg') {
   //PIC
   $width = $shrink_w;
   $height = $shrink_h;
   list($width_orig, $height_orig) = $originalImageSize;
   if ($width && ($width_orig < $height_orig)) {
    $width = ($height / $height_orig) * $width_orig;
   } else {
    $height = ($width / $width_orig) * $height_orig;
   }
   $image_p = imagecreatetruecolor($width, $height);
   $image = imagecreatefromjpeg($orig_im);
   imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
   imagejpeg($image_p, $docroot . 'UserFiles/Photos/' . $user->user_id . '/'.$filenameThumb.'.jpg', 92);
   ImageDestroy($image_p);
  }
  if ($ext == 'jpeg') {
   //PIC
   $width = $shrink_w;
   $height = $shrink_h;
   list($width_orig, $height_orig) = $originalImageSize;
   if ($width && ($width_orig < $height_orig)) {
    $width = ($height / $height_orig) * $width_orig;
   } else {
    $height = ($width / $width_orig) * $height_orig;
   }
   $image_p = imagecreatetruecolor($width, $height);
   $image = imagecreatefromjpeg($orig_im);
   imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
   imagejpeg($image_p, $docroot . 'UserFiles/Photos/' . $user->user_id . '/'.$filenameThumb.'.jpeg', 92);
   ImageDestroy($image_p);
  }
  redirect($path_to.'photos.dash.php');
 }

Form:

  <div class="col2InnerHead">
   Upload New Photo
  </div>
  <div class="bodybigger"> </div>
  <div class="col2InnerRow">'.$m.'
         <center><form enctype="multipart/form-data" method="POST" action="pupload.php" name="form" id="form">
         <div class="Row100Center">*Max filesize: 1.2 MB and the width and height cannot exceed 800px.</div>
         <div class="bodybig"> </div>
         <div class="Row100Center"><a href="http://www.resizr.com/" target="_blank">Resize with <strong>Resizr</strong> for free.</a></div>
         <div class="bodybig"> </div>
         <div class="Row100Center"><input class="input-text" type="file" name="photo"><input type="hidden" name="action" value="upload_photo"></div>
         <div class="bodybig"> </div>
         <div class="Row100Center"><input type="checkbox" name="affirm" value="TRUE"> I affirm that this photo is family friendly.</div>
         <div class="bodybig"> </div>
         <div class="Row100Center"><input type="hidden" name="a" value="upload"><input class="submit" type="submit" name="submit" value="Upload New Profile Photo"></div>
         <div class="bodybig"> </div>
         </form></center>
        </div>

If you have Imagemagick installed you could try that as it seems to use less memory - or due to it being an external program.

From memory I could not resize an image over 2.8mb in one of the tests I did using GD but IM was happy at twice that size.