FATAL ERROR - image upload/ allowed memory size - bytes exhausted

10 posts by 2 authors in: Forums > CMS Builder
Last Post: February 8, 2019   (RSS)

By csdesign - February 6, 2019

Hello! 

If I could get some help ASAP on this it would be greatly appreciated! 

I'm getting errors while trying to upload images. Upload max image size is 7MB and image size is around 2MB. However, when I try a 178kb image, that works fine. 
I guess I'm not understanding if this is a cmsadmin issue or a server issue. 

++++

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 12288 bytes) in /home/codyhors/public_html/cmsb/lib/image_functions.php on line 130 Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 73728 bytes) in /home/codyhors/public_html/cmsb/lib/errorlog_functions.php on line 322

lines 128-141 of "image_functions.php"

  if (empty($src_image) || empty($dst_image) || $quality <= 0) { return false; }
  if ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h)) {
    $temp = imagecreatetruecolor ($dst_w * $quality + 1, $dst_h * $quality + 1);
    _image_saveTransparency($temp, $src_image, $imageType);
    imagecopyresized ($temp, $src_image, 0, 0, $src_x, $src_y, $dst_w * $quality + 1, $dst_h * $quality + 1, $src_w, $src_h);
    imagecopyresampled ($dst_image, $temp, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $dst_w * $quality, $dst_h * $quality);
    imagedestroy($temp);
  }
  else {
    imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
  }

  return true;
}
lines 320 - 330 of "errorlog_functions.php"

  // create index of nums to names
  if (!$numsToNames) {
    foreach (get_defined_constants() as $name => $num) {  
      if (preg_match("/^E_/", $name)) { $numsToNames[$num] = $name; }
    }
  }
  
  //
  if (array_key_exists($errno, $numsToNames)) { return $numsToNames[$errno]; }
  else                                        { return $errno; }
}

++++

I'm running v3.11 (Build 2156).  I did find a reference for errors like this and I cleared the error logs and fixed a reoccurring header error but still getting the fatal errors. 

https://www.interactivetools.com/forum/forum-posts.php?postNum=2242679#post2242679
• Admin Men > General > Security Settings > View Error Log (CLEARED ALL)

Thanks for the help!!  Tina

By csdesign - February 6, 2019

and the error logs are showing this error for the detail page... and I've double checked this. Can't find the issue because the images have been posting up to this point and the code appears to be correct. I haven't changed it for quite awhile.  Not sure if these errors are related to the fatal error codes or not. 

By daniel - February 7, 2019 - edited: February 7, 2019

Hi Tina,

Cases like this are most often caused by the fact that image uploading/resizing with PHP can use a very large amount of memory, and it's based on the dimensions of the image rather than its file size. Your memory limit (128MB) is a decent amount and so should be able to handle most images, but some very large ones (4500x4500+ pixels) could still exceed it, even while fitting under your 7MB upload size limit.

If you're only having issues with this image (or a relatively small number), my first suggestion would be just to try sizing them down before uploading. That a 178kb image still uploads fine makes me think that this should work. If you're dealing with a large number of images, it might instead be a better option to increase the memory limit for PHP.

I hope that gives you some better info to move forward with; let me know if you have any additional questions!

Thanks,

Daniel
Technical Lead
interactivetools.com

By csdesign - February 7, 2019

Hi Daniel, Thanks for the quick reply!  You definitely nailed it.  I took one of the images that was 5633px x 3532px at 1.9MB and resized it to 1200px x 600px approx at 2.9MB and it loaded right up. 

The reason we have the file size set so high is that multiple sellers upload all their own photos. The main photo stays on the server and is then used for the print catalog and we use resized thumbnails for the website. Since most users don't have photoshop or maybe the knowledge in resizing correctly is there any way to set the memory limit higher to adjust for those individuals? How do I increase the memory limit for PHP? 

Thanks again!!!  Tina

By daniel - February 8, 2019

Hi Tina,

If your limit is at 128MB then it's likely being set in /cmsb/lib/init.php. Search the file for a line that looks like this:

ini_set('memory_limit', '128M');

And update as you need. You should be able to see on the General Settings page in the CMSB Admin under "File size limits" if the change is taking effect.

Note that this file will likely be overwritten during a CMSB upgrade, so if you upgrade in the future make sure to double check this in case it needs to be changed again.

Let me know if that does the trick!

Thanks,

Daniel
Technical Lead
interactivetools.com

By csdesign - February 8, 2019

I doubled it it to 256 and it works perfect. I just tested with the previous large dimension images that I was getting an error on and now it work. Thanks SO much!! 

By csdesign - February 8, 2019

I had thought part of this (the error log) was causing the issue to begin with because it hadn't been cleared but now I see it's not. I don't really see why this error is being generated and is this going to cause any issues long term?  (see screenshot) 

By daniel - February 8, 2019

Hi Tina,

It's tough to say why that error specifically is being generated without seeing the code, but my best guess is that there's a foreach() being passed a variable that is either sometimes or always empty. I can't guarantee that this is a harmless error, though if the page in question is functioning as you expect, then it's not likely to be something critical. If you simply want to silence the error, you can this line ahead of the foreach() in question (at line 207):

if (empty( $foo ) || !is_array( $foo )) { $foo = array(); }

You will need to change "$foo" to match the actual variable being supplied to the foreach() in your code. 

Let me know if you have any further questions!

Thanks,

Daniel
Technical Lead
interactivetools.com

By csdesign - February 8, 2019

awesome! Thank you!!!!