From c9791703614baeca6329f01383b5e1b00a41e0f2 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Mon, 18 Mar 2013 15:26:51 +0100 Subject: [PATCH] added opacity method --- README.md | 4 ++++ src/Intervention/Image/Image.php | 27 ++++++++++++++++++++++++++- tests/ImageTest.php | 22 ++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 24236d8a..48df42c3 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ Add the facade of this package to the `$aliases` array. * Image::crop - Crop the current image * Image::grab - Cut out a detail of the image in given ratio and resize to output size * Image::resizeCanvas - Resize image boundaries +* Image::opacity - Set opacity of current image * Image::mask - Apply given image as alpha mask on current image * 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) @@ -224,6 +225,9 @@ $img->mask('public/mask.png', true); // rotate image 90° clockwise $img->rotate(90); +// set 75% opacity +$img->opacity(75); + // flip image horizontally $img->flip('h'); diff --git a/src/Intervention/Image/Image.php b/src/Intervention/Image/Image.php index 8a8f796d..34235cce 100644 --- a/src/Intervention/Image/Image.php +++ b/src/Intervention/Image/Image.php @@ -678,6 +678,30 @@ class Image return $this; } + /** + * Set opacity of current image + * + * @param integer $value + * @return Image + */ + public function opacity($value) + { + if ($value >= 0 && $value <= 100) { + $value = intval($value) / 100; + } else { + throw new Exception('Opacity must be between 0 and 100'); + } + + // create alpha mask + $alpha = new self(null, $this->width, $this->height); + $alpha->fill(sprintf('rgba(0, 0, 0, %.1f)', $value)); + + // apply alpha mask + $this->mask($alpha, true); + + return $this; + } + /** * Apply given image as alpha mask on current image * @@ -1128,7 +1152,8 @@ class Image /** * Save image in filesystem * - * @param string $path + * @param string $path + * @param integer $quality * @return Image */ public function save($path = null, $quality = 90) diff --git a/tests/ImageTest.php b/tests/ImageTest.php index 4ca86651..fd2e0fb6 100644 --- a/tests/ImageTest.php +++ b/tests/ImageTest.php @@ -435,6 +435,28 @@ class ImageTest extends PHPUnit_Framework_Testcase $this->assertInstanceOf('Intervention\Image\Image', $img); } + public function testOpacity() + { + // simple image mask + $img = Image::make('public/test.jpg'); + $img->resize(32, 32)->opacity(50); + $this->assertInstanceOf('Intervention\Image\Image', $img); + $this->assertInternalType('int', $img->width); + $this->assertInternalType('int', $img->height); + $this->assertEquals($img->width, 32); + $this->assertEquals($img->height, 32); + $checkColor = $img->pickColor(15, 15, 'array'); + $this->assertEquals($checkColor['red'], 254); + $this->assertEquals($checkColor['green'], 204); + $this->assertEquals($checkColor['blue'], 112); + $this->assertEquals($checkColor['alpha'], 64); + $checkColor = $img->pickColor(31, 31, 'array'); + $this->assertEquals($checkColor['red'], 255); + $this->assertEquals($checkColor['green'], 166); + $this->assertEquals($checkColor['blue'], 0); + $this->assertEquals($checkColor['alpha'], 64); + } + public function testMaskImage() { // simple image mask