From 27bbfb0c1b05f7abb997c1df0df5c041fb2de9b0 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Mon, 4 Feb 2013 18:07:42 +0100 Subject: [PATCH] added static method canvas --- README.md | 13 ++++++----- src/Intervention/Image/Image.php | 14 +++++++++++- tests/ImageTest.php | 38 ++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 44f2fb5b..357c8d68 100644 --- a/README.md +++ b/README.md @@ -42,8 +42,9 @@ Add the facade of this package to the `$aliases` array. ## Usage * Image::__construct - Create new instance of Image class -* Image::make - Create new image resource from image file -* Image::resize - Resize image based on given width and/or height +* Image::make - Open a new image resource from image file or create a new empty image +* Image::canvas - Create a new empty image resource +* Image::resize - Resize current image based on given width and/or height * Image::grab - Cut out a detail of the image in given ratio and resize to output size * Image::insert - Insert another image on top of the current image * Image::brightness - Changes brightness of current image @@ -64,6 +65,9 @@ Add the facade of this package to the `$aliases` array. ### Code example (Laravel) ```php +// create an empty Image resource (background color transparent) +$img = Image::canvas(640, 480); + // create Image from file $img = Image::make('public/foo.jpg'); @@ -103,12 +107,9 @@ $img->save('public/bar.jpg'); $img->save('public/bar.jpg', 60); // its also possible to chain methods -$img1 = Image::make('public/img1.png'); +$img1 = Image::canvas(800, 600); $img2 = Image::make('public/img2.png'); $img1->resize(300, 200)->insert($img2)->save('public/bar.jpg'); - -// create an empty (transparent background) image, without opening any file -$img = new Image(null, 300, 200); ``` ```php diff --git a/src/Intervention/Image/Image.php b/src/Intervention/Image/Image.php index 6a9bde05..83f1c396 100644 --- a/src/Intervention/Image/Image.php +++ b/src/Intervention/Image/Image.php @@ -73,7 +73,7 @@ class Image } /** - * Create a new image resource from image file + * Open a new image resource from image file or create a new empty image * * @param string $path * @return Image @@ -83,6 +83,18 @@ class Image return new Image($path, $width, $height); } + /** + * Create a new empty image resource + * + * @param int $width + * @param int $height + * @return Image + */ + public static function canvas($width, $height) + { + return new Image(null, $width, $height); + } + /** * Set local properties for image resource * diff --git a/tests/ImageTest.php b/tests/ImageTest.php index b32c9e49..594f051e 100644 --- a/tests/ImageTest.php +++ b/tests/ImageTest.php @@ -361,4 +361,42 @@ class ImageTest extends PHPUnit_Framework_Testcase $img->contrast(-50); $this->assertInstanceOf('Intervention\Image\Image', $img); } + + public function testStaticCallMake() + { + $img = Image::make('public/test.jpg'); + $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); + $this->assertEquals($img->dirname, 'public'); + $this->assertEquals($img->basename, 'test.jpg'); + $this->assertEquals($img->extension, 'jpg'); + $this->assertEquals($img->filename, 'test'); + } + + public function testStaticCallCanvas() + { + $img = Image::canvas(300, 200); + $this->assertInternalType('resource', $img->resource); + $this->assertInternalType('int', $img->width); + $this->assertInternalType('int', $img->height); + $this->assertEquals($img->width, 300); + $this->assertEquals($img->height, 200); + } + + public function testCreateCanvasWithTransparentBackground() + { + $img = Image::canvas(100, 100); + $color = $img->pickColor(50, 50, 'array'); + $this->assertInternalType('int', $color['red']); + $this->assertInternalType('int', $color['green']); + $this->assertInternalType('int', $color['blue']); + $this->assertInternalType('int', $color['alpha']); + $this->assertEquals($color['red'], 0); + $this->assertEquals($color['green'], 0); + $this->assertEquals($color['blue'], 0); + $this->assertEquals($color['alpha'], 127); + } } \ No newline at end of file