diff --git a/README.md b/README.md index 0125bf2d..7b8a983c 100644 --- a/README.md +++ b/README.md @@ -47,8 +47,8 @@ Add the facade of this package to the `$aliases` array. * Image::resize - Resize current image based on given width and/or height * 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 (from -255 to +255) -* Image::contrast - Changes contrast of current image (from -100 to +100) +* Image::brightness - Changes brightness of current image (-100 = min brightness, 0 = no change, +100 = max brightness) +* Image::contrast - Changes contrast of current image (-100 = min contrast, 0 = no change, +100 = max contrast) * Image::pixelate - Pixelate current image * Image::greyscale - Turn current image into a greyscale version * Image::text - Write text in current image diff --git a/src/Intervention/Image/Image.php b/src/Intervention/Image/Image.php index 94e34bca..1fbcfe9b 100644 --- a/src/Intervention/Image/Image.php +++ b/src/Intervention/Image/Image.php @@ -429,7 +429,15 @@ class Image */ public function brightness($level) { + // normalize level + if ($level >= -100 && $level <= 100) { + $level = $level * 2.55; + } else { + throw new Exception('Brightness level must be between -100 and +100'); + } + imagefilter($this->resource, IMG_FILTER_BRIGHTNESS, $level); + return $this; } @@ -441,7 +449,15 @@ class Image */ public function contrast($level) { + // normalize level + if ($level >= -100 && $level <= 100) { + $level = $level * (-1); + } else { + throw new Exception('Contrast level must be between -100 and +100'); + } + imagefilter($this->resource, IMG_FILTER_CONTRAST, $level); + return $this; } @@ -455,6 +471,7 @@ class Image public function pixelate($size = 10, $advanced = true) { imagefilter($this->resource, IMG_FILTER_PIXELATE, $size, $advanced); + return $this; } @@ -466,6 +483,7 @@ class Image public function grayscale() { imagefilter($this->resource, IMG_FILTER_GRAYSCALE); + return $this; } @@ -477,6 +495,7 @@ class Image public function greyscale() { $this->grayscale(); + return $this; } @@ -488,6 +507,7 @@ class Image public function reset() { $this->setProperties($this->dirname .'/'. $this->basename); + return $this; } diff --git a/tests/ImageTest.php b/tests/ImageTest.php index 7c642690..7475df06 100644 --- a/tests/ImageTest.php +++ b/tests/ImageTest.php @@ -349,19 +349,47 @@ class ImageTest extends PHPUnit_Framework_Testcase public function testBrightnessImage() { $img = $this->getTestImage(); - $img->brightness(50); - $img->brightness(-50); + $img->brightness(100); + $img->brightness(-100); $this->assertInstanceOf('Intervention\Image\Image', $img); } public function testContrastImage() { $img = $this->getTestImage(); - $img->contrast(50); - $img->contrast(-50); + $img->contrast(100); + $img->contrast(-100); $this->assertInstanceOf('Intervention\Image\Image', $img); } + public function testBrightnessException() + { + $img = $this->getTestImage(); + + // test exception + try { + $img->brightness(255); + } catch (Exception $e) { + return; + } + + $this->fail('An expected exception has not been raised for brightness filter'); + } + + public function testContrastException() + { + $img = $this->getTestImage(); + + // test exception + try { + $img->contrast(255); + } catch (Exception $e) { + return; + } + + $this->fail('An expected exception has not been raised for contrast filter'); + } + public function testStaticCallMake() { $img = Image::make('public/test.jpg');