[-] BO : #PSCFV-2716 : Fix memory limit when upload image file

This commit is contained in:
lLefevre
2012-06-07 12:12:41 +00:00
parent ef8a2b6a43
commit c70a501211
4 changed files with 42 additions and 11 deletions
+29 -10
View File
@@ -51,16 +51,9 @@ class ImageManagerCore
{
$infos = getimagesize($image);
$memory_limit = Tools::getMemoryLimit();
// memory_limit == -1 => unlimited memory
if (function_exists('memory_get_usage') && (int)$memory_limit != -1)
{
$current_memory = memory_get_usage();
// Evaluate the memory required to resize the image: if it's too much, you can't resize it.
if (($infos[0] * $infos[1] * $infos['bits'] * (isset($infos['channels']) ? ($infos['channels'] / 8) : 1) + pow(2, 16)) * 1.8 + $current_memory > $memory_limit - 1024 * 1024)
return false;
}
// Evaluate the memory required to resize the image: if it's too much, you can't resize it.
if (!ImageManager::checkImageMemoryLimit($image))
return false;
$x = $infos[0];
$y = $infos[1];
@@ -85,6 +78,32 @@ class ImageManagerCore
return '<img src="'._PS_TMP_IMG_.$cache_image.(!$disable_cache ? '?time='.time() : '').'" alt="" class="imgm" />';
}
/**
* Check if memory limit is too long or not
*
* @static
* @param $image
* @return bool
*/
public static function checkImageMemoryLimit($image)
{
$infos = getimagesize($image);
$memory_limit = Tools::getMemoryLimit();
// memory_limit == -1 => unlimited memory
if (function_exists('memory_get_usage') && (int)$memory_limit != -1)
{
$current_memory = memory_get_usage();
$channel = isset($infos['channels']) ? ($infos['channels'] / 8) : 1;
// Evaluate the memory required to resize the image: if it's too much, you can't resize it.
if (($infos[0] * $infos[1] * $infos['bits'] * $channel + pow(2, 16)) * 1.8 + $current_memory > $memory_limit - 1024 * 1024)
return false;
}
return true;
}
/**
* Resize, cut and optimize image
*
+6 -1
View File
@@ -2463,9 +2463,14 @@ class AdminControllerCore extends Controller
if (!move_uploaded_file($_FILES[$name]['tmp_name'], $tmp_name))
return false;
// Evaluate the memory required to resize the image: if it's too much, you can't resize it.
if (!ImageManager::checkImageMemoryLimit($tmp_name))
$this->errors[] = Tools::displayError('This image cannot be loaded due to memory limit restrictions, please increase your memory_limit value on your server configuration.');
// Copy new image
if (!ImageManager::resize($tmp_name, _PS_IMG_DIR_.$dir.$id.'.'.$this->imageType, (int)$width, (int)$height, ($ext ? $ext : $this->imageType)))
if (empty($this->errors) && !ImageManager::resize($tmp_name, _PS_IMG_DIR_.$dir.$id.'.'.$this->imageType, (int)$width, (int)$height, ($ext ? $ext : $this->imageType)))
$this->errors[] = Tools::displayError('An error occurred while uploading image.');
if (count($this->errors))
return false;
if ($this->afterImageUpload())