From 96b84d15d805acfe37379e30649a96fbc665dbff Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sat, 9 Nov 2013 12:12:34 +0100 Subject: [PATCH] added colorize method --- .../ColorizeOutOfBoundsException.php | 8 +++++ src/Intervention/Image/Image.php | 31 +++++++++++++++++++ tests/ImageTest.php | 17 ++++++++++ 3 files changed, 56 insertions(+) create mode 100644 src/Intervention/Image/Exception/ColorizeOutOfBoundsException.php diff --git a/src/Intervention/Image/Exception/ColorizeOutOfBoundsException.php b/src/Intervention/Image/Exception/ColorizeOutOfBoundsException.php new file mode 100644 index 00000000..3f866fbe --- /dev/null +++ b/src/Intervention/Image/Exception/ColorizeOutOfBoundsException.php @@ -0,0 +1,8 @@ + 100) || + ($green < -100 || $green > 100) || + ($blue < -100 || $blue > 100) || + ($alpha < -100 || $alpha > 100)) { + throw new Exception\ColorizeOutOfBoundsException( + 'Colorize levels must be between -100 and +100' + ); + } + + // normalize colorize levels + $red = round($red * 2.55); + $green = round($green * 2.55); + $blue = round($blue * 2.55); + $alpha = round($alpha * 2.55); + + // apply filter + imagefilter($this->resource, IMG_FILTER_COLORIZE, $red, $green, $blue, $alpha); + + return $this; + } + /** * Apply blur filter on the current image * diff --git a/tests/ImageTest.php b/tests/ImageTest.php index 1dd58c2b..3f11d327 100644 --- a/tests/ImageTest.php +++ b/tests/ImageTest.php @@ -1577,6 +1577,23 @@ class ImageTest extends PHPUnit_Framework_Testcase $img->contrast(-101); } + public function testColorizeImage() + { + $img = $this->getTestImage(); + $img->colorize(-100, 0, 100); + $img->colorize(100, -100, -100, 50); + $this->assertInstanceOf('Intervention\Image\Image', $img); + } + + /** + * @expectedException Intervention\Image\Exception\ColorizeOutOfBoundsException + */ + public function testColorizeOutOfBounds() + { + $img = $this->getTestImage(); + $img->colorize(-101, 0, 0); + } + public function testEncode() { // default encoding