diff --git a/README.md b/README.md index 90dfa996..4367c03e 100644 --- a/README.md +++ b/README.md @@ -103,9 +103,6 @@ $img->grab(200); // crop the best fitting 5:3 (600x360) ratio and resize to 600x360 pixel $img->grab(600, 360); -// crop the best fitting 1:1 (150x150) ratio and resize to 150x150 pixel -$img->grab(array('width' => '150')); - // save image $img->save('public/bar.jpg'); ``` diff --git a/src/Intervention/Image/Image.php b/src/Intervention/Image/Image.php index a90e152e..f17c5931 100644 --- a/src/Intervention/Image/Image.php +++ b/src/Intervention/Image/Image.php @@ -301,38 +301,22 @@ class Image * parameter is calculated based on the given * @return Image */ - public function grab() + public function grab($width = null, $height = null) { - $args = func_get_args(); - - if (array_key_exists(0, $args) && is_array($args[0])) { - // extract 'width' and 'height' - extract(array_only($args[0], array('width', 'height'))); - $width = isset($width) ? intval($width) : null; - $height = isset($height) ? intval($height) : null; - - if ( ! is_null($width) OR ! is_null($height)) { - // if width or height are not set, define values automatically - $width = is_null($width) ? $height : $width; - $height = is_null($height) ? $width : $height; - } else { - // width or height not defined (resume with original values) - throw new Exception('Width or Height needs to be defined as keys in paramater array'); - } - - } elseif (array_key_exists(0, $args) && array_key_exists(1, $args) && is_numeric($args[0]) && is_numeric($args[1])) { - - $width = intval($args[0]); - $height = intval($args[1]); - - } elseif (array_key_exists(0, $args) && is_numeric($args[0])) { - - $width = intval($args[0]); - $height = intval($args[0]); - + if (is_array($width)) { + $dimensions = $width; + return $this->legacyGrab($dimensions); } - if (is_null($width) OR is_null($height)) { + $width = is_numeric($width) ? intval($width) : null; + $height = is_numeric($height) ? intval($height) : null; + + if ( ! is_null($width) OR ! is_null($height)) { + // if width or height are not set, define values automatically + $width = is_null($width) ? $height : $width; + $height = is_null($height) ? $width : $height; + } else { + // width or height not defined (resume with original values) throw new Exception('width or height needs to be defined'); } @@ -355,6 +339,13 @@ class Image return $this->modify(0, 0, $src_x, $src_y, $width, $height, $grab_width, $grab_height); } + public function legacyGrab($dimensions = array()) + { + $width = array_key_exists('width', $dimensions) ? intval($dimensions['width']) : null; + $height = array_key_exists('height', $dimensions) ? intval($dimensions['height']) : null; + return $this->grab($width, $height); + } + /** * Insert another image on top of the current image * diff --git a/tests/ImageTest.php b/tests/ImageTest.php index 347bf287..0f0a9b79 100644 --- a/tests/ImageTest.php +++ b/tests/ImageTest.php @@ -143,10 +143,10 @@ class ImageTest extends PHPUnit_Framework_Testcase $this->assertEquals($img->height, 100); $img = $this->getTestImage(); - $img->grab(array('width' => '320', 'height' => '100')); + $img->grab(null, 100); $this->assertInternalType('int', $img->width); $this->assertInternalType('int', $img->height); - $this->assertEquals($img->width, 320); + $this->assertEquals($img->width, 100); $this->assertEquals($img->height, 100); $img = $this->getTestImage(); @@ -157,11 +157,11 @@ class ImageTest extends PHPUnit_Framework_Testcase $this->assertEquals($img->height, 100); $img = $this->getTestImage(); - $img->grab(array('height' => '100')); + $img->grab(array('height' => '200')); $this->assertInternalType('int', $img->width); $this->assertInternalType('int', $img->height); - $this->assertEquals($img->width, 100); - $this->assertEquals($img->height, 100); + $this->assertEquals($img->width, 200); + $this->assertEquals($img->height, 200); } public function testInsertImage()