News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

memory leaks when creating thumbs

Started by stAn99, February 25, 2016, 14:28:43 PM

Previous topic - Next topic

stAn99

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
----
RuposTel.com
www.rupostel.com
Your customized checkout solution for Virtuemart