From fd29c19fde016905f6f5c15fa109aafdde2dfd02 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Thu, 28 Feb 2013 17:35:28 +0100 Subject: [PATCH] added crop method --- README.md | 17 +++++++++++++++++ src/Intervention/Image/Image.php | 30 ++++++++++++++++++++++++++++++ tests/ImageTest.php | 19 +++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/README.md b/README.md index 36df01e3..8223394c 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ Add the facade of this package to the `$aliases` array. * Image::make - Open a new image resource from image file or create a new empty image * Image::canvas - Create a new empty image resource * Image::resize - Resize current image based on given width and/or height +* Image::crop - Crop the current image * Image::grab - Cut out a detail of the image in given ratio and resize to output size * Image::insert - Insert another image on top of the current image * Image::brightness - Changes brightness of current image (-100 = min brightness, 0 = no change, +100 = max brightness) @@ -99,6 +100,22 @@ $img->reset(); $img->save('public/bar.jpg', 60); ``` +#### Crop image + +```php +// create Image from file +$img = Image::make('public/foo.jpg'); + +// crop 300x200 pixel cutout at position x:200, y:100 +$img->crop(300, 200, 200, 100); + +// crop 300x200 pixel cutout centered on current image +$img->crop(300, 200); + +// save image +$img->save(); +``` + #### Smart resizing ```php diff --git a/src/Intervention/Image/Image.php b/src/Intervention/Image/Image.php index 921ef551..9d7f467a 100644 --- a/src/Intervention/Image/Image.php +++ b/src/Intervention/Image/Image.php @@ -315,6 +315,36 @@ class Image return $this->resize($width, $height, true); } + /** + * Crop the current image + * + * @param integer $width + * @param integer $height + * @param integer $pos_x + * @param integer $pos_y + * + * @return Image + */ + public function crop($width, $height, $pos_x = null, $pos_y = null) + { + $width = is_numeric($width) ? intval($width) : null; + $height = is_numeric($height) ? intval($height) : null; + $pos_x = is_numeric($pos_x) ? intval($pos_x) : null; + $pos_y = is_numeric($pos_y) ? intval($pos_y) : null; + + if (is_null($pos_x) && is_null($pos_y)) { + // center position of width/height rectangle + $pos_x = floor(($this->width - intval($width)) / 2); + $pos_y = floor(($this->height - intval($height)) / 2); + } + + if (is_null($width) || is_null($height)) { + throw new Exception('width and height of cutout needs to be defined'); + } + + return $this->modify(0, 0, $pos_x , $pos_y, $width, $height, $width, $height); + } + /** * Cut out a detail of the image in given ratio and resize to output size * diff --git a/tests/ImageTest.php b/tests/ImageTest.php index 055e12f4..b6b25104 100644 --- a/tests/ImageTest.php +++ b/tests/ImageTest.php @@ -107,6 +107,25 @@ class ImageTest extends PHPUnit_Framework_Testcase $this->assertEquals($img->height, $original_height); } + public function testCropImage() + { + $img = $this->getTestImage(); + $img->crop(100, 100); + $this->assertInternalType('int', $img->width); + $this->assertInternalType('int', $img->height); + $this->assertEquals($img->width, 100); + $this->assertEquals($img->height, 100); + $this->assertEquals('#ffbe46', $img->pickColor(99, 99, 'hex')); + + $img = $this->getTestImage(); + $img->crop(100, 100, 650, 400); + $this->assertInternalType('int', $img->width); + $this->assertInternalType('int', $img->height); + $this->assertEquals($img->width, 100); + $this->assertEquals($img->height, 100); + $this->assertEquals('#ffa600', $img->pickColor(99, 99, 'hex')); + } + public function testLegacyResize() { // auto height