public function ToolkitGdTest::testGifTransparentImages in Drupal 8
Same name and namespace in other branches
- 9 core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php \Drupal\KernelTests\Core\Image\ToolkitGdTest::testGifTransparentImages()
Tests for GIF images with transparency.
File
- core/
tests/ Drupal/ KernelTests/ Core/ Image/ ToolkitGdTest.php, line 460
Class
- ToolkitGdTest
- Tests that core image manipulations work properly: scale, resize, rotate, crop, scale and crop, and desaturate.
Namespace
Drupal\KernelTests\Core\ImageCode
public function testGifTransparentImages() {
// Prepare a directory for test file results.
$directory = Settings::get('file_public_path') . '/imagetest';
\Drupal::service('file_system')
->prepareDirectory($directory, FileSystemInterface::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('core/tests/fixtures/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:
// @code
// $image = imagecreatefromgif('core/tests/fixtures/files/image-test.gif');
// imagecolortransparent($image, 6);
// imagegif($image, 'core/tests/fixtures/files/image-test-transparent-out-of-range.gif');
// @endcode
// This allows us to test that an image with an out-of-range color index
// can be loaded correctly.
$file = 'image-test-transparent-out-of-range.gif';
$image = $this->imageFactory
->get('core/tests/fixtures/files/' . $file);
$toolkit = $image
->getToolkit();
if (!$image
->isValid()) {
$this
->fail(new FormattableMarkup('Could not load image %file.', [
'%file' => $file,
]));
}
else {
// All images should be converted to truecolor when loaded.
$image_truecolor = imageistruecolor($toolkit
->getResource());
$this
->assertTrue($image_truecolor, new FormattableMarkup('Image %file after load is a truecolor image.', [
'%file' => $file,
]));
}
}