diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php index 6449c2a..1646f75 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php @@ -94,6 +94,10 @@ protected function execute(array $arguments) { imagedestroy($original_resource); return TRUE; } + else { + // In case of failure, restore the original resource. + $this->getToolkit()->setResource($original_resource); + } } return FALSE; } diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php index 076a1fa..4b4d051 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php +++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php @@ -73,6 +73,10 @@ protected function execute(array $arguments = array()) { imagedestroy($original_resource); return TRUE; } + else { + // In case of failure, restore the original resource. + $this->getToolkit()->setResource($original_resource); + } } return FALSE; } diff --git a/core/modules/system/src/Tests/Image/ToolkitGdTest.php b/core/modules/system/src/Tests/Image/ToolkitGdTest.php index 4e622a8..7304740 100644 --- a/core/modules/system/src/Tests/Image/ToolkitGdTest.php +++ b/core/modules/system/src/Tests/Image/ToolkitGdTest.php @@ -399,40 +399,40 @@ function testManipulations() { } } - // Test that 'create_new' operation does not leave orphaned GD resources. - $image = $this->imageFactory->get(drupal_get_path('module', 'simpletest') . '/files/image-test.png'); - $old_res = $image->getToolkit()->getResource(); - // Check if resource has been created successfully. - $this->assertTrue(is_resource($old_res)); - $image->createNew(20, 20); - $new_res = $image->getToolkit()->getResource(); - // Check if the original resource has been destroyed. - $this->assertFalse(is_resource($old_res)); - // Check if a new resource has been created successfully. - $this->assertTrue(is_resource($new_res)); - // Test failures of the 'create_new' operation. $image = $this->imageFactory->get(); $image->createNew(-50, 20); - $this->assertFalse($image->isValid(), 'create_new with negative width fails.'); + $this->assertFalse($image->isValid(), 'CreateNew with negative width fails.'); $image->createNew(50, 20, 'foo'); - $this->assertFalse($image->isValid(), 'create_new with invalid extension fails.'); + $this->assertFalse($image->isValid(), 'CreateNew with invalid extension fails.'); $image->createNew(50, 20, 'gif', '#foo'); - $this->assertFalse($image->isValid(), 'create_new with invalid color hex string fails.'); + $this->assertFalse($image->isValid(), 'CreateNew with invalid color hex string fails.'); $image->createNew(50, 20, 'gif', '#ff0000'); - $this->assertTrue($image->isValid(), 'create_new with valid arguments validates the Image.'); + $this->assertTrue($image->isValid(), 'CreateNew with valid arguments validates the Image.'); } /** * Tests that GD resources are freed from memory. */ public function testResourceDestruction() { + // Test that an Image object going out of scope releases its GD resource. $image = $this->imageFactory->get(drupal_get_path('module', 'simpletest') . '/files/image-test.png'); $res = $image->getToolkit()->getResource(); $this->assertTrue(is_resource($res), 'Successfully loaded image resource.'); - // Force the toolkit to go out of scope. $image = NULL; $this->assertFalse(is_resource($res), 'Image resource was destroyed after losing scope.'); + + // Test that 'create_new' operation does not leave orphaned GD resources. + $image = $this->imageFactory->get(drupal_get_path('module', 'simpletest') . '/files/image-test.png'); + $old_res = $image->getToolkit()->getResource(); + // Check if resource has been created successfully. + $this->assertTrue(is_resource($old_res)); + $image->createNew(20, 20); + $new_res = $image->getToolkit()->getResource(); + // Check if the original resource has been destroyed. + $this->assertFalse(is_resource($old_res)); + // Check if a new resource has been created successfully. + $this->assertTrue(is_resource($new_res)); } /**