1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-22 21:42:53 +02:00

normalized input values of brightness and contrast filters

This commit is contained in:
Oliver Vogel
2013-02-23 17:18:07 +01:00
parent d44d331283
commit 3ab0b923a2
3 changed files with 54 additions and 6 deletions

View File

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

View File

@@ -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;
}

View File

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