From a61d88834e95b951c8e70a547845f906835e17d2 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Fri, 29 Mar 2013 12:45:33 +0100 Subject: [PATCH] added possibility to fill and insert universal input --- src/Intervention/Image/Image.php | 41 ++++++++----- tests/ImageTest.php | 101 +++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 14 deletions(-) diff --git a/src/Intervention/Image/Image.php b/src/Intervention/Image/Image.php index 028e5216..c780f180 100644 --- a/src/Intervention/Image/Image.php +++ b/src/Intervention/Image/Image.php @@ -717,15 +717,15 @@ class Image /** * Insert another image on top of the current image * - * @param mixed $file + * @param mixed $source * @param integer $pos_x * @param integer $pos_y * @param string $anchor * @return Image */ - public function insert($file, $pos_x = 0, $pos_y = 0, $anchor = null) + public function insert($source, $pos_x = 0, $pos_y = 0, $anchor = null) { - $obj = is_a($file, 'Intervention\Image\Image') ? $file : (new Image($file)); + $obj = is_a($source, 'Intervention\Image\Image') ? $source : (new Image($source)); // define anchor switch ($anchor) { @@ -901,34 +901,47 @@ class Image } /** - * Fill image with given color or image at position x,y + * Fill image with given color or image source at position x,y * - * @param mixed $color + * @param mixed $source * @param integer $pos_x * @param integer $pos_y * @return Image */ - public function fill($color, $pos_x = 0, $pos_y = 0) + public function fill($source, $pos_x = 0, $pos_y = 0) { - if (is_a($color, 'Intervention\Image\Image')) { + if (is_a($source, 'Intervention\Image\Image')) { // fill with image - imagesettile($this->resource, $color->resource); - $color = IMG_COLOR_TILED; + imagesettile($this->resource, $source->resource); + $source = IMG_COLOR_TILED; - } elseif ($this->isImageResource($color)) { + } elseif ($this->isImageResource($source)) { // fill with image resource - imagesettile($this->resource, $color); - $color = IMG_COLOR_TILED; + imagesettile($this->resource, $source); + $source = IMG_COLOR_TILED; + + } elseif (is_string($source) && file_exists($source)) { + + $img = new self($source); + imagesettile($this->resource, $img->resource); + $source = IMG_COLOR_TILED; + + } elseif (is_string($source) && $this->isBinary($source)) { + + // fill with image from binary string + $img = new self($source); + imagesettile($this->resource, $img->resource); + $source = IMG_COLOR_TILED; } else { // fill with color - $color = $this->parseColor($color); + $source = $this->parseColor($source); } - imagefill($this->resource, $pos_x, $pos_y, $color); + imagefill($this->resource, $pos_x, $pos_y, $source); return $this; } diff --git a/tests/ImageTest.php b/tests/ImageTest.php index 889a03fd..f638eee4 100644 --- a/tests/ImageTest.php +++ b/tests/ImageTest.php @@ -742,6 +742,57 @@ class ImageTest extends PHPUnit_Framework_Testcase $img->reset(); } + public function testInsertImageFromResource() + { + $resource = imagecreatefrompng('public/tile.png'); + $img = Image::canvas(16, 16)->insert($resource); + $this->assertInstanceOf('Intervention\Image\Image', $img); + $this->assertInternalType('int', $img->width); + $this->assertInternalType('int', $img->height); + $this->assertEquals($img->width, 16); + $this->assertEquals($img->height, 16); + $this->assertEquals('#b4e000', $img->pickColor(0, 0, 'hex')); + $this->assertEquals('#445160', $img->pickColor(15, 15, 'hex')); + } + + public function testInsertImageFromBinary() + { + $data = file_get_contents('public/tile.png'); + $img = Image::canvas(16, 16)->insert($data); + $this->assertInstanceOf('Intervention\Image\Image', $img); + $this->assertInternalType('int', $img->width); + $this->assertInternalType('int', $img->height); + $this->assertEquals($img->width, 16); + $this->assertEquals($img->height, 16); + $this->assertEquals('#b4e000', $img->pickColor(0, 0, 'hex')); + $this->assertEquals('#445160', $img->pickColor(15, 15, 'hex')); + } + + public function testInsertImageFromObject() + { + $obj = new Image('public/tile.png'); + $img = Image::canvas(16, 16)->insert($obj); + $this->assertInstanceOf('Intervention\Image\Image', $img); + $this->assertInternalType('int', $img->width); + $this->assertInternalType('int', $img->height); + $this->assertEquals($img->width, 16); + $this->assertEquals($img->height, 16); + $this->assertEquals('#b4e000', $img->pickColor(0, 0, 'hex')); + $this->assertEquals('#445160', $img->pickColor(15, 15, 'hex')); + } + + public function testInsertImageFromPath() + { + $img = Image::canvas(16, 16)->insert('public/tile.png'); + $this->assertInstanceOf('Intervention\Image\Image', $img); + $this->assertInternalType('int', $img->width); + $this->assertInternalType('int', $img->height); + $this->assertEquals($img->width, 16); + $this->assertEquals($img->height, 16); + $this->assertEquals('#b4e000', $img->pickColor(0, 0, 'hex')); + $this->assertEquals('#445160', $img->pickColor(15, 15, 'hex')); + } + public function testOpacity() { // simple image mask @@ -874,7 +925,57 @@ class ImageTest extends PHPUnit_Framework_Testcase $this->assertInstanceOf('Intervention\Image\Image', $img); $this->assertEquals('#b4e000', $img->pickColor(0, 0, 'hex')); $this->assertEquals('#445160', $img->pickColor(31, 31, 'hex')); + } + public function testFillImageWithResource() + { + $resource = imagecreatefrompng('public/tile.png'); + $img = Image::canvas(32, 32)->fill($resource); + $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); + $this->assertEquals('#b4e000', $img->pickColor(0, 0, 'hex')); + $this->assertEquals('#445160', $img->pickColor(31, 31, 'hex')); + } + + public function testFillImageWithBinary() + { + $data = file_get_contents('public/tile.png'); + $img = Image::canvas(32, 32)->fill($data); + $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); + $this->assertEquals('#b4e000', $img->pickColor(0, 0, 'hex')); + $this->assertEquals('#445160', $img->pickColor(31, 31, 'hex')); + } + + public function testFillImageWithObject() + { + $obj = new Image('public/tile.png'); + $img = Image::canvas(32, 32)->fill($obj); + $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); + $this->assertEquals('#b4e000', $img->pickColor(0, 0, 'hex')); + $this->assertEquals('#445160', $img->pickColor(31, 31, 'hex')); + } + + public function testFillImageWithPath() + { + $img = Image::canvas(32, 32)->fill('public/tile.png'); + $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); + $this->assertEquals('#b4e000', $img->pickColor(0, 0, 'hex')); + $this->assertEquals('#445160', $img->pickColor(31, 31, 'hex')); } public function testPixelImage()