Beispiel #1
0
	/**
	 * Loads an image from a file system path.
	 *
	 * @param string $path
	 *
	 * @throws Exception
	 * @return Image
	 */
	public function loadImage($path)
	{
		if (!IOHelper::fileExists($path))
		{
			throw new Exception(Craft::t('No file exists at the path “{path}”', array('path' => $path)));
		}

		if (!craft()->images->checkMemoryForImage($path))
		{
			throw new Exception(Craft::t("Not enough memory available to perform this image operation."));
		}

		$extension = IOHelper::getExtension($path);

		if ($extension === 'svg')
		{
			if (!craft()->images->isImagick())
			{
				throw new Exception(Craft::t('The file “{path}” does not appear to be an image.', array('path' => $path)));
			}

			$svg = IOHelper::getFileContents($path);

			if ($this->minSvgWidth !== null && $this->minSvgHeight !== null)
			{
				// Does the <svg> node contain valid `width` and `height` attributes?
				list($width, $height) = ImageHelper::parseSvgSize($svg);

				if ($width !== null && $height !== null)
				{
					$scale = 1;

					if ($width < $this->minSvgWidth)
					{
						$scale = $this->minSvgWidth / $width;
					}

					if ($height < $this->minSvgHeight)
					{
						$scale = max($scale, ($this->minSvgHeight / $height));
					}

					$width = round($width * $scale);
					$height = round($height * $scale);

					$svg = preg_replace(ImageHelper::SVG_WIDTH_RE, "\${1}{$width}px\"", $svg);
					$svg = preg_replace(ImageHelper::SVG_HEIGHT_RE, "\${1}{$height}px\"", $svg);
				}
			}

			try
			{
				$this->_image = $this->_instance->load($svg);
			}
			catch (\Imagine\Exception\RuntimeException $e)
			{
				// Invalid SVG. Maybe it's missing its DTD?
				$svg = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'.$svg;
				$this->_image = $this->_instance->load($svg);
			}
		}
		else
		{
			$imageInfo = @getimagesize($path);

			if (!is_array($imageInfo))
			{
				throw new Exception(Craft::t('The file “{path}” does not appear to be an image.', array('path' => $path)));
			}

			$this->_image = $this->_instance->open($path);
		}

		$this->_extension = $extension;
		$this->_imageSourcePath = $path;

		if ($this->_extension == 'gif')
		{
			if (!craft()->images->isGd() && $this->_image->layers())
			{
				$this->_isAnimatedGif = true;
			}
		}

		return $this;
	}