diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php
index eab55c1..0abd2d4 100644
--- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php
+++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php
@@ -137,6 +137,10 @@ public function apply($operation, array $arguments = array()) {
       $this->logger->warning($e->getMessage(), array());
       return FALSE;
     }
+    catch (\RuntimeException $e) {
+      $this->logger->warning($e->getMessage(), []);
+      return FALSE;
+    }
   }
 
 }
diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php
index 9e21c03..a67501b 100644
--- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php
+++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php
@@ -190,6 +190,9 @@ protected function validateArguments(array $arguments) {
    *
    * @return bool
    *   TRUE if the operation was performed successfully, FALSE otherwise.
+   *
+   * @throws \RuntimeException
+   *   If the operation can not be performed.
    */
   abstract protected function execute(array $arguments);
 
diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php
index 779deef..3ff986b 100644
--- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php
+++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationInterface.php
@@ -34,6 +34,8 @@
    *
    * @throws \InvalidArgumentException
    *   If one or more of the arguments are not valid.
+   * @throws \RuntimeException
+   *   If the operation can not be performed.
    */
   public function apply(array $arguments);
 
diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php
index f9cfb53..1f5c89b 100644
--- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php
+++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php
@@ -7,7 +7,9 @@
 
 namespace Drupal\system\Plugin\ImageToolkit;
 
+use Drupal\Component\Utility\Bytes;
 use Drupal\Component\Utility\Color;
+use Drupal\Component\Utility\Environment;
 use Drupal\Component\Utility\Unicode;
 use Drupal\Core\Config\ConfigFactoryInterface;
 use Drupal\Core\Form\FormStateInterface;
@@ -43,6 +45,13 @@ class GDToolkit extends ImageToolkitBase {
   protected $type;
 
   /**
+   * Memory required to perform a GD operation, in bytes.
+   *
+   * @var int
+   */
+  protected $requiredMemory;
+
+  /**
    * Image information from a file, available prior to loading the GD resource.
    *
    * This contains a copy of the array returned by executing getimagesize()
@@ -139,12 +148,25 @@ public function setResource($resource) {
    */
   public function getResource() {
     if (!is_resource($this->resource)) {
-      $this->load();
+      if (!$this->load()) {
+        return NULL;
+      };
     }
     return $this->resource;
   }
 
   /**
+   * Returns the memory required to perform a GD function.
+   *
+   * @return string|null
+   *   A human readable representation of the memory required to perform a GD
+   *   operation (e.g. '15.6 Mb').
+   */
+  public function getRequiredMemory() {
+    return $this->requiredMemory !== NULL ? format_size($this->requiredMemory) : NULL;
+  }
+
+  /**
    * {@inheritdoc}
    */
   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
@@ -181,6 +203,15 @@ protected function load() {
       return FALSE;
     }
 
+    // Check if enough memory is available.
+    if (!$this->checkAvailableMemory($this->getWidth(), $this->getHeight())) {
+      // If not, need to mark the image as invalid, to avoid useless attempts
+      // to manipulate it.
+      $this->preLoadInfo = NULL;
+      $this->logger->error("Not enough memory (required: %required_memory) to load image '%file'", ['%required_memory' => $this->getRequiredMemory(), '%file' => $this->getSource()]);
+      return FALSE;
+    }
+
     $function = 'imagecreatefrom' . image_type_to_extension($this->getType(), FALSE);
     if (function_exists($function) && $resource = $function($this->getSource())) {
       $this->setResource($resource);
@@ -436,4 +467,38 @@ public function extensionToImageType($extension) {
   protected static function supportedTypes() {
     return array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
   }
+
+  /**
+   * Ensures that there is enough memory available for a GD operation.
+   *
+   * GD functions that create new GD resources will fail fatally if there is
+   * not enough memory available to perform the operation. This method checks
+   * if there is enough memory available before calling the GD function. The
+   * algorithm implemented follows the one described in
+   * http://php.net/manual/en/function.imagecreatetruecolor.php#99623.
+   *
+   * @param int $width
+   *   The image width.
+   * @param int $height
+   *   The image height.
+   * @param int $bits_per_pixel
+   *   (optional) The bits per pixel of the image. Defaults to 32 (4 bytes).
+   * @param float $tweak_factor
+   *   (optional) A tweak factor as described in
+   *   http://php.net/manual/en/function.imagecreatetruecolor.php#99623.
+   *   Defaults to 1.7.
+   *
+   * @return bool
+   *   TRUE if there is sufficient memory to allow the operation.
+   *
+   * @see http://php.net/manual/en/function.imagecreatetruecolor.php#99623
+   */
+  public function checkAvailableMemory($width, $height, $bits_per_pixel = 32, $tweak_factor = 1.7) {
+    $this->memoryRequired = (int) ($width * $height * ($bits_per_pixel / 8) * $tweak_factor);
+    $usage = memory_get_usage(TRUE);
+    $memory_limit = ini_get('memory_limit');
+    $size = Bytes::toInt($memory_limit);
+    return Environment::checkMemoryLimit($usage + $this->memoryRequired, $memory_limit);
+  }
+
 }
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 ae72b82..8226dc7 100644
--- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php
+++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php
@@ -84,6 +84,11 @@ protected function validateArguments(array $arguments) {
    * {@inheritdoc}
    */
   protected function execute(array $arguments) {
+    // Check if enough memory is available.
+    if (!$this->getToolkit()->checkAvailableMemory($arguments['width'], $arguments['height'])) {
+      throw new \RuntimeException("Not enough memory (required: {$this->getToolkit()->getRequiredMemory()}) to perform the image '{$this->getPluginId()}' operation");
+    }
+
     // Get the image type.
     $type = $this->getToolkit()->extensionToImageType($arguments['extension']);
 
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 c77ff31..f5fc048 100644
--- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php
+++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/Rotate.php
@@ -98,6 +98,16 @@ protected function execute(array $arguments) {
       return FALSE;
     }
 
+    // Check that enough memory is available.
+    // @todo more accurate width and height of the image after rotation will be
+    // available after #1551686. In the meantime, the maximum width and height
+    // can never exceed the lenght of the diagonal of the rectangle represented
+    // by the image.
+    $diagonal = (int) sqrt(pow($this->getToolkit()->getWidth(), 2) + pow($this->getToolkit()->getHeight(), 2)) + 1;
+    if (!$this->getToolkit()->checkAvailableMemory($diagonal, $diagonal)) {
+      throw new \RuntimeException("Not enough memory (required: {$this->getToolkit()->getRequiredMemory()}) to perform the image '{$this->getPluginId()}' operation");
+    }
+
     // Stores the original GD resource.
     $original_res = $this->getToolkit()->getResource();
 
diff --git a/core/modules/system/src/Tests/Image/ToolkitGdTest.php b/core/modules/system/src/Tests/Image/ToolkitGdTest.php
index 7304740..8d9b476 100644
--- a/core/modules/system/src/Tests/Image/ToolkitGdTest.php
+++ b/core/modules/system/src/Tests/Image/ToolkitGdTest.php
@@ -412,9 +412,9 @@ function testManipulations() {
   }
 
   /**
-   * Tests that GD resources are freed from memory.
+   * Tests that memory is allocated and released properly.
    */
-  public function testResourceDestruction() {
+  public function testMemoryAllocation() {
     // 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();
@@ -433,6 +433,15 @@ public function testResourceDestruction() {
     $this->assertFalse(is_resource($old_res));
     // Check if a new resource has been created successfully.
     $this->assertTrue(is_resource($new_res));
+
+    // Test creation of an image that will exceed the memory available.
+    $old_res = $image->getToolkit()->getResource();
+    $image->createNew(20000, 20000);
+    $new_res = $image->getToolkit()->getResource();
+    // Check that a new resource has not been created, and the old one is still
+    // valid.
+    $this->assertEqual($new_res, $old_res);
+    $this->assertTrue(is_resource($old_res));
   }
 
   /**
diff --git a/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php b/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php
new file mode 100644
index 0000000..23edaee5
--- /dev/null
+++ b/core/modules/system/tests/src/Unit/Plugin/ImageToolkit/GdToolkitTest.php
@@ -0,0 +1,205 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Tests\system\Unit\Plugin\ImageToolkit\GdToolkitTest.
+ */
+
+namespace Drupal\Tests\system\Unit\Plugin\ImageToolkit;
+
+use Drupal\Core\Image\Image;
+use Drupal\Core\ImageToolkit\ImageToolkitInterface;
+use Drupal\Tests\UnitTestCase;
+
+/**
+ * Tests the GDToolkit class.
+ *
+ * @coversDefaultClass \Drupal\system\Plugin\ImageToolkit\GDToolkit
+ * @group Image
+ */
+class GdToolkitTest extends UnitTestCase {
+
+  /**
+   * Path of the image file used for testing.
+   *
+   * @var string
+   */
+  protected $source;
+
+  /**
+   * Image object under test.
+   *
+   * @var \Drupal\Core\Image\Image
+   */
+  protected $image;
+
+  /**
+   * The GD Image toolkit used for testing.
+   *
+   * @var \Drupal\Core\ImageToolkit\ImageToolkitInterface|\PHPUnit_Framework_MockObject_MockObject
+   */
+  protected $toolkit;
+
+  /**
+   * The GD Image toolkit operation used for testing.
+   *
+   * @var \Drupal\Core\ImageToolkit\ImageToolkitOperationInterface
+   */
+  protected $toolkitOperation;
+
+  /**
+   * @inheritdoc
+   */
+  protected function setUp() {
+    // Use the Druplicon image.
+    $this->source = __DIR__ . '/../../../../../../simpletest/files/image-test.jpg';
+  }
+
+  /**
+   * Sets the GD toolkit mock.
+   *
+   * @param array $stubs
+   *   (optional) Array containing methods to be replaced with stubs.
+   */
+  protected function setToolkitMock(array $stubs = []) {
+    $logger = $this->getMock('Psr\Log\LoggerInterface');
+    $operation_manager = $this->getMock('Drupal\Core\ImageToolkit\ImageToolkitOperationManagerInterface');
+    $config_factory = $this->getMock('Drupal\Core\Config\ConfigFactoryInterface');
+    $stream_wrapper_manager = $this->getMock('Drupal\Core\StreamWrapper\StreamWrapperManagerInterface');
+    $stubs = array_merge(['getPluginId'], $stubs);
+    $mock_builder = $this->getMockBuilder('Drupal\system\Plugin\ImageToolkit\GDToolkit');
+    $this->toolkit = $mock_builder
+      ->setConstructorArgs([[], '', [], $operation_manager, $logger, $config_factory, $stream_wrapper_manager])
+      ->setMethods($stubs)
+      ->getMock();
+    $this->toolkit->expects($this->any())
+      ->method('getPluginId')
+      ->will($this->returnValue('gd'));
+  }
+
+  /**
+   * Sets the GD toolkit operation mock.
+   *
+   * @param string $class_name
+   *   The name of the GD toolkit operation class to be mocked.
+   * @param ImageToolkitInterface $toolkit
+   *   The image toolkit object.
+   * @param array $stubs
+   *   (optional) Array containing methods to be replaced with stubs.
+   */
+  protected function setToolkitOperationMock($class_name, ImageToolkitInterface $toolkit, array $stubs = []) {
+    $logger = $this->getMock('Psr\Log\LoggerInterface');
+    $mock_builder = $this->getMockBuilder('Drupal\system\Plugin\ImageToolkit\Operation\gd\\' . $class_name);
+    $this->toolkitOperation = $mock_builder
+      ->setMethods($stubs)
+      ->setConstructorArgs([[], '', [], $toolkit, $logger])
+      ->getMock();
+  }
+
+  /**
+   * Mock function for GDToolkit::apply().
+   */
+  public function applyMockedOperation($operation, array $arguments = []) {
+    return $this->toolkitOperation->apply($arguments);
+  }
+
+  /**
+   * Test loading an image.
+   *
+   * @covers ::load
+   */
+  public function testLoad() {
+    $this->setToolkitMock(['checkAvailableMemory']);
+    $this->toolkit->expects($this->any())
+      ->method('checkAvailableMemory')
+      ->will($this->returnValue(TRUE));
+
+    $this->image = new Image($this->toolkit, $this->source);
+    $this->assertTrue(is_resource($this->image->getToolkit()->getResource()));
+    $this->assertTrue($this->image->isValid());
+    $this->assertEquals(40, $this->image->getWidth());
+    $this->assertEquals(20, $this->image->getHeight());
+  }
+
+  /**
+   * Simulates not enough memory to load an image.
+   *
+   * @covers ::load
+   */
+  public function testNotEnoughMemoryOnLoad() {
+    $this->setToolkitMock(['checkAvailableMemory']);
+    $this->toolkit->expects($this->any())
+      ->method('checkAvailableMemory')
+      ->will($this->returnValue(FALSE));
+
+    // Getting image from file validates the Image object, since GD toolkit is
+    // lazy loading the resource.
+    $this->image = new Image($this->toolkit, $this->source);
+    $this->assertTrue($this->image->isValid());
+    // Trying to get GD resource without enough memory yields to NULL resource
+    // and invalidates Image object.
+    $this->assertFalse(is_resource($this->image->getToolkit()->getResource()));
+    $this->assertFalse($this->image->isValid());
+    $this->assertEquals(NULL, $this->image->getWidth());
+    $this->assertEquals(NULL, $this->image->getHeight());
+  }
+
+  /**
+   * Simulates not enough memory to create a new image.
+   *
+   * @covers \Drupal\system\Plugin\ImageToolkit\Operation\gd\CreateNew::execute
+   *
+   * @expectedException \RuntimeException
+   */
+  public function testNotEnoughMemoryOnCreateNew() {
+    $this->setToolkitMock(['checkAvailableMemory', 'getToolkitOperation', 'apply']);
+    $this->setToolkitOperationMock('CreateNew', $this->toolkit);
+    $this->toolkit->expects($this->any())
+      ->method('checkAvailableMemory')
+      ->will($this->onConsecutiveCalls(TRUE, FALSE));
+    $this->toolkit->expects($this->any())
+      ->method('getToolkitOperation')
+      ->will($this->returnValue($this->toolkitOperation));
+    $this->toolkit->expects($this->once())
+      ->method('apply')
+      ->will($this->returnCallback([$this, 'applyMockedOperation']));
+
+    $this->image = new Image($this->toolkit, $this->source);
+    // Get the GD resource to force GDToolkit::load, where the memory check is
+    // mocked to succeed.
+    $this->assertTrue(is_resource($this->image->getToolkit()->getResource()));
+    // Try the Image::createNew operation, where the memory check is mocked
+    // to fail. Expected a runtime exception.
+    $this->image->createNew(100, 200);
+  }
+
+  /**
+   * Simulates not enough memory to rotate an image.
+   *
+   * @covers \Drupal\system\Plugin\ImageToolkit\Operation\gd\Rotate::execute
+   *
+   * @expectedException \RuntimeException
+   */
+  public function testNotEnoughMemoryOnRotate() {
+    $this->setToolkitMock(['checkAvailableMemory', 'getToolkitOperation', 'apply']);
+    $this->setToolkitOperationMock('Rotate', $this->toolkit);
+    $this->toolkit->expects($this->any())
+      ->method('checkAvailableMemory')
+      ->will($this->onConsecutiveCalls(TRUE, FALSE));
+    $this->toolkit->expects($this->any())
+      ->method('getToolkitOperation')
+      ->will($this->returnValue($this->toolkitOperation));
+    $this->toolkit->expects($this->once())
+      ->method('apply')
+      ->will($this->returnCallback([$this, 'applyMockedOperation']));
+
+    $this->image = new Image($this->toolkit, $this->source);
+    // Get the GD resource to force GDToolkit::load, where the memory check is
+    // mocked to succeed.
+    $this->assertTrue(is_resource($this->image->getToolkit()->getResource()));
+    // Try the Image::rotate operation, where the memory check is mocked
+    // to fail. Expected a runtime exception.
+    $this->image->rotate(120);
+  }
+
+}
