Grafika\Gd\Editor::compare PHP Method

compare() public method

Compare two images and returns a hamming distance. A value of 0 indicates a likely similar picture. A value between 1 and 10 is potentially a variation. A value greater than 10 is likely a different image.
public compare ( Grafika\ImageInterface | string $image1, Grafika\ImageInterface | string $image2 ) : integer
$image1 Grafika\ImageInterface | string
$image2 Grafika\ImageInterface | string
return integer Hamming distance. Note: This breaks the chain if you are doing fluent api calls as it does not return an Editor.
    public function compare($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);
        }
        $hash = new DifferenceHash();
        $bin1 = $hash->hash($image1, $this);
        $bin2 = $hash->hash($image2, $this);
        $str1 = str_split($bin1);
        $str2 = str_split($bin2);
        $distance = 0;
        foreach ($str1 as $i => $char) {
            if ($char !== $str2[$i]) {
                $distance++;
            }
        }
        return $distance;
    }