diff --git a/core/modules/simpletest/files/img-test.webp b/core/modules/simpletest/files/img-test.webp
new file mode 100644
index 0000000000000000000000000000000000000000..c9c799cedc6c196ae94a8a17e9ec3a43b650798c
GIT binary patch
literal 104
zcmV-u0GIz#Nk&Fs00012MM6+kP&iCf0000lC%^;%Cm;|=+8=t2`4<or`9IqH2ePGX
zU=&GGq~Q=iJ0RLZV*qyji@N$X0wVfPG;Nc*$(<pog>90|;Qr+;NhD8J@u@#MfE_Ua
K<m5heTp0jUXDCeo

literal 0
HcmV?d00001

diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php
index 7b195cc..b855494 100644
--- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php
+++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php
@@ -235,8 +235,12 @@ public function save($destination) {
       $success = $function($this->getResource(), $destination, $this->configFactory->get('system.image.gd')->get('jpeg_quality'));
     }
     else {
-      // Always save PNG images with full transparency.
-      if ($this->getType() == IMAGETYPE_PNG) {
+      // Always save alpha supporting images with alpha information.
+      $alpha_types = [IMAGETYPE_PNG];
+      if (static::isWebpSupported()) {
+        $alpha_types[] = IMAGETYPE_WEBP;
+      }
+      if (in_array($this->getType(), $alpha_types)) {
         imagealphablending($this->getResource(), FALSE);
         imagesavealpha($this->getResource(), TRUE);
       }
@@ -440,7 +444,22 @@ public function extensionToImageType($extension) {
    *   IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG, IMAGETYPE_PNG, etc.).
    */
   protected static function supportedTypes() {
-    return array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
+    $supported_types = [IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF];
+    if (static::isWebpSupported()) {
+      $supported_types[] = IMAGETYPE_WEBP;
+    }
+    return $supported_types;
+  }
+
+  /**
+   * Checks if WEBP images can be supported.
+   *
+   * @return bool
+   *   TRUE if WEBP images can be processed by GD, FALSE otherwise.
+   */
+  public static function isWebpSupported() {
+//    return PHP_VERSION_ID >= 70100 && function_exists('imagewebp') ? TRUE : FALSE;
+    return TRUE;
   }
 
 }
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 0d1c5a6..7d63d2d 100644
--- a/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php
+++ b/core/modules/system/src/Plugin/ImageToolkit/Operation/gd/CreateNew.php
@@ -90,9 +90,13 @@ protected function execute(array $arguments) {
       return FALSE;
     }
 
+    // Handles IMAGETYPE_WEBP as it is missing before PHP 7.1.
+    $webp = $this->getToolkit()->isWebpSupported() ? IMAGETYPE_WEBP : -1;
+
     // Fill the resource with transparency as possible.
     switch ($type) {
       case IMAGETYPE_PNG:
+      case $webp:
         imagealphablending($res, FALSE);
         $transparency = imagecolorallocatealpha($res, 0, 0, 0, 127);
         imagefill($res, 0, 0, $transparency);
diff --git a/core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php b/core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php
index fe36c3d..a7fb86f 100644
--- a/core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php
@@ -52,6 +52,11 @@ protected function setUp() {
 
     // Set the image factory service.
     $this->imageFactory = $this->container->get('image.factory');
+
+    // For PHP 7.1+, ensure test PHP has webp extension installed.
+    if (PHP_VERSION_ID >= 70100) {
+      $this->assertTrue(function_exists('imagewebp'));
+    }
   }
 
   protected function checkRequirements() {
@@ -107,8 +112,14 @@ function testManipulations() {
     // Test that the image factory is set to use the GD toolkit.
     $this->assertEqual($this->imageFactory->getToolkitId(), 'gd', 'The image factory is set to use the \'gd\' image toolkit.');
 
+    // Get a blank Image object.
+    $image = $this->imageFactory->get();
+
     // Test the list of supported extensions.
     $expected_extensions = ['png', 'gif', 'jpeg', 'jpg', 'jpe'];
+    if ($image->getToolkit()->isWebpSupported()) {
+      $expected_extensions[] = 'webp';
+    }
     $supported_extensions = $this->imageFactory->getSupportedExtensions();
     $this->assertEqual($expected_extensions, array_intersect($expected_extensions, $supported_extensions));
 
@@ -121,7 +132,9 @@ function testManipulations() {
       'jpg' => IMAGETYPE_JPEG,
       'jpe' => IMAGETYPE_JPEG
     ];
-    $image = $this->imageFactory->get();
+    if ($image->getToolkit()->isWebpSupported()) {
+      $expected_image_types['webp'] = IMAGETYPE_WEBP;
+    }
     foreach ($expected_image_types as $extension => $expected_image_type) {
       $image_type = $image->getToolkit()->extensionToImageType($extension);
       $this->assertSame($expected_image_type, $image_type);
@@ -138,6 +151,9 @@ function testManipulations() {
       'image-test-no-transparency.gif',
       'image-test.jpg',
     );
+    if ($image->getToolkit()->isWebpSupported()) {
+      $files[] = 'img-test.webp';
+    }
 
     // Setup a list of tests to perform on each type.
     $operations = array(
@@ -212,6 +228,17 @@ function testManipulations() {
         'corners' => $default_corners,
       ),
     );
+    if ($image->getToolkit()->isWebpSupported()) {
+      $operations += [
+        'convert_webp' => [
+          'function' => 'convert',
+          'width' => 40,
+          'height' => 20,
+          'arguments' => array('extension' => 'webp'),
+          'corners' => $default_corners,
+        ],
+      ];
+    }
 
     // Systems using non-bundled GD2 don't have imagerotate. Test if available.
     if (function_exists('imagerotate')) {
@@ -377,7 +404,11 @@ function testManipulations() {
     }
 
     // Test creation of image from scratch, and saving to storage.
-    foreach (array(IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_JPEG) as $type) {
+    $image_types = [IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_JPEG];
+    if ($image->getToolkit()->isWebpSupported()) {
+      $image_types[] = IMAGETYPE_WEBP;
+    }
+    foreach ($image_types as $type) {
       $image = $this->imageFactory->get();
       $image->createNew(50, 20, image_type_to_extension($type, FALSE), '#ffff00');
       $file = 'from_null' . image_type_to_extension($type);
