You are here

protected function ImageEffectsTestBase::assertImagesAreEqual in Image Effects 8

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/ImageEffectsTestBase.php \Drupal\Tests\image_effects\Functional\ImageEffectsTestBase::assertImagesAreEqual()
  2. 8.2 tests/src/Functional/ImageEffectsTestBase.php \Drupal\Tests\image_effects\Functional\ImageEffectsTestBase::assertImagesAreEqual()

Asserts that two GD images are equal.

Some difference can be allowed to account for e.g. compression artifacts.

Parameters

\Drupal\Core\Image\ImageInterface $expected_image: A GD image resource for the expected image.

\Drupal\Core\Image\ImageInterface $actual_image: A GD image resource for the actual image.

int $max_diff: (optional) The maximum allowed difference, range from 0 to 255. Defaults to 1.

string $message: (optional) The message to display along with the assertion.

3 calls to ImageEffectsTestBase::assertImagesAreEqual()
ScaleAndSmartCropTest::testScaleAndSmartCrop in tests/src/Functional/Effect/ScaleAndSmartCropTest.php
Test the image_effects_scale_and_smart_crop effect.
ScaleAndSmartCropTest::testScaleAndSmartCropUpscale in tests/src/Functional/Effect/ScaleAndSmartCropTest.php
Test the image_effects_scale_and_smart_crop effect with upscaling.
SmartCropTest::testSmartCrop in tests/src/Functional/Effect/SmartCropTest.php
Test the image_effects_smart_crop effect.

File

tests/src/Functional/ImageEffectsTestBase.php, line 368

Class

ImageEffectsTestBase
Base test class for image_effects tests.

Namespace

Drupal\Tests\image_effects\Functional

Code

protected function assertImagesAreEqual(ImageInterface $expected_image, ImageInterface $actual_image, $max_diff = 1, $message = NULL) {

  // Only works with GD.
  $this
    ->assertSame('gd', $expected_image
    ->getToolkitId());
  $this
    ->assertSame('gd', $actual_image
    ->getToolkitId());

  // Check dimensions.
  $this
    ->assertSame($expected_image
    ->getWidth(), $actual_image
    ->getWidth());
  $this
    ->assertSame($expected_image
    ->getHeight(), $actual_image
    ->getHeight());

  // Image difference.
  $difference = GdImageAnalysis::difference($expected_image
    ->getToolkit()
    ->getResource(), $actual_image
    ->getToolkit()
    ->getResource());
  $mean = GdImageAnalysis::mean($difference);
  imagedestroy($difference);
  $this
    ->assertTrue($mean < $max_diff, $message);
}