Grafika\Gd\Editor::equal PHP 메소드

equal() 공개 메소드

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.
리턴 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
            for ($y = 0; $y < $image1->getHeight(); $y++) {
                for ($x = 0; $x < $image1->getWidth(); $x++) {
                    // Get image1 pixel
                    $rgb1 = imagecolorat($image1->getCore(), $x, $y);
                    $r1 = $rgb1 >> 16 & 0xff;
                    $g1 = $rgb1 >> 8 & 0xff;
                    $b1 = $rgb1 & 0xff;
                    // Get image2 pixel
                    $rgb2 = imagecolorat($image2->getCore(), $x, $y);
                    $r2 = $rgb2 >> 16 & 0xff;
                    $g2 = $rgb2 >> 8 & 0xff;
                    $b2 = $rgb2 & 0xff;
                    // Compare pixel value
                    if ($r1 !== $r2 or $g1 !== $g2 or $b1 !== $b2) {
                        return false;
                    }
                }
            }
        }
        return true;
    }