/** * Allocates memory (if required). * * @param int $bytesRequired * @param int $extra in bytes * @return bool|null true if enough memory, false if could not allocate, null if there is no way to know */ public static function allocateMemory($bytesRequired, $extra = 0x1000000) { $memoryLimit = \Ip\Internal\System\Helper\SystemInfo::getMemoryLimit(); if (!function_exists('memory_get_usage') && $memoryLimit !== '-1') { //try to allocate as much as we can ini_set('memory_limit', '100M'); ini_set('memory_limit', '150M'); ini_set('memory_limit', '200M'); ini_set('memory_limit', '500M'); return null; // We can't calculate how much memory should be allocated } if ('-1' == $memoryLimit) { // unlimited return true; } $memoryRequired = memory_get_usage() + $bytesRequired; if ($memoryRequired < $memoryLimit) { return true; } $megabytesNeeded = ceil($memoryRequired + $extra / 0x100000) . 'M'; if (!ini_set('memory_limit', $megabytesNeeded)) { ipLog()->warning('Could not allocate enough memory. Please increase memory limit to {memoryNeeded}', array('memoryNeeded' => $megabytesNeeded)); return false; } return true; }
/** * Takes memory required to process supplied image file and a bit more for future PHP operations. * @param resource $imageFile * @return bool true on success */ protected function getMemoryNeeded($imageFile) { if (!file_exists($imageFile)) { return 0; } $imageInfo = getimagesize($imageFile); if (!isset($imageInfo['channels']) || !$imageInfo['channels']) { $imageInfo['channels'] = 4; } if (!isset($imageInfo['bits']) || !$imageInfo['bits']) { $imageInfo['bits'] = 8; } if (!isset($imageInfo[0])) { $imageInfo[0] = 1; } if (!isset($imageInfo[1])) { $imageInfo[1] = 1; } $memoryNeeded = round(($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $imageInfo['channels'] / 8 + Pow(2, 16)) * 1.65); $success = \Ip\Internal\System\Helper\SystemInfo::allocateMemory($memoryNeeded); return $success; }
public static function checkMemoryLimit() { if (\Ip\Internal\System\Helper\SystemInfo::getMemoryLimitAsMb() < 100 && \Ip\Internal\System\Helper\SystemInfo::getMemoryLimitAsMb() != -1) { return 'warning'; } return 'success'; }
/** * @param $imageFile * @return bool|null */ public static function getMemoryNeeded($imageFile) { $imageInfo = getimagesize($imageFile); if (!isset($imageInfo['channels']) || !$imageInfo['channels']) { $imageInfo['channels'] = 4; } if (!isset($imageInfo['bits']) || !$imageInfo['bits']) { $imageInfo['bits'] = 8; } if (!isset($imageInfo[0])) { $imageInfo[0] = 1; } if (!isset($imageInfo[1])) { $imageInfo[1] = 1; } $a64kb = 65536; $bytesNeeded = round(($imageInfo[0] * $imageInfo[1] * $imageInfo['bits'] * $imageInfo['channels'] / 8 + $a64kb) * 1.65); return \Ip\Internal\System\Helper\SystemInfo::allocateMemory($bytesNeeded); }