/**
  * 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 (StringHelper::toLowerCase(IOHelper::getExtension($filePath)) == 'svg') {
         return true;
     }
     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;
 }