From 12db22c4cbf44fe2579b4f30e142f0db3dbecf2b Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Tue, 24 Nov 2015 12:36:24 +0000 Subject: [PATCH] Issue #2485761 by mondrake, c470ip, jhedstrom: Support for transparent GIFs broken --- .../image/src/Tests/ImageFieldDisplayTest.php | 7 +- .../files/image-test-transparent-indexed.gif | Bin 0 -> 140 bytes .../ImageToolkit/Operation/gd/CreateNew.php | 6 +- .../system/src/Tests/Image/ToolkitGdTest.php | 66 ++++++++++++++---- 4 files changed, 61 insertions(+), 18 deletions(-) create mode 100644 core/modules/simpletest/files/image-test-transparent-indexed.gif diff --git a/core/modules/image/src/Tests/ImageFieldDisplayTest.php b/core/modules/image/src/Tests/ImageFieldDisplayTest.php index c4cf485b910..2c6a2e4cc29 100644 --- a/core/modules/image/src/Tests/ImageFieldDisplayTest.php +++ b/core/modules/image/src/Tests/ImageFieldDisplayTest.php @@ -346,6 +346,7 @@ class ImageFieldDisplayTest extends ImageFieldTestBase { $alt = $this->randomString(512); $title = $this->randomString(1024); $edit = array( + // Get the path of the 'image-test.png' file. 'files[settings_default_image_uuid]' => drupal_realpath($images[0]->uri), 'settings[default_image][alt]' => $alt, 'settings[default_image][title]' => $title, @@ -378,7 +379,8 @@ class ImageFieldDisplayTest extends ImageFieldTestBase { // Create alt text for the image. $alt = $this->randomMachineName(); - $nid = $this->uploadNodeImage($images[1], $field_name, 'article', $alt); + // Upload the 'image-test.gif' file. + $nid = $this->uploadNodeImage($images[2], $field_name, 'article', $alt); $node_storage->resetCache(array($nid)); $node = $node_storage->load($nid); $file = $node->{$field_name}->entity; @@ -413,7 +415,8 @@ class ImageFieldDisplayTest extends ImageFieldTestBase { $this->createImageField($private_field_name, 'article', array('uri_scheme' => 'private')); // Add a default image to the new field. $edit = array( - 'files[settings_default_image_uuid]' => drupal_realpath($images[1]->uri), + // Get the path of the 'image-test.gif' file. + 'files[settings_default_image_uuid]' => drupal_realpath($images[2]->uri), 'settings[default_image][alt]' => $alt, 'settings[default_image][title]' => $title, ); diff --git a/core/modules/simpletest/files/image-test-transparent-indexed.gif b/core/modules/simpletest/files/image-test-transparent-indexed.gif new file mode 100644 index 0000000000000000000000000000000000000000..eb81f59138bb32b7543f2c12b87e24250d1ae6aa GIT binary patch literal 140 zcmZ?wbhEHb6k!l#*v!kY;{Tbw{~xUVe{s+M`>Xz+Tl4??qW?$N{lB>K|C!zY?``>i zeZ&9DoBm(@|NlP&7%2W^VSK^N%b)||fy`iFu@ugetResource()); $this->assertTrue($image_truecolor, SafeMarkup::format('Image %file after load is a truecolor image.', array('%file' => $file))); - if ($image->getToolkit()->getType() == IMAGETYPE_GIF) { - if ($op == 'desaturate') { - // Transparent GIFs and the imagefilter function don't work together. - $values['corners'][3][3] = 0; - } - } - // Store the original GD resource. $old_res = $toolkit->getResource(); @@ -330,11 +323,21 @@ class ToolkitGdTest extends KernelTestBase { if ($image->getToolkit()->getType() != IMAGETYPE_JPEG && $image_original_type != IMAGETYPE_JPEG) { // Now check each of the corners to ensure color correctness. foreach ($values['corners'] as $key => $corner) { - // The test gif that does not have transparency has yellow where the - // others have transparent. - if ($file === 'image-test-no-transparency.gif' && $corner === $this->transparent) { - $corner = $this->yellow; + // The test gif that does not have transparency color set is a + // special case. + if ($file === 'image-test-no-transparency.gif') { + if ($op == 'desaturate') { + // For desaturating, keep the expected color from the test + // data, but set alpha channel to fully opaque. + $corner[3] = 0; + } + elseif ($corner === $this->transparent) { + // Set expected pixel to yellow where the others have + // transparent. + $corner = $this->yellow; + } } + // Get the location of the corner. switch ($key) { case 0: @@ -436,9 +439,46 @@ class ToolkitGdTest extends KernelTestBase { } /** - * Tests loading an image whose transparent color index is out of range. + * Tests for GIF images with transparency. */ - function testTransparentColorOutOfRange() { + function testGifTransparentImages() { + // Prepare a directory for test file results. + $directory = $this->publicFilesDirectory .'/imagetest'; + file_prepare_directory($directory, FILE_CREATE_DIRECTORY); + + // Test loading an indexed GIF image with transparent color set. + // Color at top-right pixel should be fully transparent. + $file = 'image-test-transparent-indexed.gif'; + $image = $this->imageFactory->get(drupal_get_path('module', 'simpletest') . '/files/' . $file); + $resource = $image->getToolkit()->getResource(); + $color_index = imagecolorat($resource, $image->getWidth() - 1, 0); + $color = array_values(imagecolorsforindex($resource, $color_index)); + $this->assertEqual($this->rotateTransparent, $color, "Image {$file} after load has full transparent color at corner 1."); + + // Test deliberately creating a GIF image with no transparent color set. + // Color at top-right pixel should be fully transparent while in memory, + // fully opaque after flushing image to file. + $file = 'image-test-no-transparent-color-set.gif'; + $file_path = $directory . '/' . $file ; + // Create image. + $image = $this->imageFactory->get(); + $image->createNew(50, 20, 'gif', NULL); + $resource = $image->getToolkit()->getResource(); + $color_index = imagecolorat($resource, $image->getWidth() - 1, 0); + $color = array_values(imagecolorsforindex($resource, $color_index)); + $this->assertEqual($this->rotateTransparent, $color, "New GIF image with no transparent color set after creation has full transparent color at corner 1."); + // Save image. + $this->assertTrue($image->save($file_path), "New GIF image {$file} was saved."); + // Reload image. + $image_reloaded = $this->imageFactory->get($file_path); + $resource = $image_reloaded->getToolkit()->getResource(); + $color_index = imagecolorat($resource, $image_reloaded->getWidth() - 1, 0); + $color = array_values(imagecolorsforindex($resource, $color_index)); + // Check explicitly for alpha == 0 as the rest of the color has been + // compressed and may have slight difference from full white. + $this->assertEqual(0, $color[3], "New GIF image {$file} after reload has no transparent color at corner 1."); + + // Test loading an image whose transparent color index is out of range. // This image was generated by taking an initial image with a palette size // of 6 colors, and setting the transparent color index to 6 (one higher // than the largest allowed index), as follows: