1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-11 00:14:03 +02:00

added possibility to create image from gd resource

This commit is contained in:
Oliver Vogel
2013-03-28 17:15:17 +01:00
parent d4ebd42853
commit 92cb637475
2 changed files with 36 additions and 7 deletions

View File

@@ -87,17 +87,21 @@ class Image
/** /**
* Create a new instance of Image class * Create a new instance of Image class
* *
* @param string $path * @param string $source
* @param integer $width * @param integer $width
* @param integer $height * @param integer $height
* @param mixed $bgcolor * @param mixed $bgcolor
*/ */
public function __construct($path = null, $width = null, $height = null, $bgcolor = null) public function __construct($source = null, $width = null, $height = null, $bgcolor = null)
{ {
// set image properties // set image properties
if ( ! is_null($path)) { if ( ! is_null($source)) {
$this->setPropertiesFromPath($path); if (is_resource($source)) {
$this->setPropertiesFromResource($source);
} else {
$this->setPropertiesFromPath($source);
}
} else { } else {
@@ -108,13 +112,18 @@ class Image
/** /**
* Open a new image resource from image file * Open a new image resource from image file
* *
* @param string $path * @param mixed $source
* @return Image * @return Image
*/ */
public static function make($path) public static function make($source)
{ {
$image = new Image; $image = new Image;
$image->setPropertiesFromPath($path);
if (is_resource($source)) {
$image->setPropertiesFromResource($source);
} else {
$image->setPropertiesFromPath($source);
}
return $image; return $image;
} }

View File

@@ -41,6 +41,15 @@ class ImageTest extends PHPUnit_Framework_Testcase
$this->assertInternalType('int', $img->height); $this->assertInternalType('int', $img->height);
$this->assertEquals($img->width, 800); $this->assertEquals($img->width, 800);
$this->assertEquals($img->height, 600); $this->assertEquals($img->height, 600);
$resource = imagecreatefromjpeg('public/test.jpg');
$img = new Image($resource);
$this->assertInstanceOf('Intervention\Image\Image', $img);
$this->assertInternalType('resource', $img->resource);
$this->assertInternalType('int', $img->width);
$this->assertInternalType('int', $img->height);
$this->assertEquals($img->width, 800);
$this->assertEquals($img->height, 600);
} }
public function testOpenImage() public function testOpenImage()
@@ -1040,6 +1049,17 @@ class ImageTest extends PHPUnit_Framework_Testcase
$this->assertEquals($img->mime, 'image/jpeg'); $this->assertEquals($img->mime, 'image/jpeg');
} }
public function testStaticCallMakeFromResource()
{
$resource = imagecreatefromjpeg('public/test.jpg');
$img = Image::make($resource);
$this->assertInternalType('resource', $img->resource);
$this->assertInternalType('int', $img->width);
$this->assertInternalType('int', $img->height);
$this->assertEquals($img->width, 800);
$this->assertEquals($img->height, 600);
}
public function testStaticCallCanvas() public function testStaticCallCanvas()
{ {
$img = Image::canvas(300, 200); $img = Image::canvas(300, 200);