Commit 6694acd5 by Antonio Ramirez

refactor methods to simplify interface

parent c7b3d2b4
...@@ -77,11 +77,11 @@ class Image extends Component ...@@ -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 ...@@ -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 ...@@ -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 ...@@ -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 ...@@ -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);
} }
/** /**
......
{ {
"name": "yiisoft/yii2-imagine", "name": "yiisoft/yii2-imagine",
"description": "The Imagine integration for the Yii framework", "description": "The Imagine integration for the Yii framework",
"keywords": ["yii", "imagine", "image", "helper"], "keywords": ["yii", "imagine", "image", "helper"],
"type": "yii2-extension", "type": "yii2-extension",
"license": "BSD-3-Clause", "license": "BSD-3-Clause",
"support": { "support": {
"issues": "https://github.com/yiisoft/yii2/issues?labels=ext%3Aimagine", "issues": "https://github.com/yiisoft/yii2/issues?labels=ext%3Aimagine",
"forum": "http://www.yiiframework.com/forum/", "forum": "http://www.yiiframework.com/forum/",
"wiki": "http://www.yiiframework.com/wiki/", "wiki": "http://www.yiiframework.com/wiki/",
"irc": "irc://irc.freenode.net/yii", "irc": "irc://irc.freenode.net/yii",
"source": "https://github.com/yiisoft/yii2" "source": "https://github.com/yiisoft/yii2"
}, },
"authors": [ "authors": [
{ {
"name": "Antonio Ramirez", "name": "Antonio Ramirez",
"email": "amigo.cobos@gmail.com" "email": "amigo.cobos@gmail.com"
} }
], ],
"require": { "require": {
"yiisoft/yii2": "*", "yiisoft/yii2": "*",
"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"
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment