diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php
index d607303..ed08fd2 100644
--- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php
+++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php
@@ -196,6 +196,7 @@ protected function load() {
           'height' => imagesy($resource),
           'extension' => image_type_to_extension($this->getType(), FALSE),
           'transparent_color' => $this->getTransparentColor(),
+          'is_temp' => TRUE,
         );
         if ($this->apply('create_new', $data)) {
           imagecopy($this->getResource(), $resource, 0, 0, 0, 0, imagesx($resource), imagesy($resource));
diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Convert.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Convert.php
index a905871..344794d 100644
--- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Convert.php
+++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Convert.php
@@ -56,7 +56,8 @@ protected function execute(array $arguments) {
       'width' => $width,
       'height' => $height,
       'extension' => $arguments['extension'],
-      'transparent_color' => $this->getToolkit()->getTransparentColor()
+      'transparent_color' => $this->getToolkit()->getTransparentColor(),
+      'is_temp' => TRUE,
     );
     if ($this->getToolkit()->apply('create_new', $data)) {
       if (imagecopyresampled($this->getToolkit()->getResource(), $original_resource, 0, 0, 0, 0, $width, $height, $width, $height)) {
diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php
index 2b556f7..ae72b82 100644
--- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php
+++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php
@@ -43,6 +43,11 @@ protected function arguments() {
         'required' => FALSE,
         'default' => '#ffffff',
       ),
+      'is_temp' => array(
+        'description' => 'If TRUE, this operation is being used to create a temporary image by another GD operation. After performing its function, the caller is responsible for destroying the original GD resource.',
+        'required' => FALSE,
+        'default' => FALSE,
+      ),
     );
   }
 
@@ -82,6 +87,9 @@ protected function execute(array $arguments) {
     // Get the image type.
     $type = $this->getToolkit()->extensionToImageType($arguments['extension']);
 
+    // Store the original GD resource.
+    $original_res = $this->getToolkit()->getResource();
+
     // Create the resource.
     if (!$res = imagecreatetruecolor($arguments['width'], $arguments['height'])) {
       return FALSE;
@@ -119,6 +127,12 @@ protected function execute(array $arguments) {
     // Update the toolkit properties.
     $this->getToolkit()->setType($type);
     $this->getToolkit()->setResource($res);
+
+    // Destroy the original resource if it is not needed by other operations.
+    if (!$arguments['is_temp'] && is_resource($original_res)) {
+      imagedestroy($original_res);
+    }
+
     return TRUE;
   }
 
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 aed4a26..6449c2a 100644
--- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php
+++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Crop.php
@@ -86,7 +86,8 @@ protected function execute(array $arguments) {
       'width' => $arguments['width'],
       'height' => $arguments['height'],
       'extension' => image_type_to_extension($this->getToolkit()->getType(), FALSE),
-      'transparent_color' => $this->getToolkit()->getTransparentColor()
+      'transparent_color' => $this->getToolkit()->getTransparentColor(),
+      'is_temp' => TRUE,
     );
     if ($this->getToolkit()->apply('create_new', $data)) {
       if (imagecopyresampled($this->getToolkit()->getResource(), $original_resource, 0, 0, $arguments['x'], $arguments['y'], $arguments['width'], $arguments['height'], $arguments['width'], $arguments['height'])) {
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 2e4494c..076a1fa 100644
--- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php
+++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Resize.php
@@ -65,7 +65,8 @@ protected function execute(array $arguments = array()) {
       'width' => $arguments['width'],
       'height' => $arguments['height'],
       'extension' => image_type_to_extension($this->getToolkit()->getType(), FALSE),
-      'transparent_color' => $this->getToolkit()->getTransparentColor()
+      'transparent_color' => $this->getToolkit()->getTransparentColor(),
+      'is_temp' => TRUE,
     );
     if ($this->getToolkit()->apply('create_new', $data)) {
       if (imagecopyresampled($this->getToolkit()->getResource(), $original_resource, 0, 0, 0, 0, $arguments['width'], $arguments['height'], imagesx($original_resource), imagesy($original_resource))) {
diff --git a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php
index e7e3a6c..c77ff31 100644
--- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php
+++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php
@@ -98,17 +98,25 @@ protected function execute(array $arguments) {
       return FALSE;
     }
 
-    $this->getToolkit()->setResource(imagerotate($this->getToolkit()->getResource(), 360 - $arguments['degrees'], $arguments['background_idx']));
-
-    // GIFs need to reassign the transparent color after performing the rotate,
-    // but only do so, if the image already had transparency of its own, or the
-    // rotate added a transparent background.
-    if (!empty($arguments['gif_transparent_color'])) {
-      $transparent_idx = imagecolorexactalpha($this->getToolkit()->getResource(), $arguments['gif_transparent_color']['red'], $arguments['gif_transparent_color']['green'], $arguments['gif_transparent_color']['blue'], $arguments['gif_transparent_color']['alpha']);
-      imagecolortransparent($this->getToolkit()->getResource(), $transparent_idx);
+    // Stores the original GD resource.
+    $original_res = $this->getToolkit()->getResource();
+
+    if ($new_res = imagerotate($this->getToolkit()->getResource(), 360 - $arguments['degrees'], $arguments['background_idx'])) {
+      $this->getToolkit()->setResource($new_res);
+      imagedestroy($original_res);
+
+      // GIFs need to reassign the transparent color after performing the
+      // rotate, but only do so, if the image already had transparency of its
+      // own, or the rotate added a transparent background.
+      if (!empty($arguments['gif_transparent_color'])) {
+        $transparent_idx = imagecolorexactalpha($this->getToolkit()->getResource(), $arguments['gif_transparent_color']['red'], $arguments['gif_transparent_color']['green'], $arguments['gif_transparent_color']['blue'], $arguments['gif_transparent_color']['alpha']);
+        imagecolortransparent($this->getToolkit()->getResource(), $transparent_idx);
+      }
+
+      return TRUE;
     }
 
-    return TRUE;
+    return FALSE;
   }
 
 }
diff --git a/core/modules/system/src/Tests/Image/ToolkitGdTest.php b/core/modules/system/src/Tests/Image/ToolkitGdTest.php
index da8126b..4e622a8 100644
--- a/core/modules/system/src/Tests/Image/ToolkitGdTest.php
+++ b/core/modules/system/src/Tests/Image/ToolkitGdTest.php
@@ -278,9 +278,19 @@ function testManipulations() {
           }
         }
 
+        // Store the original GD resource.
+        $old_res = $toolkit->getResource();
+
         // Perform our operation.
         $image->apply($values['function'], $values['arguments']);
 
+        // If the operation replaced the resource, check that the old one has
+        // been destroyed.
+        $new_res = $toolkit->getResource();
+        if ($new_res !== $old_res) {
+          $this->assertFalse(is_resource($old_res), SafeMarkup::format("'%operation' destroyed the original resource.", ['%operation' => $values['function']]));
+        }
+
         // To keep from flooding the test with assert values, make a general
         // value for whether each group of values fail.
         $correct_dimensions_real = TRUE;
@@ -389,16 +399,28 @@ function testManipulations() {
       }
     }
 
-    // Test failures of CreateNew.
+    // 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(), 'CreateNew with negative width fails.');
+    $this->assertFalse($image->isValid(), 'create_new with negative width fails.');
     $image->createNew(50, 20, 'foo');
-    $this->assertFalse($image->isValid(), 'CreateNew with invalid extension fails.');
+    $this->assertFalse($image->isValid(), 'create_new with invalid extension fails.');
     $image->createNew(50, 20, 'gif', '#foo');
-    $this->assertFalse($image->isValid(), 'CreateNew with invalid color hex string fails.');
+    $this->assertFalse($image->isValid(), 'create_new with invalid color hex string fails.');
     $image->createNew(50, 20, 'gif', '#ff0000');
-    $this->assertTrue($image->isValid(), 'CreateNew with valid arguments validates the Image.');
+    $this->assertTrue($image->isValid(), 'create_new with valid arguments validates the Image.');
   }
 
   /**
