1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-19 04:01:30 +02:00

fixed issue when cloning Image objects

This commit is contained in:
Oliver Vogel
2014-05-22 20:03:33 +02:00
parent 10088537b7
commit 0598667afb
5 changed files with 64 additions and 0 deletions

View File

@@ -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
*

View File

@@ -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;
}
}

View File

@@ -262,4 +262,12 @@ class Image extends File
{
return $this->encoded;
}
/**
* Cloning an image
*/
public function __clone()
{
$this->core = $this->driver->cloneCore($this->core);
}
}

View File

@@ -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');

View File

@@ -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');