BaseImage.php 7.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

namespace yii\imagine;

use Yii;
use Imagine\Image\Box;
use Imagine\Image\Color;
use Imagine\Image\ImageInterface;
use Imagine\Image\ImagineInterface;
use Imagine\Image\ManipulatorInterface;
use Imagine\Image\Point;
use yii\base\InvalidConfigException;
use yii\base\InvalidParamException;
use yii\helpers\ArrayHelper;

/**
 * BaseImage provides concrete implementation for [[Image]].
 *
 * Do not use BaseImage. Use [[Image]] instead.
 *
 * @author Antonio Ramirez <amigo.cobos@gmail.com>
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class BaseImage
{
	/**
	 * GD2 driver definition for Imagine implementation using the GD library.
	 */
	const DRIVER_GD2 = 'gd2';
	/**
	 * imagick driver definition.
	 */
	const DRIVER_IMAGICK = 'imagick';
	/**
	 * gmagick driver definition.
	 */
	const DRIVER_GMAGICK = 'gmagick';
	/**
	 * @var array|string the driver to use. This can be either a single driver name or an array of driver names.
	 * If the latter, the first available driver will be used.
	 */
	public static $driver = [self::DRIVER_GMAGICK, self::DRIVER_IMAGICK, self::DRIVER_GD2];

	/**
	 * @var ImagineInterface instance.
	 */
	private static $_imagine;

	/**
	 * Returns the `Imagine` object that supports various image manipulations.
	 * @return ImagineInterface the `Imagine` object
	 */
	public static function getImagine()
	{
Qiang Xue committed
61 62
		if (self::$_imagine === null) {
			self::$_imagine = static::createImagine();
63
		}
Qiang Xue committed
64
		return self::$_imagine;
65 66 67
	}

	/**
Qiang Xue committed
68
	 * @param ImagineInterface $imagine the `Imagine` object.
69 70 71
	 */
	public static function setImagine($imagine)
	{
Qiang Xue committed
72
		self::$_imagine = $imagine;
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	}

	/**
	 * Creates an `Imagine` object based on the specified [[driver]].
	 * @return ImagineInterface the new `Imagine` object
	 * @throws InvalidConfigException if [[driver]] is unknown or the system doesn't support any [[driver]].
	 */
	protected static function createImagine()
	{
		foreach ((array)static::$driver as $driver) {
			switch ($driver) {
				case self::DRIVER_GMAGICK:
					if (class_exists('Gmagick', false)) {
						return new \Imagine\Gmagick\Imagine();
					}
					break;
				case self::DRIVER_IMAGICK:
Qiang Xue committed
90
					if (class_exists('Imagick', false)) {
91 92 93 94
						return new \Imagine\Imagick\Imagine();
					}
					break;
				case self::DRIVER_GD2:
Qiang Xue committed
95
					if (function_exists('gd_info')) {
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
						return new \Imagine\Gd\Imagine();
					}
					break;
				default:
					throw new InvalidConfigException("Unknown driver: $driver");
			}
		}
		throw new InvalidConfigException("Your system does not support any of these drivers: " . implode(',', (array)static::$driver));
	}

	/**
	 * Crops an image.
	 *
	 * For example,
	 *
	 * ~~~
	 * $obj->crop('path\to\image.jpg', 200, 200, [5, 5]);
	 *
	 * $point = new \Imagine\Image\Point(5, 5);
	 * $obj->crop('path\to\image.jpg', 200, 200, $point);
	 * ~~~
	 *
	 * @param string $filename the image file path or path alias.
	 * @param integer $width the crop width
	 * @param integer $height the crop height
Qiang Xue committed
121
	 * @param array $start the starting point. This must be an array with two elements representing `x` and `y` coordinates.
122 123 124
	 * @return ImageInterface
	 * @throws InvalidParamException if the `$start` parameter is invalid
	 */
Qiang Xue committed
125
	public static function crop($filename, $width, $height, array $start = [0, 0])
