function GdToolkitImageManipulationTestBase::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.

Attributes

#[DataProvider('providerOperationTestCases')]

File

core/tests/Drupal/KernelTests/Core/Image/GdToolkitImageManipulationTestBase.php, line 287

Class

GdToolkitImageManipulationTestBase
GD image toolkit image manipulation test base class.

Namespace

Drupal\KernelTests\Core\Image

Code

public function testManipulations(string $test_case, string $operation, array $arguments, array $expected) : void {
  // Load up a fresh image.
  $image = $this->imageFactory
    ->get('core/tests/fixtures/files/' . $this->sourceTestImage);
  $toolkit = $image->getToolkit();
  $this->assertTrue($image->isValid());
  $image_original_type = $image->getToolkit()
    ->getType();
  $this->assertTrue(imageistruecolor($toolkit->getImage()), "Image '{$this->sourceTestImage}' 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 '{$this->sourceTestImage}' after '{$test_case}' should have a proper height.");
    $this->assertSame($expected['width'], imagesx($toolkit->getImage()), "Image '{$this->sourceTestImage}' after '{$test_case}' should have a proper width.");
    $this->assertSame($expected['height'], $image->getHeight(), "Image '{$this->sourceTestImage}' after '{$test_case}' should have a proper height.");
    $this->assertSame($expected['width'], $image->getWidth(), "Image '{$this->sourceTestImage}' 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) {
    // 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 and AVIF have small differences in color after processing.
    $tolerance = match ($image_original_type) {  IMAGETYPE_JPEG, IMAGETYPE_AVIF => 3,
      default => 1,
    
    };
    $this->assertColorsAreEqual($expected_color, $actual_color, $tolerance, "Image '{$this->sourceTestImage}' 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.