diff --git a/src/Intervention/Image/Image.php b/src/Intervention/Image/Image.php index f38c1bbd..9b7cd8bd 100644 --- a/src/Intervention/Image/Image.php +++ b/src/Intervention/Image/Image.php @@ -242,12 +242,10 @@ class Image */ private function initEmpty($width, $height, $bgcolor = null) { + // define width + height $this->width = is_numeric($width) ? intval($width) : 1; $this->height = is_numeric($height) ? intval($height) : 1; - $this->original['width'] = $this->width; - $this->original['height'] = $this->height; - // create empty image $this->resource = imagecreatetruecolor($this->width, $this->height); @@ -259,8 +257,11 @@ class Image $bgcolor = $this->parseColor($bgcolor); } + // fill with background color imagefill($this->resource, 0, 0, $bgcolor); - $this->original['bgcolor'] = $bgcolor; + + // save current state as original + $this->backup(); } /** @@ -1385,6 +1386,16 @@ class Image return $this; } + /** + * Set current image as original (reset will return to this) + * + * @return void + */ + public function backup() + { + $this->original = $this->cloneResource($this->resource); + } + /** * Reset to original image resource * @@ -1392,13 +1403,8 @@ class Image */ public function reset() { - if (is_null($this->dirname) && is_null($this->basename)) { - - $this->initEmpty($this->original['width'], $this->original['height'], $this->original['bgcolor']); - - } else { - - $this->initFromPath($this->dirname .'/'. $this->basename); + if ($this->isImageResource($this->original)) { + $this->initFromResource($this->original); } return $this; @@ -1744,6 +1750,19 @@ class Image return false; } + /** + * Clones and returns cloned image resource + * + * @param Resource $resource + * @return Resource + */ + private function cloneResource($resource) + { + ob_start(); + imagegd2($resource); + return imagecreatefromstring(ob_get_clean()); + } + /** * Set file info from image path in filesystem * @@ -1799,6 +1818,9 @@ class Image ); break; } + + // save current state as original + $this->backup(); } /** @@ -1811,8 +1833,9 @@ class Image $this->resource = $resource; $this->width = imagesx($this->resource); $this->height = imagesy($this->resource); - $this->original['width'] = $this->width; - $this->original['height'] = $this->height; + + // save current state as original + $this->backup(); } /** @@ -1836,8 +1859,9 @@ class Image $this->mime = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $string); $this->width = imagesx($this->resource); $this->height = imagesy($this->resource); - $this->original['width'] = $this->width; - $this->original['height'] = $this->height; + + // save current state as original + $this->backup(); } /** diff --git a/tests/ImageTest.php b/tests/ImageTest.php index 7e60d538..aaab3d72 100644 --- a/tests/ImageTest.php +++ b/tests/ImageTest.php @@ -1338,13 +1338,28 @@ class ImageTest extends PHPUnit_Framework_Testcase public function testResetEmptyImage() { - $img = new Image(null, 800, 600); + $img = new Image(null, 800, 600, '#0000ff'); $img->resize(300, 200); + $img->fill('#00ff00'); $img->reset(); $this->assertInternalType('int', $img->width); $this->assertInternalType('int', $img->height); $this->assertEquals($img->width, 800); $this->assertEquals($img->height, 600); + $this->assertEquals('#0000ff', $img->pickColor(0, 0, 'hex')); + } + + public function testBackup() + { + $img = new Image(null, 800, 600, '#0000ff'); + $img->fill('#00ff00'); + $img->backup(); + $img->reset(); + $this->assertInternalType('int', $img->width); + $this->assertInternalType('int', $img->height); + $this->assertEquals($img->width, 800); + $this->assertEquals($img->height, 600); + $this->assertEquals('#00ff00', $img->pickColor(0, 0, 'hex')); } public function testLimitColors()