#18402013 : added estimation function called before loading image to create thumbnail (fixed a crash occurred when creating a thumbnail of large image file)

git-svn-id: http://xe-core.googlecode.com/svn/sandbox@6899 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
haneul 2009-10-29 04:49:13 +00:00
parent fd50fdd9e3
commit 8cd154be31

View file

@ -397,6 +397,40 @@
return true;
}
/**
* @brief convert size in string into numeric value
* @param[in] $val size in string (ex., 10, 10K, 10M, 10G )
* @return converted size
*/
function returnBytes($val)
{
$val = trim($val);
$last = strtolower(substr($val, -1));
if($last == 'g') $val *= 1024*1024*1024;
else if($last == 'm') $val *= 1024*1024;
else if($last == 'k') $val *= 1024;
return $val;
}
/**
* @brief check available memory to load image file
* @param[in] $imageInfo image info retrieved by getimagesize function
* @return true: it's ok, false: otherwise
*/
function checkMemoryLoadImage(&$imageInfo)
{
if(!function_exists('memory_get_usage')) return true;
$K64 = 65536;
$TWEAKFACTOR = 1.5;
$channels = $imageInfo['channels'];
if(!$channels) $channels = 6; //for png
$memoryNeeded = round( ($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $channels / 8 + $K64 ) * $TWEAKFACTOR );
$availableMemory = FileHandler::returnBytes(ini_get('memory_limit')) - memory_get_usage();
if($availableMemory < $memoryNeeded) return false;
return true;
}
/**
* @brief moves an image file (resizing is possible)
* @param[in] $source_file path of the source file
@ -416,7 +450,10 @@
if(!$resize_height) $resize_height = $resize_width;
// retrieve source image's information
list($width, $height, $type, $attrs) = @getimagesize($source_file);
$imageInfo = getimagesize($source_file);
if(!FileHandler::checkMemoryLoadImage($imageInfo)) return false;
list($width, $height, $type, $attrs) = $imageInfo;
if($width<1 || $height<1) return;
switch($type) {