mirror of
https://github.com/Intervention/image.git
synced 2025-08-01 03:20:17 +02:00
fixed transparency/alpha
This commit is contained in:
@@ -95,6 +95,9 @@ $img->save('public/bar.jpg', 60);
|
|||||||
$img1 = Image::make('public/img1.png');
|
$img1 = Image::make('public/img1.png');
|
||||||
$img2 = Image::make('public/img2.png');
|
$img2 = Image::make('public/img2.png');
|
||||||
$img1->resize(300, 200)->insert($img2)->save('public/bar.jpg');
|
$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
|
```php
|
||||||
|
@@ -66,10 +66,10 @@ class Image
|
|||||||
*
|
*
|
||||||
* @param string $path
|
* @param string $path
|
||||||
*/
|
*/
|
||||||
public function __construct($path = null)
|
public function __construct($path = null, $width = null, $height = null)
|
||||||
{
|
{
|
||||||
$this->filesystem = new Filesystem;
|
$this->filesystem = new Filesystem;
|
||||||
$this->setProperties($path);
|
$this->setProperties($path, $width, $height);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -88,7 +88,7 @@ class Image
|
|||||||
*
|
*
|
||||||
* @param string $path
|
* @param string $path
|
||||||
*/
|
*/
|
||||||
private function setProperties($path)
|
private function setProperties($path, $width = null, $height = null)
|
||||||
{
|
{
|
||||||
if ( ! is_null($path) && $this->filesystem->exists($path)) {
|
if ( ! is_null($path) && $this->filesystem->exists($path)) {
|
||||||
|
|
||||||
@@ -124,13 +124,16 @@ class Image
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
$this->width = 1;
|
$this->width = is_numeric($width) ? intval($width) : 1;
|
||||||
$this->height = 1;
|
$this->height = is_numeric($height) ? intval($height) : 1;
|
||||||
$this->resource = @imagecreatetruecolor($this->width, $this->height);
|
|
||||||
}
|
|
||||||
|
|
||||||
@imagealphablending($this->resource, false);
|
// create empty image
|
||||||
@imagesavealpha($this->resource, true);
|
$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;
|
break;
|
||||||
|
|
||||||
case 'png':
|
case 'png':
|
||||||
|
@imagealphablending($this->resource, false);
|
||||||
|
@imagesavealpha($this->resource, true);
|
||||||
@imagepng($this->resource);
|
@imagepng($this->resource);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@@ -173,6 +173,27 @@ class ImageTest extends PHPUnit_Framework_Testcase
|
|||||||
$this->assertInstanceOf('Intervention\Image\Image', $img);
|
$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()
|
public function testResetImage()
|
||||||
{
|
{
|
||||||
$img = $this->getTestImage();
|
$img = $this->getTestImage();
|
||||||
|
Reference in New Issue
Block a user