From 68352b3ffc76bee1979d411eba6f0136fe9b5630 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Fri, 28 Feb 2014 11:26:09 +0100 Subject: [PATCH] improved fill method --- src/Intervention/Image/Image.php | 12 +++++++++--- tests/ImageTest.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/Intervention/Image/Image.php b/src/Intervention/Image/Image.php index 1b323cd2..4f46da67 100644 --- a/src/Intervention/Image/Image.php +++ b/src/Intervention/Image/Image.php @@ -1054,7 +1054,7 @@ class Image * @param integer $pos_y * @return Image */ - public function fill($source, $pos_x = 0, $pos_y = 0) + public function fill($source, $pos_x = null, $pos_y = null) { if (is_a($source, 'Intervention\Image\Image')) { @@ -1086,8 +1086,14 @@ class Image // fill with color $source = $this->parseColor($source); } - - imagefill($this->resource, $pos_x, $pos_y, $source); + + if (is_int($pos_x) && is_int($pos_y)) { + // floodfill if exact position is defined + imagefill($this->resource, $pos_x, $pos_y, $source); + } else { + // fill whole image otherwise + imagefilledrectangle($this->resource, 0, 0, $this->width - 1, $this->height - 1, $source); + } return $this; } diff --git a/tests/ImageTest.php b/tests/ImageTest.php index 7f260353..e246b544 100644 --- a/tests/ImageTest.php +++ b/tests/ImageTest.php @@ -1242,6 +1242,20 @@ class ImageTest extends PHPUnit_Framework_Testcase } public function testFillImageWithPath() + { + $img = Image::canvas(100, 100, '#ffa600')->fill('public/circle.png'); + $this->assertInstanceOf('Intervention\Image\Image', $img); + $this->assertInternalType('int', $img->width); + $this->assertInternalType('int', $img->height); + $this->assertEquals($img->width, 100); + $this->assertEquals($img->height, 100); + $this->assertEquals('#ffa600', $img->pickColor(0, 0, 'hex')); + $this->assertEquals('#322000', $img->pickColor(12, 12, 'hex')); + $this->assertEquals('#ffa600', $img->pickColor(99, 99, 'hex')); + $this->assertEquals('#322000', $img->pickColor(80, 80, 'hex')); + } + + public function testFillImageWithTransparentImage() { $img = Image::canvas(32, 32)->fill('public/tile.png'); $this->assertInstanceOf('Intervention\Image\Image', $img); @@ -1253,6 +1267,22 @@ class ImageTest extends PHPUnit_Framework_Testcase $this->assertEquals('#445160', $img->pickColor(31, 31, 'hex')); } + public function testFillWithPosition() + { + $img = Image::make('public/tile.png')->fill('#ff00ff', 0, 0); + $this->assertInstanceOf('Intervention\Image\Image', $img); + $this->assertEquals('#ff00ff', $img->pickColor(0, 0, 'hex')); + $this->assertEquals('#445160', $img->pickColor(15, 15, 'hex')); + } + + public function testFillWithoutPosition() + { + $img = Image::make('public/tile.png')->fill('#ff00ff'); + $this->assertInstanceOf('Intervention\Image\Image', $img); + $this->assertEquals('#ff00ff', $img->pickColor(0, 0, 'hex')); + $this->assertEquals('#ff00ff', $img->pickColor(15, 15, 'hex')); + } + public function testPixelImage() { $img = $this->getTestImage();