1
0
mirror of https://github.com/Intervention/image.git synced 2025-01-17 20:28:21 +01:00

removed array param in grab method

This commit is contained in:
Oliver Vogel 2013-02-24 20:20:56 +01:00
parent 5f2456ee7c
commit 4555cc9b16
3 changed files with 25 additions and 37 deletions

View File

@ -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');
```

View File

@ -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
*

View File

@ -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()