function ToolkitGdTest::testManipulations

Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php \Drupal\KernelTests\Core\Image\ToolkitGdTest::testManipulations()
  2. 10 core/tests/Drupal/KernelTests/Core/Image/ToolkitGdTest.php \Drupal\KernelTests\Core\Image\ToolkitGdTest::testManipulations()
  3. 11.x 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 281

Class

ToolkitGdTest
Tests for the GD image toolkit.

Namespace

Drupal\KernelTests\Core\Image

Code

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->getResource()), "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->getResource()), "Image '{$file_name}' after '{$test_case}' should have a proper height.");
    $this->assertSame($expected['width'], imagesx($toolkit->getResource()), "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.
    switch ($key) {
      case 0:
        $x = 0;
        $y = 0;
        break;

      case 1:
        $x = $image->getWidth() - 1;
        $y = 0;
        break;

      case 2:
        $x = $image->getWidth() - 1;
        $y = $image->getHeight() - 1;
        break;

      case 3:
        $x = 0;
        $y = $image->getHeight() - 1;
        break;

    }
    $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);
  if (PHP_VERSION_ID >= 80000) {
    $this->assertInstanceOf(\GDImage::class, $image_reloaded->getToolkit()
      ->getResource());
  }
  else {
    $this->assertIsResource($image_reloaded->getToolkit()
      ->getResource());
    $this->assertSame(get_resource_type($image_reloaded->getToolkit()
      ->getResource()), 'gd');
  }
}

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