Magento will automatically resize product images based on dimensions specified in templates so that customers aren't downloading large source images when all they need is a small thumbnail. However, we've discovered an issue with recent versions of Magento which will sometimes cause a "Memory Limit has been reached" error when attempting to resize images through a PHP command line script.
This issue was introduced in Magento CE 1.8.0.0 and EE 1.13.0.0 versions, and will normally only occur with code that is run from the CLI. The default PHP CLI memory limit is often set to -1 so that there is no memory limit applied. Reviewing the relevant code we found that Magento was using the following method to determine if it had enough memory to resize images:
protected function _isMemoryLimitReached()
{
$limit = $this->_convertToByte(ini_get('memory_limit'));
$size = getimagesize($this->_fileName);
$requiredMemory = $size[0] * $size[1] * 3;
return (memory_get_usage(true) + $requiredMemory) > $limit;
}
If the memory limit is set to -1, this code will return true, as the required memory will always be greater than -1. To fix this issue, you simply need to adjust the function to include an extra check for the -1 memory limit:
protected function _isMemoryLimitReached()
{
$limit = $this->_convertToByte(ini_get('memory_limit'));
+ // If the memory limit is -1 we want to return false as this value means we have no memory limit.
+ if ($limit === -1) {
+ return false;
+ }
$size = getimagesize($this->_fileName);
$requiredMemory = $size[0] * $size[1] * 3;
return (memory_get_usage(true) + $requiredMemory) > $limit;
}
You can download a patch to fix the memory limit error when resizing images and apply it with the following command from your Magento root directory:
patch -p0 > image_memory_limit.patch
It should be noted that this bug will only come up if Magento is attempting to resize an image which doesn't already exist. If Magento had previously generated that image size (such as through a web request), then it won't try to do so again and you won't run into the problem. This issue has already been submitted to the Magento bug tracker, so hopefully a fix will be included in a future release. The Mercator project has been updated with the patch as well.