Browse Source

refactored Image helper.

tags/2.0.0-beta
Qiang Xue 11 years ago
parent
commit
afbcae0c7f
  1. 57
      extensions/yii/imagine/BaseImage.php

57
extensions/yii/imagine/BaseImage.php

@ -8,7 +8,6 @@
namespace yii\imagine; namespace yii\imagine;
use Yii; use Yii;
use Imagine\Exception\InvalidArgumentException;
use Imagine\Image\Box; use Imagine\Image\Box;
use Imagine\Image\Color; use Imagine\Image\Color;
use Imagine\Image\ImageInterface; use Imagine\Image\ImageInterface;
@ -66,7 +65,7 @@ class BaseImage
} }
/** /**
* @param Imagine\Image\ImagineInterface $imagine the `Imagine` object. * @param ImagineInterface $imagine the `Imagine` object.
*/ */
public static function setImagine($imagine) public static function setImagine($imagine)
{ {
@ -119,29 +118,20 @@ class BaseImage
* @param string $filename the image file path or path alias. * @param string $filename the image file path or path alias.
* @param integer $width the crop width * @param integer $width the crop width
* @param integer $height the crop height * @param integer $height the crop height
* @param array|Point $start the starting point. This can be either an array of `x` and `y` coordinates, or * @param array $start the starting point. This must be an array with two elements representing `x` and `y` coordinates.
* a `Point` object.
* @return ImageInterface * @return ImageInterface
* @throws InvalidParamException if the `$start` parameter is invalid * @throws InvalidParamException if the `$start` parameter is invalid
*/ */
public static function crop($filename, $width, $height, $start = [0, 0]) public static function crop($filename, $width, $height, array $start = [0, 0])
{ {
if (is_array($start)) { if (!isset($start[0], $start[1])) {
if (isset($start[0], $start[1])) {
$start = new Point($start[0], $start[1]);
} else {
throw new InvalidParamException('$start must be an array of two elements.'); throw new InvalidParamException('$start must be an array of two elements.');
} }
}
if ($start instanceof Point) {
return static::getImagine() return static::getImagine()
->open(Yii::getAlias($filename)) ->open(Yii::getAlias($filename))
->copy() ->copy()
->crop($start, new Box($width, $height)); ->crop(new Point($start[0], $start[1]), new Box($width, $height));
} else {
throw new InvalidParamException('$start must be either an array or an "Imagine\\Image\\Point" object.');
}
} }
/** /**
@ -188,63 +178,50 @@ class BaseImage
* Adds a watermark to an existing image. * Adds a watermark to an existing image.
* @param string $filename the image file path or path alias. * @param string $filename the image file path or path alias.
* @param string $watermarkFilename the file path or path alias of the watermark image. * @param string $watermarkFilename the file path or path alias of the watermark image.
* @param array|Point $start the starting point. This can be either an array of `x` and `y` coordinates, or * @param array $start the starting point. This must be an array with two elements representing `x` and `y` coordinates.
* a `Point` object.
* @return ImageInterface * @return ImageInterface
* @throws InvalidParamException if `$start` is invalid * @throws InvalidParamException if `$start` is invalid
*/ */
public static function watermark($filename, $watermarkFilename, $start = [0, 0]) public static function watermark($filename, $watermarkFilename, array $start = [0, 0])
{ {
if (is_array($start)) { if (!isset($start[0], $start[1])) {
if (isset($start[0], $start[1])) {
$start = new Point($start[0], $start[1]);
} else {
throw new InvalidParamException('$start must be an array of two elements.'); throw new InvalidParamException('$start must be an array of two elements.');
} }
}
if ($start instanceof Point) {
$img = static::getImagine()->open(Yii::getAlias($filename)); $img = static::getImagine()->open(Yii::getAlias($filename));
$watermark = static::getImagine()->open(Yii::getAlias($watermarkFilename)); $watermark = static::getImagine()->open(Yii::getAlias($watermarkFilename));
return $img->paste($watermark, $start); return $img->paste($watermark, new Point($start[0], $start[1]));
} else {
throw new InvalidParamException('$start must be either an array or an "Imagine\\Image\\Point" object.');
}
} }
/** /**
* Draws a text string on an existing image. * Draws a text string on an existing image.
* @param string $filename the image file path or path alias. * @param string $filename the image file path or path alias.
* @param string $text the text to write to the image * @param string $text the text to write to the image
* @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.
* @param array $fontOptions the font options. The following options may be specified: * @param array $fontOptions the font options. The following options may be specified:
* *
* - font: The path to the font file to use to style the text. This option is required.
* - color: The font color. Defaults to "fff". * - color: The font color. Defaults to "fff".
* - size: The font size. Defaults to 12. * - size: The font size. Defaults to 12.
* - x: The X position to write the text. Defaults to 5.
* - y: The Y position to write the text. Defaults to 5.
* - angle: The angle to use to write the text. Defaults to 0. * - angle: The angle to use to write the text. Defaults to 0.
* *
* @return ImageInterface * @return ImageInterface
* @throws InvalidParamException if `$fontOptions` is invalid * @throws InvalidParamException if `$fontOptions` is invalid
*/ */
public static function text($filename, $text, array $fontOptions) public static function text($filename, $text, $fontFile, array $start = [0, 0], array $fontOptions = [])
{ {
$font = ArrayHelper::getValue($fontOptions, 'font'); if (!isset($start[0], $start[1])) {
if ($font === null) { throw new InvalidParamException('$start must be an array of two elements.');
throw new InvalidParamException('$fontOptions must contain a "font" key specifying which font file to use.');
} }
$fontSize = ArrayHelper::getValue($fontOptions, 'size', 12); $fontSize = ArrayHelper::getValue($fontOptions, 'size', 12);
$fontColor = ArrayHelper::getValue($fontOptions, 'color', 'fff'); $fontColor = ArrayHelper::getValue($fontOptions, 'color', 'fff');
$fontPosX = ArrayHelper::getValue($fontOptions, 'x', 5);
$fontPosY = ArrayHelper::getValue($fontOptions, 'y', 5);
$fontAngle = ArrayHelper::getValue($fontOptions, 'angle', 0); $fontAngle = ArrayHelper::getValue($fontOptions, 'angle', 0);
$img = static::getImagine()->open(Yii::getAlias($filename)); $img = static::getImagine()->open(Yii::getAlias($filename));
$font = static::getImagine()->font(Yii::getAlias($font), $fontSize, new Color($fontColor)); $font = static::getImagine()->font(Yii::getAlias($fontFile), $fontSize, new Color($fontColor));
return $img->draw()->text($text, $font, new Point($fontPosX, $fontPosY), $fontAngle); return $img->draw()->text($text, $font, new Point($start[0], $start[1]), $fontAngle);
} }
/** /**
@ -255,7 +232,7 @@ class BaseImage
* @param integer $alpha the alpha value of the frame. * @param integer $alpha the alpha value of the frame.
* @return ImageInterface * @return ImageInterface
*/ */
public static function frame($filename, $margin = 5, $color = '000', $alpha = 100) public static function frame($filename, $margin = 20, $color = '666', $alpha = 100)
{ {
$img = static::getImagine()->open(Yii::getAlias($filename)); $img = static::getImagine()->open(Yii::getAlias($filename));

Loading…
Cancel
Save