You are here

public static function GdImageAnalysis::difference in Image Effects 8.3

Same name and namespace in other branches
  1. 8 src/Component/GdImageAnalysis.php \Drupal\image_effects\Component\GdImageAnalysis::difference()
  2. 8.2 src/Component/GdImageAnalysis.php \Drupal\image_effects\Component\GdImageAnalysis::difference()

Generates a GD resource calculating the difference of two images.

The images must have the same dimensions.

Parameters

resource $image1: A GD image resource.

resource $image2: A GD image resource.

Return value

resource A GD image resource, with the subtracted image, or NULL if the dimensions of the two images differ.

3 calls to GdImageAnalysis::difference()
GdImageAnalysisTest::testDifference in tests/src/Unit/GdImageAnalysisTest.php
Verify the difference calculation.
ImageEffectsTestBase::assertImagesAreEqual in tests/src/Functional/ImageEffectsTestBase.php
Asserts that two GD images are equal.
ImageEffectsTestBase::assertImagesAreNotEqual in tests/src/Functional/ImageEffectsTestBase.php
Asserts that two GD images are not equal.

File

src/Component/GdImageAnalysis.php, line 50

Class

GdImageAnalysis
Image analysis helper methods for GD.

Namespace

Drupal\image_effects\Component

Code

public static function difference($image1, $image2) {
  if (imagesx($image1) !== imagesx($image2) || imagesy($image1) !== imagesy($image2)) {
    return NULL;
  }
  $difference = imagecreatetruecolor(imagesx($image1), imagesy($image1));
  for ($i = 0; $i < imagesx($image1); $i++) {
    for ($j = 0; $j < imagesy($image1); $j++) {
      $rgb1 = imagecolorat($image1, $i, $j);
      $r1 = $rgb1 >> 16 & 0xff;
      $g1 = $rgb1 >> 8 & 0xff;
      $b1 = $rgb1 & 0xff;
      $rgb2 = imagecolorat($image2, $i, $j);
      $r2 = $rgb2 >> 16 & 0xff;
      $g2 = $rgb2 >> 8 & 0xff;
      $b2 = $rgb2 & 0xff;
      imagesetpixel($difference, $i, $j, imagecolorallocate($difference, abs($r2 - $r1), abs($g2 - $g1), abs($b2 - $b1)));
    }
  }
  return $difference;
}