1
0
mirror of https://github.com/Intervention/image.git synced 2025-09-02 10:23:29 +02:00

added optional parameter to create empty images with bgcolor

This commit is contained in:
Oliver Vogel
2013-03-20 16:36:38 +01:00
parent 7bf5960df4
commit ca3a41f5a3
2 changed files with 37 additions and 15 deletions

View File

@@ -88,9 +88,12 @@ class Image
/** /**
* Create a new instance of Image class * Create a new instance of Image class
* *
* @param string $path * @param string $path
* @param integer $width
* @param integer $height
* @param mixed $bgcolor
*/ */
public function __construct($path = null, $width = null, $height = null) public function __construct($path = null, $width = null, $height = null, $bgcolor = null)
{ {
// create filesystem // create filesystem
$this->filesystem = new Filesystem; $this->filesystem = new Filesystem;
@@ -102,7 +105,7 @@ class Image
} else { } else {
$this->setPropertiesEmpty($width, $height); $this->setPropertiesEmpty($width, $height, $bgcolor);
} }
} }
@@ -125,12 +128,13 @@ class Image
* *
* @param int $width * @param int $width
* @param int $height * @param int $height
* @param mixed $bgcolor
* @return Image * @return Image
*/ */
public static function canvas($width, $height) public static function canvas($width, $height, $bgcolor = null)
{ {
$image = new Image; $image = new Image;
$image->setPropertiesEmpty($width, $height); $image->setPropertiesEmpty($width, $height, $bgcolor);
return $image; return $image;
} }
@@ -154,6 +158,8 @@ class Image
* (requires additional package intervention/imagecache) * (requires additional package intervention/imagecache)
* *
* @param Closure $callback * @param Closure $callback
* @param integer $lifetime
* @param boolean $returnObj
* @return Image * @return Image
*/ */
public static function cache(Closure $callback = null, $lifetime = null, $returnObj = false) public static function cache(Closure $callback = null, $lifetime = null, $returnObj = false)
@@ -230,11 +236,12 @@ class Image
/** /**
* Set properties for empty image resource * Set properties for empty image resource
* *
* @param int $width * @param int $width
* @param int $height * @param int $height
* @param mixed $bgcolor
* @return void * @return void
*/ */
private function setPropertiesEmpty($width, $height) private function setPropertiesEmpty($width, $height, $bgcolor = null)
{ {
$this->width = is_numeric($width) ? intval($width) : 1; $this->width = is_numeric($width) ? intval($width) : 1;
$this->height = is_numeric($height) ? intval($height) : 1; $this->height = is_numeric($height) ? intval($height) : 1;
@@ -245,9 +252,15 @@ class Image
// create empty image // create empty image
$this->resource = @imagecreatetruecolor($this->width, $this->height); $this->resource = @imagecreatetruecolor($this->width, $this->height);
// fill with transparent background instead of black // set background color
$transparent = imagecolorallocatealpha($this->resource, 0, 0, 0, 127); if (is_null($bgcolor)) {
imagefill($this->resource, 0, 0, $transparent); // fill with transparent background instead of black
$bgcolor = imagecolorallocatealpha($this->resource, 0, 0, 0, 127);
} else {
$bgcolor = $this->parseColor($bgcolor);
}
imagefill($this->resource, 0, 0, $bgcolor);
} }
/** /**
@@ -573,8 +586,8 @@ class Image
/** /**
* Cut out a detail of the image in given ratio and resize to output size * Cut out a detail of the image in given ratio and resize to output size
* *
* @param int $width * @param integer $width
* @param int $height * @param integer $height
* *
* @return Image * @return Image
*/ */
@@ -705,7 +718,8 @@ class Image
/** /**
* Apply given image as alpha mask on current image * Apply given image as alpha mask on current image
* *
* @param mixed $file * @param mixed $file
* @param boolean $mask_with_alpha
* @return Image * @return Image
*/ */
public function mask($file, $mask_with_alpha = false) public function mask($file, $mask_with_alpha = false)
@@ -1028,7 +1042,7 @@ class Image
/** /**
* Returns image type stream * Returns image type stream
* *
* @param string $type gif|png|jpg|jpeg * @param string $type gif|png|jpg|jpeg
* @param integer quality * @param integer quality
* @return string * @return string
*/ */

View File

@@ -871,6 +871,14 @@ class ImageTest extends PHPUnit_Framework_Testcase
$this->assertInternalType('int', $img->height); $this->assertInternalType('int', $img->height);
$this->assertEquals($img->width, 300); $this->assertEquals($img->width, 300);
$this->assertEquals($img->height, 200); $this->assertEquals($img->height, 200);
$img = Image::canvas(32, 32, 'b53717');
$this->assertInternalType('resource', $img->resource);
$this->assertInternalType('int', $img->width);
$this->assertInternalType('int', $img->height);
$this->assertEquals($img->width, 32);
$this->assertEquals($img->height, 32);
$this->assertEquals($img->pickColor(15, 15, 'hex'), '#b53717');
} }
public function testStaticCallRaw() public function testStaticCallRaw()