Skip to content
This repository has been archived by the owner on May 25, 2023. It is now read-only.

Fatal error memory limit when uploading with UploadHandler.php #3562

Open
cfconsultancy opened this issue Feb 17, 2020 · 1 comment
Open
Labels

Comments

@cfconsultancy
Copy link

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

@blueimp
Copy link
Owner

blueimp commented Feb 18, 2020

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants