1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-30 17:19:50 +02:00

added opacity method

This commit is contained in:
Oliver Vogel
2013-03-18 15:26:51 +01:00
parent 4f89fa304e
commit c979170361
3 changed files with 52 additions and 1 deletions

View File

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

View File

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

View File

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