fix bug: when load image file

PHP7 Windows A non well formed numeric value encountered
This commit is contained in:
githubjeka 2017-03-03 16:24:14 +03:00
parent 7c4ca2d825
commit bb06c36226
2 changed files with 38 additions and 14 deletions

View File

@ -113,6 +113,8 @@ class Helpers
*
* @param String $val
* @return int bytes
* @deprecated bug on PHP7 "A non well formed numeric value encountered"
* @see \humhub\libs\Helpers::getBytesOfIniValue instead
*/
public static function GetBytesOfPHPIniValue($val)
{
@ -130,6 +132,26 @@ class Helpers
return $val;
}
/**
* Returns bytes of a PHP Ini Setting Value
* E.g. 10M will converted into 10485760
*
* Source: http://php.net/manual/en/function.ini-get.php#96996
*
* @param string $valueString
* @return int bytes
*/
public static function getBytesOfIniValue($valueString)
{
switch (substr ($valueString, -1))
{
case 'M': case 'm': return (int)$valueString * 1048576;
case 'K': case 'k': return (int)$valueString * 1024;
case 'G': case 'g': return (int)$valueString * 1073741824;
default: return $valueString;
}
}
/**
* Returns a unique string
*
@ -150,7 +172,7 @@ class Helpers
public static function CheckClassType($className, $type = "")
{
$className = preg_replace('/[^a-z0-9_\-\\\]/i', "", $className);
if (is_array($type)) {
foreach ($type as $t) {
if (class_exists($className) && is_subclass_of($className, $t)) {

View File

@ -8,6 +8,7 @@
namespace humhub\modules\file\libs;
use humhub\libs\Helpers;
use Yii;
use yii\base\Exception;
@ -21,9 +22,9 @@ class ImageConverter
{
/** Max value of memory allowed to be allocated additional to the currently set memory limit in php.ini in MBytes. **/
const DEFAULT_MAX_ADDITIONAL_MEMORY_ALLOCATION = 64;
const DEFAULT_MAX_ADDITIONAL_MEMORY_ALLOCATION = 64;
const SETTINGS_NAME_MAX_MEMORY_ALLOCATION = 'maxImageProcessingMemoryAllocation';
/**
* Transforms given File to Jpeg
*
@ -67,7 +68,7 @@ class ImageConverter
*/
public static function Resize($sourceFile, $targetFile, $options = array())
{
if (!isset($options['width']))
$options['width'] = 0;
@ -76,7 +77,7 @@ class ImageConverter
if (!isset($options['mode']))
$options['mode'] = 'force';
if (Yii::$app->getModule('file')->settings->get('imageMagickPath')) {
self::ResizeImageMagick($sourceFile, $targetFile, $options);
} else {
@ -87,17 +88,17 @@ class ImageConverter
ini_set('memory_limit', $memoryLimit);
}
}
/**
* Dynamically allocate enough memory to process the given image.
*
*
* @throws Exception if the memory is not sufficient to process the image.
* @param String $sourceFile the source file.
* @param boolean $test if true the memory will not really be allocated and no exception will be thrown.
* @return boolean true if sufficient memory is available.
* @return boolean true if sufficient memory is available.
*/
public static function allocateMemory($sourceFile, $test = false) {
$width = 0;
$height = 0;
// buffer for memory needed by other stuff
@ -109,24 +110,25 @@ class ImageConverter
// check if the file exists, if not it seems that we do not have to allocate memory and we return true
if (!file_exists ( $sourceFile )) {
return true;
}
}
// getting the image width and height
list ($width, $height) = getimagesize($sourceFile);
// get defined memory limit from php_ini
$memoryLimit = ini_get('memory_limit');
// calc needed size for processing image dimensions in Bytes.
$memoryLimit = Helpers::getBytesOfIniValue($memoryLimit) * 1048576;
// calc needed size for processing image dimensions in Bytes.
$neededMemory = floor(($width * $height * $bytesPerPixel * $tweakFactor + 1048576) / 1048576);
$maxMemoryAllocation = Yii::$app->getModule('file')->settings->get(self::SETTINGS_NAME_MAX_MEMORY_ALLOCATION);
$maxMemoryAllocation = $maxMemoryAllocation == null ? self::DEFAULT_MAX_ADDITIONAL_MEMORY_ALLOCATION : $maxMemoryAllocation;
$newMemoryLimit = $memoryLimit + min($neededMemory, $maxMemoryAllocation);
// dynamically allocate memory to process image
$result = ini_set('memory_limit', $newMemoryLimit . 'M');
// check if we were able to set memory_limit with ini_set with the current server configuration
$failure = (version_compare(PHP_VERSION, '5.3.0') >= 0) ? false : '';
$allocatedMemory = $result == $failure ? $memoryLimit : $newMemoryLimit;
if($neededMemory + $buffer < $allocatedMemory) {
return true;
}