You are here

protected function ImageEffectsTestBase::assertImagesAreNotEqual in Image Effects 8.3

Asserts that two GD images are not 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.

1 call to ImageEffectsTestBase::assertImagesAreNotEqual()
BackgroundTest::testBackgroundEffect in tests/src/Functional/Effect/BackgroundTest.php
Background effect test.

File

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

Class

ImageEffectsTestBase
Base test class for image_effects tests.

Namespace

Drupal\Tests\image_effects\Functional

Code

protected function assertImagesAreNotEqual(ImageInterface $expected_image, ImageInterface $actual_image, $max_diff = 1, $message = '') : void {

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

  // Check dimensions.
  if ($expected_image
    ->getWidth() !== $actual_image
    ->getWidth()) {
    return;
  }
  if ($expected_image
    ->getHeight() !== $actual_image
    ->getHeight()) {
    return;
  }

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