Browse Source

refactor methods to simplify interface

tags/2.0.0-beta
Antonio Ramirez 11 years ago
parent
commit
6694acd568
  1. 71
      extensions/yii/imagine/Image.php
  2. 4
      extensions/yii/imagine/composer.json

71
extensions/yii/imagine/Image.php

@ -77,11 +77,11 @@ class Image extends Component
private $_imagine; private $_imagine;
/** /**
* @var string the driver to use. These can be: * @var string the driver to use. These can be:
* - gd2 * - [[DRIVER_GD2]]
* - imagick * - [[DRIVER_IMAGICK]]
* - gmagick * - [[DRIVER_GMAGICK]]
*/ */
private $_driver = "gd2"; private $_driver = self::DRIVER_GD2;
/** /**
* Sets the driver. * Sets the driver.
@ -94,7 +94,7 @@ class Image extends Component
throw new InvalidConfigException( throw new InvalidConfigException(
strtr('"{class}::driver" should be string of these possible options "{drivers}", "{driver}" given.', [ strtr('"{class}::driver" should be string of these possible options "{drivers}", "{driver}" given.', [
'{class}' => get_class($this), '{class}' => get_class($this),
'{drivers}' => implode(', ', $this->getAvailableDrivers()), '{drivers}' => implode('", "', $this->getAvailableDrivers()),
'{driver}' => $driver '{driver}' => $driver
])); ]));
} }
@ -148,16 +148,36 @@ class Image extends Component
* @param string $filename the full path to the image file * @param string $filename the full path to the image file
* @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 integer $x position on image to crop from. Defaults to 0. * @param mixed $point. This argument can be both an array or an \Imagine\Image\Point type class, containing both
* @param integer $y position on image to crop from. Defaults to 0. * `x` and `y` coordinates. For example:
* ~~~
* // as array
* $obj->crop('path\to\image.jpg', 200, 200, [5, 5]);
* // as \Imagine\Image\Point
* $point = new \Imagine\Image\Point(5, 5);
* $obj->crop('path\to\image.jpg', 200, 200, $point);
* ~~~
* @return \Imagine\Image\ManipulatorInterface * @return \Imagine\Image\ManipulatorInterface
* @throws \InvalidArgumentException
*/ */
public function crop($filename, $width, $height, $x = 0, $y = 0) public function crop($filename, $width, $height, $point = null)
{ {
if(is_array($point)) {
list($x, $y) = $point;
$point = new Point($x, $y);
} elseif ($point === null) {
$point = new Point(0, 0);
} elseif (!$point instanceof Point ) {
throw new \InvalidArgumentException(
strtr('"{class}::crop()" "$point" if not null, should be an "array" or a "{type}" class type, containing both "x" and "y" coordinates.', [
'{class}' => get_class($this),
'{type}' => 'Imagine\\Image\\Point'
]));
}
return $this->getImagine() return $this->getImagine()
->open($filename) ->open($filename)
->copy() ->copy()
->crop(new Point($x, $y), new Box($width, $height)); ->crop($point, new Box($width, $height));
} }
/** /**
@ -207,11 +227,19 @@ class Image extends Component
* Note: If any of `$x` or `$y` parameters are null, bottom right position will be default. * Note: If any of `$x` or `$y` parameters are null, bottom right position will be default.
* @param string $filename the full path to the image file to apply the watermark to * @param string $filename the full path to the image file to apply the watermark to
* @param string $watermarkFilename the full path to the image file to apply as watermark * @param string $watermarkFilename the full path to the image file to apply as watermark
* @param integer $x position on image to apply watermark. Defaults to null. * @param mixed $point. This argument can be both an array or an \Imagine\Image\Point type class, containing both
* @param integer $y position on image to apply watermark. Defaults to null * `x` and `y` coordinates. For example:
* ~~~
* // as array
* $obj->watermark('path\to\image.jpg', 'path\to\watermark.jpg', [5, 5]);
* // as \Imagine\Image\Point
* $point = new \Imagine\Image\Point(5, 5);
* $obj->watermark('path\to\image.jpg', 'path\to\watermark.jpg', $point);
* ~~~
* @return ManipulatorInterface * @return ManipulatorInterface
* @throws \InvalidArgumentException
*/ */
public function watermark($filename, $watermarkFilename, $x = null, $y = null) public function watermark($filename, $watermarkFilename, $point = null)
{ {
$img = $this->getImagine()->open($filename); $img = $this->getImagine()->open($filename);
$watermark = $this->getImagine()->open($watermarkFilename); $watermark = $this->getImagine()->open($watermarkFilename);
@ -220,11 +248,22 @@ class Image extends Component
$wSize = $watermark->getSize(); $wSize = $watermark->getSize();
// if x or y position was not given, set its bottom right by default // if x or y position was not given, set its bottom right by default
$pos = $x === null || $y === null if(is_array($point)) {
? new Point($size->getWidth() - $wSize->getWidth() , $size->getHeight() - $wSize->getHeight()) list($x, $y) = $point;
: new Point($x, $y); $point = new Point($x, $y);
} elseif ($point === null) {
$x = $size->getWidth() - $wSize->getWidth();
$y = $size->getHeight() - $wSize->getHeight();
$point = new Point($x, $y);
} elseif (!$point instanceof Point) {
throw new \InvalidArgumentException(
strtr('"{class}::watermark()" "$point" if not null, should be an "array" or a "{type}" class type, containing both "x" and "y" coordinates.', [
'{class}' => get_class($this),
'{type}' => 'Imagine\\Image\\Point'
]));
}
return $img->paste($watermark, $pos); return $img->paste($watermark, $point);
} }
/** /**

4
extensions/yii/imagine/composer.json

@ -22,7 +22,9 @@
"imagine/imagine": "v0.5.0" "imagine/imagine": "v0.5.0"
}, },
"autoload": { "autoload": {
"psr-0": { "yii\\imagine\\": "" } "psr-0": {
"yii\\imagine\\": ""
}
}, },
"target-dir": "yii/imagine" "target-dir": "yii/imagine"
} }

Loading…
Cancel
Save