Esempio n. 1
0
 /**
  * Determines if there is enough memory to process this image.
  *
  * The code was adapted from http://www.php.net/manual/en/function.imagecreatefromjpeg.php#64155. It will first
  * attempt to do it with available memory. If that fails, Craft will bump the memory to amount defined by the
  * [phpMaxMemoryLimit](http://buildwithcraft.com/docs/config-settings#phpMaxMemoryLimit) config setting, then try again.
  *
  * @param string $filePath The path to the image file.
  * @param bool   $toTheMax If set to true, will set the PHP memory to the config setting phpMaxMemoryLimit.
  *
  * @return bool
  */
 public function checkMemoryForImage($filePath, $toTheMax = false)
 {
     if (!function_exists('memory_get_usage')) {
         return false;
     }
     if ($toTheMax) {
         // Turn it up to 11.
         craft()->config->maxPowerCaptain();
     }
     // Find out how much memory this image is going to need.
     $imageInfo = getimagesize($filePath);
     $K64 = 65536;
     $tweakFactor = 1.7;
     $bits = isset($imageInfo['bits']) ? $imageInfo['bits'] : 8;
     $channels = isset($imageInfo['channels']) ? $imageInfo['channels'] : 4;
     $memoryNeeded = round(($imageInfo[0] * $imageInfo[1] * $bits * $channels / 8 + $K64) * $tweakFactor);
     $memoryLimit = AppHelper::getPhpConfigValueInBytes('memory_limit');
     if (memory_get_usage() + $memoryNeeded < $memoryLimit) {
         return true;
     }
     if (!$toTheMax) {
         return $this->checkMemoryForImage($filePath, true);
     }
     // Oh well, we tried.
     return false;
 }
Esempio n. 2
0
 /**
  * Return max upload size in bytes.
  *
  * @return int
  */
 public function getMaxUploadSize()
 {
     $maxUpload = AppHelper::getPhpConfigValueInBytes('upload_max_filesize');
     $maxPost = AppHelper::getPhpConfigValueInBytes('post_max_size');
     $memoryLimit = AppHelper::getPhpConfigValueInBytes('memory_limit');
     $uploadInBytes = min($maxUpload, $maxPost);
     if ($memoryLimit > 0) {
         $uploadInBytes = min($uploadInBytes, $memoryLimit);
     }
     $configLimit = (int) craft()->config->get('maxUploadFileSize');
     if ($configLimit) {
         $uploadInBytes = min($uploadInBytes, $configLimit);
     }
     return $uploadInBytes;
 }