mirror of
https://github.com/Intervention/image.git
synced 2025-01-17 20:28:21 +01:00
fixed transparency/alpha
This commit is contained in:
parent
b0ad50b28e
commit
47466941b2
@ -95,6 +95,9 @@ $img->save('public/bar.jpg', 60);
|
||||
$img1 = Image::make('public/img1.png');
|
||||
$img2 = Image::make('public/img2.png');
|
||||
$img1->resize(300, 200)->insert($img2)->save('public/bar.jpg');
|
||||
|
||||
// create an empty (transparent background) image, without opening any file
|
||||
$img = new Image(null, 300, 200);
|
||||
```
|
||||
|
||||
```php
|
||||
|
@ -66,10 +66,10 @@ class Image
|
||||
*
|
||||
* @param string $path
|
||||
*/
|
||||
public function __construct($path = null)
|
||||
public function __construct($path = null, $width = null, $height = null)
|
||||
{
|
||||
$this->filesystem = new Filesystem;
|
||||
$this->setProperties($path);
|
||||
$this->setProperties($path, $width, $height);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -88,7 +88,7 @@ class Image
|
||||
*
|
||||
* @param string $path
|
||||
*/
|
||||
private function setProperties($path)
|
||||
private function setProperties($path, $width = null, $height = null)
|
||||
{
|
||||
if ( ! is_null($path) && $this->filesystem->exists($path)) {
|
||||
|
||||
@ -124,13 +124,16 @@ class Image
|
||||
|
||||
} else {
|
||||
|
||||
$this->width = 1;
|
||||
$this->height = 1;
|
||||
$this->resource = @imagecreatetruecolor($this->width, $this->height);
|
||||
}
|
||||
$this->width = is_numeric($width) ? intval($width) : 1;
|
||||
$this->height = is_numeric($height) ? intval($height) : 1;
|
||||
|
||||
@imagealphablending($this->resource, false);
|
||||
@imagesavealpha($this->resource, true);
|
||||
// create empty image
|
||||
$this->resource = @imagecreatetruecolor($this->width, $this->height);
|
||||
|
||||
// fill with transparent background instead of black
|
||||
$transparent = imagecolorallocatealpha($this->resource, 0, 0, 0, 127);
|
||||
imagefill($this->resource, 0, 0, $transparent);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -465,6 +468,8 @@ class Image
|
||||
break;
|
||||
|
||||
case 'png':
|
||||
@imagealphablending($this->resource, false);
|
||||
@imagesavealpha($this->resource, true);
|
||||
@imagepng($this->resource);
|
||||
break;
|
||||
|
||||
|
@ -173,6 +173,27 @@ class ImageTest extends PHPUnit_Framework_Testcase
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
}
|
||||
|
||||
public function testAddImagesWithAlphaChannel()
|
||||
{
|
||||
$img = $this->getTestImage();
|
||||
$circle = new Image('public/circle.png');
|
||||
|
||||
for ($x=0; $x < $img->width; $x=$x+$circle->width) {
|
||||
for ($y=0; $y < $img->height; $y=$y+$circle->height) {
|
||||
// insert circle png at position x,y
|
||||
$img->insert($circle, $x, $y);
|
||||
}
|
||||
}
|
||||
|
||||
$save_as = 'public/final.png';
|
||||
$img->save($save_as);
|
||||
$this->assertFileExists($save_as);
|
||||
@unlink($save_as);
|
||||
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
||||
$this->assertInstanceOf('Intervention\Image\Image', $circle);
|
||||
}
|
||||
|
||||
public function testResetImage()
|
||||
{
|
||||
$img = $this->getTestImage();
|
||||
|
Loading…
x
Reference in New Issue
Block a user