Skip to content
Vadim Matsotskyi edited this page Apr 25, 2023 · 3 revisions

Available Image Comparator methods

Here is the list of the available public methods and their example usage

hashImage()

Create a perceptual hash from an image. Returns an array of individual bits containing 0 and 1

$imageComparator->hashImage('your-images/your-image.jpg');

The method accepts the following arguments:

  • $image - image path or an instance of GdImage created by gd functions, e.g. imagecreatefromstring();

  • $rotation - image rotation angle enum instance, e.g. ImageRotationAngle::D90;

  • $size - the size of the thumbnail created from the original image - the hash size will be the square of this (so a value of 8 will build a hash out of 8x8 image, of 64 bits.)

Example:

$imageComparator->hashImage(image: 'your-images/your-image.jpg', rotation: ImageRotationAngle::D90, size: 16);

compare()

Compare two images and return the percentage of their similarity

Accepts the following arguments:

  • $sourceImage - image path or an instance of GdImage of the image to compare to;
  • $comparedImage - image path or an instance of GdImage of the compared image;
  • $rotation - image rotation angle enum instance, e.g. ImageRotationAngle::D90;
  • $precision - number of decimal points of the resulting percentage
$imageComparator->compare(
    sourceImage: 'your-images/your-image1.jpg',
    comparedImage: 'your-images/your-image2.jpg',
    rotation: ImageRotationAngle::D90,
    precision: 2
    ) //86.32

compareArray()

Same as compare(), but allows to compare source image to an array of images.

Returns an array percentages of the similarity of each image

Accepts the following arguments:

  • $sourceImage - image path or an instance of GdImage of the image to compare to;
  • $images - array of images;
  • $rotation - image rotation angle enum instance, e.g. ImageRotationAngle::D90;
  • $precision - number of decimal points of the resulting percentage
$imageComparator->compareArray(
    sourceImage: 'your-images/your-image1.jpg',
    images: ['image1' => 'your-images/your-image2.jpg', 'image2' => 'your-images/your-image2.jpg'],
    rotation: ImageRotationAngle::D90,
    precision: 2
    ) // ['image1' => 86.32, 'image2' => 21.33]

detect()

Compare two images through rotations of the compared image by 0, 90, 180 and 270 degrees and return the highest percentage of their similarity

Accepts the following arguments:

  • $sourceImage - image path or an instance of GdImage of the image to compare to;
  • $comparedImage - image path or an instance of GdImage of the compared image;
  • $precision - number of decimal points of the resulting percentage
$imageComparator->detect(
    sourceImage: 'your-images/your-image1.jpg',
    comparedImage: 'your-images/your-image2.jpg',
    precision: 2
    ) //86.32

detectArray()

Same as detect(), but allows to compare source image to an array of images. Returns the array of highest percentages of the similarity of each image

Accepts the following arguments:

  • $sourceImage - image path or an instance of GdImage of the image to compare to;
  • $images - array of images;
  • $precision - number of decimal points of the resulting percentage
$imageComparator->detect(
    sourceImage: 'your-images/your-image1.jpg',
    comparedImage: ['image1' => 'your-images/your-image2.jpg', 'image2' => 'your-images/your-image2.jpg'],
    precision: 2
    ) // ['image1' => 86.32, 'image2' => 21.33]

squareImage()

Create a square image resource from another rectangular image:

getimagesizefromstring('your-images/your-image1.jpg'); // width: 500, height: 250

$squareImage = $imageComparator->squareImage('your-images/your-image1.jpg');

imagesx($squareImage); // width: 500
imagesy($squareImage); // height: 500

convertHashToBinaryString()

Convert the resulting array from hashImage() to a binary string:

$hash = [0,1,1,1,0,1,0,0,1,1,1,1,0,0,0,0];

$binaryString = $imageComparator->convertHashToBinaryString($hash);

echo $binaryString // "0111010011110000"

compareHashStrings()

Compare hash strings. Rotation is not available:

$hash1 = [0,1,1,1,0,1,0,0,1,1,1,1,0,0,0,0];
$binaryString1 = $imageComparator->convertHashToBinaryString($hash1);

$hash2 = [0,1,1,1,0,1,0,0,1,1,1,0,0,0,0,0];
$binaryString2 = $imageComparator->convertHashToBinaryString($hash2);

$similarity = $imageComparator->compareHashStrings($binaryString1, $binaryString2);

echo $similarity //93.8