diff --git a/src/Intervention/Image/Gd/Commands/ResetCommand.php b/src/Intervention/Image/Gd/Commands/ResetCommand.php index d261b9ad..c8d2e4a1 100644 --- a/src/Intervention/Image/Gd/Commands/ResetCommand.php +++ b/src/Intervention/Image/Gd/Commands/ResetCommand.php @@ -16,8 +16,12 @@ class ResetCommand extends \Intervention\Image\Commands\AbstractCommand if (is_resource($backup = $image->getBackup($backupName))) { - // destroy old resource + // destroy current resource imagedestroy($image->getCore()); + + // clone backup + $backup = $image->getDriver()->cloneCore($backup); + // reset to new resource $image->setCore($backup); diff --git a/src/Intervention/Image/Imagick/Commands/ResetCommand.php b/src/Intervention/Image/Imagick/Commands/ResetCommand.php index 8e35b5b6..ee5a2cdf 100644 --- a/src/Intervention/Image/Imagick/Commands/ResetCommand.php +++ b/src/Intervention/Image/Imagick/Commands/ResetCommand.php @@ -18,9 +18,12 @@ class ResetCommand extends \Intervention\Image\Commands\AbstractCommand if ($backup instanceof \Imagick) { - // destroy old core + // destroy current core $image->getCore()->clear(); + // clone backup + $backup = clone $backup; + // reset to new resource $image->setCore($backup); diff --git a/tests/GdSystemTest.php b/tests/GdSystemTest.php index 0db3a101..320af085 100644 --- a/tests/GdSystemTest.php +++ b/tests/GdSystemTest.php @@ -1106,6 +1106,29 @@ class GdSystemTest extends PHPUnit_Framework_TestCase $this->assertTransparentPosition($img, 0, 0); } + public function testResetToNamed() + { + $img = $this->manager()->make('tests/images/tile.png'); + $img->backup('original'); + $img->resize(30, 20); + $img->backup('30x20'); + + // reset to original + $img->reset('original'); + $this->assertEquals(16, $img->getWidth()); + $this->assertEquals(16, $img->getHeight()); + + // reset to 30x20 + // $img->reset('30x20'); + // $this->assertEquals(30, $img->getWidth()); + // $this->assertEquals(20, $img->getHeight()); + + // reset to original again + $img->reset('original'); + $this->assertEquals(16, $img->getWidth()); + $this->assertEquals(16, $img->getHeight()); + } + public function testLimitColors() { $img = $this->manager()->make('tests/images/trim.png'); diff --git a/tests/ImagickSystemTest.php b/tests/ImagickSystemTest.php index 2c40732b..f01ed6c4 100644 --- a/tests/ImagickSystemTest.php +++ b/tests/ImagickSystemTest.php @@ -1081,6 +1081,29 @@ class ImagickSystemTest extends PHPUnit_Framework_TestCase $this->assertTransparentPosition($img, 0, 0); } + public function testResetToNamed() + { + $img = $this->manager()->make('tests/images/tile.png'); + $img->backup('original'); + $img->resize(30, 20); + $img->backup('30x20'); + + // reset to original + $img->reset('original'); + $this->assertEquals(16, $img->getWidth()); + $this->assertEquals(16, $img->getHeight()); + + // reset to 30x20 + $img->reset('30x20'); + $this->assertEquals(30, $img->getWidth()); + $this->assertEquals(20, $img->getHeight()); + + // reset to original again + $img->reset('original'); + $this->assertEquals(16, $img->getWidth()); + $this->assertEquals(16, $img->getHeight()); + } + public function testLimitColors() { $img = $this->manager()->make('tests/images/trim.png'); diff --git a/tests/ResetCommandTest.php b/tests/ResetCommandTest.php index a9e16dc4..18d823dd 100644 --- a/tests/ResetCommandTest.php +++ b/tests/ResetCommandTest.php @@ -15,7 +15,10 @@ class ResetCommandTest extends PHPUnit_Framework_TestCase $size = Mockery::mock('Intervention\Image\Size', array(800, 600)); $resource = imagecreatefromjpeg(__DIR__.'/images/test.jpg'); $image = Mockery::mock('Intervention\Image\Image'); + $driver = Mockery::mock('Intervention\Image\Gd\Driver'); + $driver->shouldReceive('cloneCore')->with($resource)->once()->andReturn($resource); $image->shouldReceive('getCore')->once()->andReturn($resource); + $image->shouldReceive('getDriver')->once()->andReturn($driver); $image->shouldReceive('setCore')->once(); $image->shouldReceive('getBackup')->once()->andReturn($resource); $command = new ResetGd(array()); @@ -28,6 +31,9 @@ class ResetCommandTest extends PHPUnit_Framework_TestCase $size = Mockery::mock('Intervention\Image\Size', array(800, 600)); $resource = imagecreatefromjpeg(__DIR__.'/images/test.jpg'); $image = Mockery::mock('Intervention\Image\Image'); + $driver = Mockery::mock('Intervention\Image\Gd\Driver'); + $driver->shouldReceive('cloneCore')->with($resource)->once()->andReturn($resource); + $image->shouldReceive('getDriver')->once()->andReturn($driver); $image->shouldReceive('getCore')->once()->andReturn($resource); $image->shouldReceive('setCore')->once(); $image->shouldReceive('getBackup')->once()->withArgs(array('fooBackup'))->andReturn($resource);