diff --git a/README.md b/README.md index 32cc91fe..a65333cd 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/Intervention/Image/Image.php b/src/Intervention/Image/Image.php index 504ce6f2..d0359551 100644 --- a/src/Intervention/Image/Image.php +++ b/src/Intervention/Image/Image.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; diff --git a/tests/ImageTest.php b/tests/ImageTest.php index 57ba29fe..71e21c3a 100644 --- a/tests/ImageTest.php +++ b/tests/ImageTest.php @@ -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();