function ToolkitGdTest::testManipulations
Same name in other branches
- 9 core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php \Drupal\KernelTests\Core\Image\ToolkitGdTest::testManipulations()
- 8.9.x core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php \Drupal\KernelTests\Core\Image\ToolkitGdTest::testManipulations()
- 10 core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php \Drupal\KernelTests\Core\Image\ToolkitGdTest::testManipulations()
Tests height, width and color for the corners for the final images.
Since PHP can't visually check that our images have been manipulated properly, build a list of expected color values for each of the corners and the expected height and widths for the final images.
@dataProvider providerTestImageFiles
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Image/ ToolkitGdTest.php, line 287
Class
- ToolkitGdTest
- Tests for the GD image toolkit.
Namespace
Drupal\KernelTests\Core\ImageCode
public function testManipulations(string $file_name, string $test_case, string $operation, array $arguments, array $expected) : void {
// Load up a fresh image.
$image = $this->imageFactory
->get('core/tests/fixtures/files/' . $file_name);
$toolkit = $image->getToolkit();
$this->assertTrue($image->isValid());
$image_original_type = $image->getToolkit()
->getType();
$this->assertTrue(imageistruecolor($toolkit->getImage()), "Image '{$file_name}' after load should be a truecolor image, but it is not.");
// Perform our operation.
$image->apply($operation, $arguments);
// Flush Image object to disk storage.
$file_path = $this->directory . '/' . $test_case . image_type_to_extension($image->getToolkit()
->getType());
$image->save($file_path);
// Check that the both the GD object and the Image object have an accurate
// record of the dimensions.
if (isset($expected['height']) && isset($expected['width'])) {
$this->assertSame($expected['height'], imagesy($toolkit->getImage()), "Image '{$file_name}' after '{$test_case}' should have a proper height.");
$this->assertSame($expected['width'], imagesx($toolkit->getImage()), "Image '{$file_name}' after '{$test_case}' should have a proper width.");
$this->assertSame($expected['height'], $image->getHeight(), "Image '{$file_name}' after '{$test_case}' should have a proper height.");
$this->assertSame($expected['width'], $image->getWidth(), "Image '{$file_name}' after '{$test_case}' should have a proper width.");
}
// Now check each of the corners to ensure color correctness.
foreach ($expected['corners'] as $key => $expected_color) {
// The test gif that does not have transparency color set is a
// special case.
if ($file_name === 'image-test-no-transparency.gif') {
if ($test_case == 'desaturate') {
// For desaturating, keep the expected color from the test
// data, but set alpha channel to fully opaque.
$expected_color[3] = 0;
}
elseif ($expected_color === static::TRANSPARENT) {
// Set expected pixel to yellow where the others have
// transparent.
$expected_color = static::YELLOW;
}
}
// Get the location of the corner.
[
$x,
$y,
] = match ($key) { 0 => [
0,
0,
],
1 => [
$image->getWidth() - 1,
0,
],
2 => [
$image->getWidth() - 1,
$image->getHeight() - 1,
],
3 => [
0,
$image->getHeight() - 1,
],
};
$actual_color = $this->getPixelColor($image, $x, $y);
// If image cannot handle transparent colors, skip the pixel color test.
if ($actual_color[3] === 0 && $expected_color[3] === 127) {
continue;
}
// JPEG has small differences in color after processing.
$tolerance = $image_original_type === IMAGETYPE_JPEG ? 3 : 0;
$this->assertColorsAreEqual($expected_color, $actual_color, $tolerance, "Image '{$file_name}' object after '{$test_case}' action has the correct color placement at corner '{$key}'");
}
// Check that saved image reloads without raising PHP errors.
$image_reloaded = $this->imageFactory
->get($file_path);
$this->assertInstanceOf(\GDImage::class, $image_reloaded->getToolkit()
->getImage());
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.