function ToolkitGdTest::providerTestImageFiles

Same name in other branches
  1. 10 core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php \Drupal\KernelTests\Core\Image\ToolkitGdTest::providerTestImageFiles()
  2. 11.x core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php \Drupal\KernelTests\Core\Image\ToolkitGdTest::providerTestImageFiles()

Data provider for ::testManipulations().

File

core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php, line 106

Class

ToolkitGdTest
Tests for the GD image toolkit.

Namespace

Drupal\KernelTests\Core\Image

Code

public function providerTestImageFiles() : array {
    // Typically the corner colors will be unchanged. These colors are in the
    // order of top-left, top-right, bottom-right, bottom-left.
    $default_corners = [
        static::RED,
        static::GREEN,
        static::BLUE,
        static::TRANSPARENT,
    ];
    // Setup a list of tests to perform on each type.
    $test_cases = [
        'resize' => [
            'operation' => 'resize',
            'arguments' => [
                'width' => 20,
                'height' => 10,
            ],
            'width' => 20,
            'height' => 10,
            'corners' => $default_corners,
        ],
        'scale_x' => [
            'operation' => 'scale',
            'arguments' => [
                'width' => 20,
            ],
            'width' => 20,
            'height' => 10,
            'corners' => $default_corners,
        ],
        'scale_y' => [
            'operation' => 'scale',
            'arguments' => [
                'height' => 10,
            ],
            'width' => 20,
            'height' => 10,
            'corners' => $default_corners,
        ],
        'upscale_x' => [
            'operation' => 'scale',
            'arguments' => [
                'width' => 80,
                'upscale' => TRUE,
            ],
            'width' => 80,
            'height' => 40,
            'corners' => $default_corners,
        ],
        'upscale_y' => [
            'operation' => 'scale',
            'arguments' => [
                'height' => 40,
                'upscale' => TRUE,
            ],
            'width' => 80,
            'height' => 40,
            'corners' => $default_corners,
        ],
        'crop' => [
            'operation' => 'crop',
            'arguments' => [
                'x' => 12,
                'y' => 4,
                'width' => 16,
                'height' => 12,
            ],
            'width' => 16,
            'height' => 12,
            'corners' => array_fill(0, 4, static::WHITE),
        ],
        'scale_and_crop' => [
            'operation' => 'scale_and_crop',
            'arguments' => [
                'width' => 10,
                'height' => 8,
            ],
            'width' => 10,
            'height' => 8,
            'corners' => array_fill(0, 4, static::BLACK),
        ],
        'convert_jpg' => [
            'operation' => 'convert',
            'width' => 40,
            'height' => 20,
            'arguments' => [
                'extension' => 'jpeg',
            ],
            'corners' => $default_corners,
        ],
        'convert_gif' => [
            'operation' => 'convert',
            'width' => 40,
            'height' => 20,
            'arguments' => [
                'extension' => 'gif',
            ],
            'corners' => $default_corners,
        ],
        'convert_png' => [
            'operation' => 'convert',
            'width' => 40,
            'height' => 20,
            'arguments' => [
                'extension' => 'png',
            ],
            'corners' => $default_corners,
        ],
        'convert_webp' => [
            'operation' => 'convert',
            'width' => 40,
            'height' => 20,
            'arguments' => [
                'extension' => 'webp',
            ],
            'corners' => $default_corners,
        ],
    ];
    // Systems using non-bundled GD2 may miss imagerotate(). Test if available.
    if (function_exists('imagerotate')) {
        $test_cases += [
            'rotate_5' => [
                'operation' => 'rotate',
                // Fuchsia background.
'arguments' => [
                    'degrees' => 5,
                    'background' => '#FF00FF',
                ],
                // @todo Re-enable dimensions' check once
                //   https://www.drupal.org/project/drupal/issues/2921123 is resolved.
                // 'width' => 41,
                // 'height' => 23,
'corners' => array_fill(0, 4, static::FUCHSIA),
            ],
            'rotate_transparent_5' => [
                'operation' => 'rotate',
                'arguments' => [
                    'degrees' => 5,
                ],
                // @todo Re-enable dimensions' check once
                //   https://www.drupal.org/project/drupal/issues/2921123 is resolved.
                // 'width' => 41,
                // 'height' => 23,
'corners' => array_fill(0, 4, static::ROTATE_TRANSPARENT),
            ],
            'rotate_90' => [
                'operation' => 'rotate',
                // Fuchsia background.
'arguments' => [
                    'degrees' => 90,
                    'background' => '#FF00FF',
                ],
                'width' => 20,
                'height' => 40,
                'corners' => [
                    static::TRANSPARENT,
                    static::RED,
                    static::GREEN,
                    static::BLUE,
                ],
            ],
            'rotate_transparent_90' => [
                'operation' => 'rotate',
                'arguments' => [
                    'degrees' => 90,
                ],
                'width' => 20,
                'height' => 40,
                'corners' => [
                    static::TRANSPARENT,
                    static::RED,
                    static::GREEN,
                    static::BLUE,
                ],
            ],
        ];
    }
    // Systems using non-bundled GD2 may miss imagefilter(). Test if available.
    if (function_exists('imagefilter')) {
        $test_cases += [
            'desaturate' => [
                'operation' => 'desaturate',
                'arguments' => [],
                'height' => 20,
                'width' => 40,
                // Grayscale corners are a bit funky. Each of the corners are a shade of
                // gray. The values of these were determined simply by looking at the
                // final image to see what desaturated colors end up being.
'corners' => [
                    array_fill(0, 3, 76) + [
                        3 => 0,
                    ],
                    array_fill(0, 3, 149) + [
                        3 => 0,
                    ],
                    array_fill(0, 3, 29) + [
                        3 => 0,
                    ],
                    array_fill(0, 3, 225) + [
                        3 => 127,
                    ],
                ],
            ],
        ];
    }
    $ret = [];
    foreach ([
        'image-test.png',
        'image-test.gif',
        'image-test-no-transparency.gif',
        'image-test.jpg',
        'img-test.webp',
    ] as $file_name) {
        foreach ($test_cases as $test_case => $values) {
            $operation = $values['operation'];
            $arguments = $values['arguments'];
            unset($values['operation'], $values['arguments']);
            $ret[] = [
                $file_name,
                $test_case,
                $operation,
                $arguments,
                $values,
            ];
        }
    }
    return $ret;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.