Grafika\Imagick\Editor::equal PHP Method

equal() public method

Compare if two images are equal. It will compare if the two images are of the same width and height. If the dimensions differ, it will return false. If the dimensions are equal, it will loop through each pixels. If one of the pixel don't match, it will return false. The pixels are compared using their RGB (Red, Green, Blue) values.
public equal ( string | Grafika\ImageInterface $image1, string | Grafika\ImageInterface $image2 ) : boolean
$image1 string | Grafika\ImageInterface Can be an instance of Image or string containing the file system path to image.
$image2 string | Grafika\ImageInterface Can be an instance of Image or string containing the file system path to image.
return boolean True if equals false if not. Note: This breaks the chain if you are doing fluent api calls as it does not return an Editor.
    public function equal($image1, $image2)
    {
        if (is_string($image1)) {
            // If string passed, turn it into a Image object
            $image1 = Image::createFromFile($image1);
            $this->flatten($image1);
        }
        if (is_string($image2)) {
            // If string passed, turn it into a Image object
            $image2 = Image::createFromFile($image2);
            $this->flatten($image2);
        }
        // Check if image dimensions are equal
        if ($image1->getWidth() !== $image2->getWidth() or $image1->getHeight() !== $image2->getHeight()) {
            return false;
        } else {
            // Loop using image1
            $pixelIterator = $image1->getCore()->getPixelIterator();
            foreach ($pixelIterator as $row => $pixels) {
                /* Loop through pixel rows */
                foreach ($pixels as $column => $pixel) {
                    /* Loop through the pixels in the row (columns) */
                    /**
                     * Get image1 pixel
                     * @var $pixel \ImagickPixel
                     */
                    $rgba1 = $pixel->getColor();
                    // Get image2 pixel
                    $rgba2 = $image2->getCore()->getImagePixelColor($column, $row)->getColor();
                    // Compare pixel value
                    if ($rgba1['r'] !== $rgba2['r'] or $rgba1['g'] !== $rgba2['g'] or $rgba1['b'] !== $rgba2['b']) {
                        return false;
                    }
                }
                $pixelIterator->syncIterator();
                /* Sync the iterator, this is important to do on each iteration */
            }
        }
        return true;
    }