VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Topic started by: stAn99 on February 25, 2016, 14:28:43 PM

Title: memory leaks when creating thumbs
Post by: stAn99 on February 25, 2016, 14:28:43 PM
hello, many of my customers come around issues when creating thumbs caused by using too large images (20M jpegs) and for this reason i'd like to suggest to check if there is enough of RAM before the image processing functions are used.


$imageInfo = GetImageSize($file);

/* returns true or false on if we have enough memory to work with the image */
public static function checkRam($imageInfo)
{
if (!isset($imageInfo[0])) $imageInfo[0] = 1;
if (!isset($imageInfo['bits'])) $imageInfo['bits'] = 32;
if (!isset($imageInfo[1])) $imageInfo[0] = 1;
if (!isset($imageInfo['channels'])) $imageInfo['channels'] = 1;

$memoryNeeded = Round(($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $imageInfo['channels'] / 8 + Pow(2, 16)) * 1.65);
if (function_exists('memory_get_usage'))
{
  $ramnneeded = memory_get_usage() + $memoryNeeded;
$memory_limit = ini_get('memory_limit');
if (empty($memory_limit)) return true;

$val = trim($memory_limit);
    $last = strtolower($val[strlen($val)-1]);
    switch($last) {
        // The 'G' modifier is available since PHP 5.1.0
        case 'g':
            $val *= 1024;
        case 'm':
            $val *= 1024;
        case 'k':
            $val *= 1024;
    }
$memory_limit = $val;
 
 
  if ($ramnneeded > $memory_limit) return false;
}
return true;
}



best regards, stan