diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php index 394ffb347e..a772913e9f 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php @@ -249,7 +249,7 @@ public function save($destination) { } else { // Image types that support alpha need to be saved accordingly. - if (in_array($this->getType(), [IMAGETYPE_PNG, IMAGETYPE_WEBP])) { + if (in_array($this->getType(), [IMAGETYPE_PNG, IMAGETYPE_WEBP], TRUE)) { imagealphablending($this->getResource(), FALSE); imagesavealpha($this->getResource(), TRUE); } @@ -458,12 +458,8 @@ public function extensionToImageType($extension) { * An array of available image types. An image type is represented by a PHP * IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG, IMAGETYPE_PNG, etc.). */ - public static function supportedTypes() { - $supported_types = [IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF]; - if (function_exists('imagewebp')) { - $supported_types[] = IMAGETYPE_WEBP; - } - return $supported_types; + protected static function supportedTypes() { + return [IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_WEBP]; } } diff --git a/core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php b/core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php index 6c5b32d4aa..b597627141 100644 --- a/core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php +++ b/core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php @@ -108,14 +108,8 @@ public 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 (in_array(IMAGETYPE_WEBP, $image->getToolkit()->supportedTypes())) { - $expected_extensions[] = 'webp'; - } + $expected_extensions = ['png', 'gif', 'jpeg', 'jpg', 'jpe', 'webp']; $supported_extensions = $this->imageFactory->getSupportedExtensions(); $this->assertEqual($expected_extensions, array_intersect($expected_extensions, $supported_extensions)); @@ -127,10 +121,9 @@ public function testManipulations() { 'jpeg' => IMAGETYPE_JPEG, 'jpg' => IMAGETYPE_JPEG, 'jpe' => IMAGETYPE_JPEG, + 'webp' => IMAGETYPE_WEBP, ]; - if (in_array(IMAGETYPE_WEBP, $image->getToolkit()->supportedTypes())) { - $expected_image_types['webp'] = IMAGETYPE_WEBP; - } + $image = $this->imageFactory->get(); foreach ($expected_image_types as $extension => $expected_image_type) { $image_type = $image->getToolkit()->extensionToImageType($extension); $this->assertSame($expected_image_type, $image_type); @@ -146,10 +139,8 @@ public function testManipulations() { 'image-test.gif', 'image-test-no-transparency.gif', 'image-test.jpg', + 'img-test.webp', ]; - if (in_array(IMAGETYPE_WEBP, $image->getToolkit()->supportedTypes())) { - $files[] = 'img-test.webp'; - } // Setup a list of tests to perform on each type. $operations = [ @@ -223,18 +214,14 @@ public function testManipulations() { 'arguments' => ['extension' => 'png'], 'corners' => $default_corners, ], + 'convert_webp' => [ + 'function' => 'convert', + 'width' => 40, + 'height' => 20, + 'arguments' => ['extension' => 'webp'], + 'corners' => $default_corners, + ], ]; - if (in_array(IMAGETYPE_WEBP, $image->getToolkit()->supportedTypes())) { - $operations += [ - 'convert_webp' => [ - 'function' => 'convert', - 'width' => 40, - 'height' => 20, - 'arguments' => ['extension' => 'webp'], - 'corners' => $default_corners, - ], - ]; - } // Systems using non-bundled GD2 don't have imagerotate. Test if available. // @todo Remove the version check once @@ -396,9 +383,8 @@ public function testManipulations() { // conversion. The convert operation cannot handle that correctly. if ($image->getToolkit()->getType() == $image_original_type || $corner != $this->transparent) { $correct_colors = $this->colorsAreEqual($color, $corner); - $actual_color = implode(',', $color); - $expected_color = implode(',', $corner); - $this->assertTrue($correct_colors, "Color of image '{$file}' after '{$op}' operation at corner {$key} is ({$actual_color}), expected ({$expected_color})."); + $this->assertTrue($correct_colors, new FormattableMarkup('Image %file object after %action action has the correct color placement at corner %corner.', + ['%file' => $file, '%action' => $op, '%corner' => $key])); } } } @@ -410,7 +396,7 @@ public function testManipulations() { } // Test creation of image from scratch, and saving to storage. - foreach ($image->getToolkit()->supportedTypes() as $type) { + foreach ([IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_WEBP] 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);