126
	{
Qiang Xue committed
127 128
		if (!isset($start[0], $start[1])) {
			throw new InvalidParamException('$start must be an array of two elements.');
129 130
		}

Qiang Xue committed
131 132 133 134
		return static::getImagine()
			->open(Yii::getAlias($filename))
			->copy()
			->crop(new Point($start[0], $start[1]), new Box($width, $height));
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
	}

	/**
	 * Creates a thumbnail image. The function differs from [[\Imagine\Image\ImageInterface::thumbnail()]] function that
	 * it keeps the aspect ratio of the image.
	 * @param string $filename the image file path or path alias.
	 * @param integer $width the width in pixels to create the thumbnail
	 * @param integer $height the height in pixels to create the thumbnail
	 * @param string $mode
	 * @return ImageInterface
	 */
	public static function thumbnail($filename, $width, $height, $mode = ManipulatorInterface::THUMBNAIL_OUTBOUND)
	{
		$box = new Box($width, $height);
		$img = static::getImagine()->open(Yii::getAlias($filename));

		if (($img->getSize()->getWidth() <= $box->getWidth() && $img->getSize()->getHeight() <= $box->getHeight()) || (!$box->getWidth() && !$box->getHeight())) {
			return $img->copy();
		}

		$img = $img->thumbnail($box, $mode);

		// create empty image to preserve aspect ratio of thumbnail
158
		$thumb = static::getImagine()->create($box, new Color('FFF', 100));
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

		// calculate points
		$size = $img->getSize();

		$startX = 0;
		$startY = 0;
		if ($size->getWidth() < $width) {
			$startX = ceil($width - $size->getWidth()) / 2;
		}
		if ($size->getHeight() < $height) {
			$startY = ceil($height - $size->getHeight()) / 2;
		}

		$thumb->paste($img, new Point($startX, $startY));

		return $thumb;
	}

	/**
	 * Adds a watermark to an existing image.
	 * @param string $filename the image file path or path alias.
	 * @param string $watermarkFilename the file path or path alias of the watermark image.
Qiang Xue committed
181
	 * @param array $start the starting point. This must be an array with two elements representing `x` and `y` coordinates.
182 183 184
	 * @return ImageInterface
	 * @throws InvalidParamException if `$start` is invalid
	 */
Qiang Xue committed
185
	public static function watermark($filename, $watermarkFilename, array $start = [0, 0])
186
	{
Qiang Xue committed
187 188
		if (!isset($start[0], $start[1])) {
			throw new InvalidParamException('$start must be an array of two elements.');
189 190
		}

Qiang Xue committed
191 192
		$img = static::getImagine()->open(Yii::getAlias($filename));
		$watermark = static::getImagine()->open(Yii::getAlias($watermarkFilename));
Qiang Xue committed
193 194
		$img->paste($watermark, new Point($start[0], $start[1]));
		return $img;
195 196 197 198 199 200
	}

	/**
	 * Draws a text string on an existing image.
	 * @param string $filename the image file path or path alias.
	 * @param string $text the text to write to the image
Qiang Xue committed
201 202
	 * @param string $fontFile the file path or path alias
	 * @param array $start the starting position of the text. This must be an array with two elements representing `x` and `y` coordinates.
203 204 205 206 207 208 209 210 211
	 * @param array $fontOptions the font options. The following options may be specified:
	 *
	 * - color: The font color. Defaults to "fff".
	 * - size: The font size. Defaults to 12.
	 * - angle: The angle to use to write the text. Defaults to 0.
	 *
	 * @return ImageInterface
	 * @throws InvalidParamException if `$fontOptions` is invalid
	 */
Qiang Xue committed
212
	public static function text($filename, $text, $fontFile, array $start = [0, 0], array $fontOptions = [])
213
	{
Qiang Xue committed
214 215
		if (!isset($start[0], $start[1])) {
			throw new InvalidParamException('$start must be an array of two elements.');
216 217 218 219 220 221 222
		}

		$fontSize = ArrayHelper::getValue($fontOptions, 'size', 12);
		$fontColor = ArrayHelper::getValue($fontOptions, 'color', 'fff');
		$fontAngle = ArrayHelper::getValue($fontOptions, 'angle', 0);

		$img = static::getImagine()->open(Yii::getAlias($filename));
Qiang Xue committed
223
		$font = static::getImagine()->font(Yii::getAlias($fontFile), $fontSize, new Color($fontColor));
224

Qiang Xue committed
225 226 227
		$img->draw()->text($text, $font, new Point($start[0], $start[1]), $fontAngle);

		return $img;
228 229 230 231 232 233 234 235 236 237
	}

	/**
	 * Adds a frame around of the image. Please note that the image size will increase by `$margin` x 2.
	 * @param string $filename the full path to the image file
	 * @param integer $margin the frame size to add around the image
	 * @param string $color the frame color
	 * @param integer $alpha the alpha value of the frame.
	 * @return ImageInterface
	 */
Qiang Xue committed
238
	public static function frame($filename, $margin = 20, $color = '666', $alpha = 100)
239 240 241 242 243 244 245 246 247 248 249 250
	{
		$img = static::getImagine()->open(Yii::getAlias($filename));

		$size = $img->getSize();

		$pasteTo = new Point($margin, $margin);
		$padColor = new Color($color, $alpha);

		$box = new Box($size->getWidth() + ceil($margin * 2), $size->getHeight() + ceil($margin * 2));

		$image = static::getImagine()->create($box, $padColor);

Qiang Xue committed
251 252 253
		$image->paste($img, $pasteTo);

		return $image;
254 255
	}
}