jQuery-File-Upload icon indicating copy to clipboard operation
jQuery-File-Upload copied to clipboard

Fatal error memory limit when uploading with UploadHandler.php

Open cfconsultancy opened this issue 5 years ago • 1 comments

When uploading a too big image I get the following error

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 28672 bytes) in I:\Apache242\htdocs\cfcmssmallpro_thumb_mysqli_utf4_n\cms\tinymce\filemanager\UploadHandler.php on line 596

That is this line $this->image_objects[$file_path] = $func($file_path);

so I think there should be a check for memory limit and the return false to stop the script

cfconsultancy avatar Feb 17 '20 15:02 cfconsultancy

So there is a way to check the approximate amount of memory that is required to load the image into memory and compare it to the available memory:

$imageInfo = getimagesize($file_path);

// bytes = width * height * bits * channels / 8 * overhead_factor
$requiredMemory = ceil($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $imageInfo['channels'] / 8 * 1.5);

// bytes = memory_limit_mega_bytes * 1024 * 1024 - memory_used_bytes
$availableMemory = (int)ini_get('memory_limit') * 1024 * 1024 - memory_get_usage();

if ($requiredMemory > $availableMemory) {
  // return error
}

Alternatively, we could also catch fatal errors with register_shutdown_function and error_get_last.

I've marked this as a feature request.

In the meantime, you can limit the images to process with the max_width and max_height options, which have the biggest impact on the required memory for image scaling.

blueimp avatar Feb 18 '20 12:02 blueimp