From 0598667afb7461cbd68a0970f363ccd9186c7ab0 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Thu, 22 May 2014 20:03:33 +0200 Subject: [PATCH] fixed issue when cloning Image objects --- src/Intervention/Image/AbstractDriver.php | 10 ++++++++++ src/Intervention/Image/Gd/Driver.php | 18 ++++++++++++++++++ src/Intervention/Image/Image.php | 8 ++++++++ tests/GdSystemTest.php | 14 ++++++++++++++ tests/ImagickSystemTest.php | 14 ++++++++++++++ 5 files changed, 64 insertions(+) diff --git a/src/Intervention/Image/AbstractDriver.php b/src/Intervention/Image/AbstractDriver.php index dce552e2..2e8a0344 100644 --- a/src/Intervention/Image/AbstractDriver.php +++ b/src/Intervention/Image/AbstractDriver.php @@ -43,6 +43,16 @@ abstract class AbstractDriver */ abstract protected function coreAvailable(); + /** + * Returns clone of given core + * + * @return mixed + */ + public function cloneCore($core) + { + return clone $core; + } + /** * Initiates new image from given input * diff --git a/src/Intervention/Image/Gd/Driver.php b/src/Intervention/Image/Gd/Driver.php index d2c33581..76ca24bf 100644 --- a/src/Intervention/Image/Gd/Driver.php +++ b/src/Intervention/Image/Gd/Driver.php @@ -66,4 +66,22 @@ class Driver extends \Intervention\Image\AbstractDriver { return (extension_loaded('gd') && function_exists('gd_info')); } + + /** + * Returns clone of given core + * + * @return mixed + */ + public function cloneCore($core) + { + $width = imagesx($core); + $height = imagesy($core); + $clone = imagecreatetruecolor($width, $height); + imagealphablending($clone, false); + imagesavealpha($clone, true); + + imagecopy($clone, $core, 0, 0, 0, 0, $width, $height); + + return $clone; + } } diff --git a/src/Intervention/Image/Image.php b/src/Intervention/Image/Image.php index 87162865..9fa406fa 100644 --- a/src/Intervention/Image/Image.php +++ b/src/Intervention/Image/Image.php @@ -262,4 +262,12 @@ class Image extends File { return $this->encoded; } + + /** + * Cloning an image + */ + public function __clone() + { + $this->core = $this->driver->cloneCore($this->core); + } } diff --git a/tests/GdSystemTest.php b/tests/GdSystemTest.php index 2560d4ef..2499a8ee 100644 --- a/tests/GdSystemTest.php +++ b/tests/GdSystemTest.php @@ -1482,6 +1482,20 @@ class GdSystemTest extends PHPUnit_Framework_TestCase $this->assertColorAtPosition('#939393', $img, 35, 35); } + public function testCloneImageObject() + { + $img = $this->manager()->make('tests/images/trim.png'); + $cln = clone $img; + + // destroy original + $img->destroy(); + unset($img); + + // clone should be still intact + $this->assertInstanceOf('Intervention\Image\Image', $cln); + $this->assertInternalType('resource', $cln->getCore()); + } + private function assertColorAtPosition($color, $img, $x, $y) { $pick = $img->pickColor($x, $y, 'hex'); diff --git a/tests/ImagickSystemTest.php b/tests/ImagickSystemTest.php index d782e15d..8accab48 100644 --- a/tests/ImagickSystemTest.php +++ b/tests/ImagickSystemTest.php @@ -1463,6 +1463,20 @@ class ImagickSystemTest extends PHPUnit_Framework_TestCase $this->assertInstanceOf('Intervention\Image\Image', $img); } + public function testCloneImageObject() + { + $img = $this->manager()->make('tests/images/trim.png'); + $cln = clone $img; + + // destroy original + $img->destroy(); + unset($img); + + // clone should be still intact + $this->assertInstanceOf('Intervention\Image\Image', $cln); + $this->assertInstanceOf('Imagick', $cln->getCore()); + } + private function assertColorAtPosition($color, $img, $x, $y) { $pick = $img->pickColor($x, $y, 'hex');