1
0
mirror of https://github.com/Intervention/image.git synced 2025-09-01 01:51:43 +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

@@ -89,8 +89,11 @@ class Image
* Create a new instance of Image class
*
* @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
$this->filesystem = new Filesystem;
@@ -102,7 +105,7 @@ class Image
} else {
$this->setPropertiesEmpty($width, $height);
$this->setPropertiesEmpty($width, $height, $bgcolor);
}
}
@@ -125,12 +128,13 @@ class Image
*
* @param int $width
* @param int $height
* @param mixed $bgcolor
* @return Image
*/
public static function canvas($width, $height)
public static function canvas($width, $height, $bgcolor = null)
{
$image = new Image;
$image->setPropertiesEmpty($width, $height);
$image->setPropertiesEmpty($width, $height, $bgcolor);
return $image;
}
@@ -154,6 +158,8 @@ class Image
* (requires additional package intervention/imagecache)
*
* @param Closure $callback
* @param integer $lifetime
* @param boolean $returnObj
* @return Image
*/
public static function cache(Closure $callback = null, $lifetime = null, $returnObj = false)
@@ -232,9 +238,10 @@ class Image
*
* @param int $width
* @param int $height
* @param mixed $bgcolor
* @return void
*/
private function setPropertiesEmpty($width, $height)
private function setPropertiesEmpty($width, $height, $bgcolor = null)
{
$this->width = is_numeric($width) ? intval($width) : 1;
$this->height = is_numeric($height) ? intval($height) : 1;
@@ -245,9 +252,15 @@ class Image
// create empty image
$this->resource = @imagecreatetruecolor($this->width, $this->height);
// set background color
if (is_null($bgcolor)) {
// fill with transparent background instead of black
$transparent = imagecolorallocatealpha($this->resource, 0, 0, 0, 127);
imagefill($this->resource, 0, 0, $transparent);
$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
*
* @param int $width
* @param int $height
* @param integer $width
* @param integer $height
*
* @return Image
*/
@@ -706,6 +719,7 @@ class Image
* Apply given image as alpha mask on current image
*
* @param mixed $file
* @param boolean $mask_with_alpha
* @return Image
*/
public function mask($file, $mask_with_alpha = false)

View File

@@ -871,6 +871,14 @@ class ImageTest extends PHPUnit_Framework_Testcase
$this->assertInternalType('int', $img->height);
$this->assertEquals($img->width, 300);
$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()