diff --git a/README.md b/README.md index cd7b7e0f..5136b996 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ Add the facade of this package to the `$aliases` array. * Image::pixelate - Pixelate current image * Image::greyscale - Turn current image into a greyscale version * Image::invert - Invert colors of current image +* Image::flip - Mirror image horizontally or vertically * Image::text - Write text in current image * Image::fill - Fill image with given color at position x,y * Image::rectangle - Draw rectangle in current image starting at point 1 and ending at point 2 @@ -186,6 +187,12 @@ $img->circle('ae051f', 400, 300, 100, false); // draw a red line from point 10,10 to point 300,300 pixel $img->line('ae051f', 10, 10, 300, 300); + +// flip image horizontally +$img->flip('h'); + +// flip image vertically +$img->flip('v'); ``` #### Method chaining diff --git a/src/Intervention/Image/Image.php b/src/Intervention/Image/Image.php index 234ad621..368140f0 100644 --- a/src/Intervention/Image/Image.php +++ b/src/Intervention/Image/Image.php @@ -406,6 +406,31 @@ class Image return $this->grab($width, $height); } + public function flip($mode = null) + { + $x = 0; + $y = 0; + $width = $this->width; + $height = $this->height; + + switch (strtolower($mode)) { + case 2: + case 'v': + case 'vert': + case 'vertical': + $y = $height - 1; + $height = $height * (-1); + break; + + default: + $x = $width - 1; + $width = $width * (-1); + break; + } + + return $this->modify(0, 0, $x, $y, $this->width, $this->height, $width, $height); + } + /** * Insert another image on top of the current image * diff --git a/tests/ImageTest.php b/tests/ImageTest.php index 6bd0506b..5d4ffa68 100644 --- a/tests/ImageTest.php +++ b/tests/ImageTest.php @@ -183,6 +183,19 @@ class ImageTest extends PHPUnit_Framework_Testcase $this->assertEquals($img->height, 200); } + public function testFlipImage() + { + $img = $this->getTestImage(); + $img->flip('h'); + $this->assertInstanceOf('Intervention\Image\Image', $img); + $this->assertEquals('#ffbf47', $img->pickColor(0, 0, 'hex')); + + $img = $this->getTestImage(); + $img->flip('v'); + $this->assertInstanceOf('Intervention\Image\Image', $img); + $this->assertEquals('#fed78c', $img->pickColor(0, 0, 'hex')); + } + public function testInsertImage() { $img = $this->getTestImage();