Download the PHP package sapientpro/image-comparator without Composer
On this page you can find all versions of the php package sapientpro/image-comparator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package image-comparator
Image Comparator: Compare images using PHP
- Image Comparator: Compare images using PHP
- Installation
- Prerequisites
- Usage
- Available methods
Image Comparator is a PHP library for image comparison and hashing. You can compare 2 and more images using perceptual hashing method.
Based on https://github.com/kennethrapp/phasher package, with PHP 8 and PHPUnit support. The original project was abandoned in November 2017.
Perceptual hashing is a method to generate a hash of an image which allows multiple images to be compared by an index of similarity. You can find out more at The Hacker Factor and phash.org.
Installation
Prerequisites
- PHP 8.1 or higher
- gd extension enabled
To install the library, run:
composer require sapientpro/image-comparator
Usage
ImageComparator
is the core class of the library:
After creating an instance you can use one of the methods available:
If the image path can't be resolved, ImageResourceException
will be thrown:
Example usage:
We have two images:
https://github.com/sapientpro/image-comparator/blob/master/tests/images/ebay-image.png?raw=true
Now, let's compare them:
The higher the result, the higher the similarity of images.
Let's compare different images:
Rotation angle can be passed if compared image is rotated.
You must pass \SapientPro\ImageComparator\Enum\ImageRotationAngle enum with one of the following values:
D0
= 0 degress, D90
= 90 degrees, D180
= 180 degrees, D270
= 270 degrees
You can also use detect()
method which will rotate the compared image and return the highest percentage of similarity:
With compareArray()
and detectArray()
methods you can compare the source image to any number of images you want.
The behaviour is the same as in compare()
and detect()
methods.
If needed, you can create a square image resource from another image
and pass it to compare()
, compareArray()
, detect()
, detectArray
and hashImage()
methods:
If needed, you can convert the resulting array from hashImage()
to a binary string and pass it to compareHashStrings()
method:
By default, images are hashed using the average hashing algorithm,
which is implemented in SapientPro\ImageComparator\Strategy\AverageHashStrategy
.
This strategy is initialized in the constructor of ImageComparator
.
It is also possible to use difference hashing algorithm, implemented in SapientPro\ImageComparator\Strategy\DifferenceHashStrategy.
To use it, you need to call ImageComparator's setHashStrategy()
method and pass the instance of the strategy:
If the strategy is set by setHashingStrategy()
, it will be used under the hood in other comparison methods:
Available methods
You can read about available methods in our wiki page