PHPackages                             sapientpro/image-comparator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Image &amp; Media](/categories/media)
4. /
5. sapientpro/image-comparator

ActiveLibrary[Image &amp; Media](/categories/media)

sapientpro/image-comparator
===========================

Compare images using PHP

v1.2.1(1y ago)60150.4k—0.5%9[1 PRs](https://github.com/sapientpro/image-comparator/pulls)5MITPHPPHP ^8.1

Since Apr 24Pushed 10mo ago3 watchersCompare

[ Source](https://github.com/sapientpro/image-comparator)[ Packagist](https://packagist.org/packages/sapientpro/image-comparator)[ RSS](/packages/sapientpro-image-comparator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (3)Versions (7)Used By (5)

Image Comparator: Compare images using PHP
==========================================

[](#image-comparator-compare-images-using-php)

[![https://packagist.org/packages/sapientpro/image-comparator](https://camo.githubusercontent.com/4aac78ce3c8b2e4c4036b089a3d3c79b33490c6b524f0bc99f63af884ef3d3fc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73617069656e7470726f2f696d6167652d636f6d70617261746f72)](https://camo.githubusercontent.com/4aac78ce3c8b2e4c4036b089a3d3c79b33490c6b524f0bc99f63af884ef3d3fc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73617069656e7470726f2f696d6167652d636f6d70617261746f72)[![https://packagist.org/packages/sapientpro/image-comparator](https://camo.githubusercontent.com/4774e987013427dbf8138eb02160fe58a01ecbf254d1af7ed8961912b5079036/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73617069656e7470726f2f696d6167652d636f6d70617261746f72)](https://camo.githubusercontent.com/4774e987013427dbf8138eb02160fe58a01ecbf254d1af7ed8961912b5079036/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f73617069656e7470726f2f696d6167652d636f6d70617261746f72)[![https://packagist.org/packages/sapientpro/image-comparator](https://camo.githubusercontent.com/4951dc5cef4b7a735975d5bf86b704475833aa28f9627f0f2f3a9a51683c8c27/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f73617069656e7470726f2f696d6167652d636f6d70617261746f72)](https://camo.githubusercontent.com/4951dc5cef4b7a735975d5bf86b704475833aa28f9627f0f2f3a9a51683c8c27/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f73617069656e7470726f2f696d6167652d636f6d70617261746f72)

- [Image Comparator: Compare images using PHP](#image-comparator-compare-images-using-php)
    - [Installation](#installation)
        - [Prerequisites](#prerequisites)
    - [Usage](#usage)
    - [Available methods](#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  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](http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html)and [phash.org](http://phash.org).

Installation
------------

[](#installation)

### Prerequisites

[](#prerequisites)

- PHP 8.1 or higher
- gd extension enabled

To install the library, run:

`composer require sapientpro/image-comparator`

Usage
-----

[](#usage)

`ImageComparator` is the core class of the library:

```
use SapientPro\ImageComparator\ImageComparator;

$imageComparator = new ImageComparator();
```

After creating an instance you can use one of the methods available:

```
$imageComparator->compare('your-images/your-image1.jpg', 'your-images/your-image12.jpg');
```

If the image path can't be resolved, `ImageResourceException` will be thrown:

```
$imageComparator->hashImage('your-images/non-existent-image.jpg'); // SapientPro\ImageComparator\ImageResourceException: Could not create an image resource from file
```

Example usage:

We have two images:

[![Equals1](https://github.com/sapientpro/image-comparator/raw/master/tests/images/ebay-image.png?raw=true)](https://github.com/sapientpro/image-comparator/blob/master/tests/images/ebay-image.png?raw=true)[![Equals2](https://github.com/sapientpro/image-comparator/raw/master/tests/images/amazon-image.png?raw=true)](https://github.com/sapientpro/image-comparator/blob/master/tests/images/amazon-image.png?raw=true)

Now, let's compare them:

```
use SapientPro\ImageComparator\ImageComparator;

$image1 = 'https://github.com/sapientpro/image-comparator/blob/feature/phasher-implementation/tests/images/ebay-image.png?raw=true';
$image2 = 'https://github.com/sapientpro/image-comparator/blob/feature/phasher-implementation/tests/images/amazon-image.png?raw=true';

$imageComparator = new ImageComparator();
$similarity = $imageComparator->compare($image1, $image2); //default hashing without rotation

echo $similarity; //87.5
```

The higher the result, the higher the similarity of images.

Let's compare different images:

[![Equals1](https://github.com/sapientpro/image-comparator/raw/master/tests/images/ebay-image2.png?raw=true)](https://github.com/sapientpro/image-comparator/blob/master/tests/images/ebay-image2.png?raw=true)[![Equals2](https://github.com/sapientpro/image-comparator/raw/master/tests/images/amazon-image2.png?raw=true)](https://github.com/sapientpro/image-comparator/blob/master/tests/images/amazon-image2.png?raw=true)

```
use SapientPro\ImageComparator\ImageComparator;

$image1 = 'https://github.com/sapientpro/image-comparator/blob/feature/phasher-implementation/tests/images/ebay-image2.png?raw=true';
$image2 = 'https://github.com/sapientpro/image-comparator/blob/feature/phasher-implementation/tests/images/amazon-image2.png?raw=true';

$imageComparator = new ImageComparator();
$similarity = $imageComparator->compare($image1, $image2); //default hashing without rotation

echo $similarity; //54.7
```

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

```
use SapientPro\ImageComparator\Enum\ImageRotationAngle;

$similarity = $imageComparator->compare($image1, $image2, ImageRotationAngle::D180); //compared image will be considered rotated by 180 degrees

echo $similarity; //95.3
```

You can also use `detect()` method which will rotate the compared image and return the highest percentage of similarity:

```
use SapientPro\ImageComparator\ImageComparator;

$image1 = 'https://raw.githubusercontent.com/sapientpro/phasher/feature/phasher-implementation/tests/images/forest1.jpg';
$image2 = 'https://raw.githubusercontent.com/sapientpro/phasher/feature/phasher-implementation/tests/images/forest1-copyrighted.jpg';

$imageComparator = new ImageComparator();
$similarity = $imageComparator->detect($image1, $image2);

echo $similarity; //95.3
```

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.

```
use SapientPro\ImageComparator\ImageComparator;

$image1 = 'https://raw.githubusercontent.com/sapientpro/phasher/feature/phasher-implementation/tests/images/forest1.jpg';
$image2 = 'https://raw.githubusercontent.com/sapientpro/phasher/feature/phasher-implementation/tests/images/forest1-copyrighted.jpg';
$image3 = 'https://raw.githubusercontent.com/sapientpro/phasher/feature/phasher-implementation/tests/images/forest.jpg';

$imageComparator = new ImageComparator();
$similarity = $imageComparator->compareArray(
    $image1,
    [
        'forest' => $image2,
        'anotherForest' => $image3
    ]
); // returns ['forest' => 95.33, 'anotherForest' => 75.22]
```

If needed, you can create a square image resource from another image and pass it to `compare()`, `compareArray()`, `detect()`, `detectArray` and `hashImage()` methods:

```
use SapientPro\ImageComparator\ImageComparator;

$image1 = 'https://raw.githubusercontent.com/sapientpro/phasher/feature/phasher-implementation/tests/images/forest1.jpg';
$image2 = 'https://raw.githubusercontent.com/sapientpro/phasher/feature/phasher-implementation/tests/images/forest1-copyrighted.jpg';

$imageComparator = new ImageComparator();

$squareImage1 = $imageComparator->squareImage($image1);
$squareImage2 = $imageComparator->squareImage($image2);

$similarity = $imageComparator->compare($squareImage1, $squareImage2);

echo $similarity //96.43;
```

If needed, you can convert the resulting array from `hashImage()` to a binary string and pass it to `compareHashStrings()` method:

```
use SapientPro\ImageComparator\ImageComparator;

$image1 = 'https://raw.githubusercontent.com/sapientpro/phasher/feature/phasher-implementation/tests/images/forest1.jpg';
$image2 = 'https://raw.githubusercontent.com/sapientpro/phasher/feature/phasher-implementation/tests/images/forest1-copyrighted.jpg';

$imageComparator = new ImageComparator();

$hash1 = $imageComparator->hashImage($image1);
$hash2 = $imageComparator->hashImage($image2);

$hashString1 = $imageComparator->convertHashToBinaryString($hash1);
$hashString2 = $imageComparator->convertHashToBinaryString($hash2);

$similarity = $imageComparator->compareHashStrings($hashString1, $hashString2);

echo $similarity //96.43;
```

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:

```
use SapientPro\ImageComparator\Strategy\DifferenceHashStrategy;

$imageComparator->setHashStrategy(new DifferenceHashStrategy());
$imageComparator->hashImage('image1.jpg');
```

If the strategy is set by `setHashingStrategy()`, it will be used under the hood in other comparison methods:

```
use SapientPro\ImageComparator\Strategy\DifferenceHashStrategy;

$imageComparator->setHashStrategy(new DifferenceHashStrategy());
$imageComparator->compare('image1.jpg', 'image2.jpg'); // images will be hashed using difference hash algorithm and then compared
```

Available methods
-----------------

[](#available-methods)

You can read about available methods in our [wiki page](https://github.com/sapientpro/image-comparator/wiki)

Developed by SapientPro Team - [Custom software development](https://sapient.pro)

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance49

Moderate activity, may be stable

Popularity48

Moderate usage in the ecosystem

Community23

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 61.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~170 days

Total

5

Last Release

438d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/49f5f12f2b5e4d05c45945a4f3e9b94142da5c90ef79b56d22ba3a18f1d7a372?d=identicon)[sapient.pro](/maintainers/sapient.pro)

---

Top Contributors

[![vadymtsots](https://avatars.githubusercontent.com/u/73906449?v=4)](https://github.com/vadymtsots "vadymtsots (26 commits)")[![D4lv1k](https://avatars.githubusercontent.com/u/92361604?v=4)](https://github.com/D4lv1k "D4lv1k (11 commits)")[![dkulyk](https://avatars.githubusercontent.com/u/370042?v=4)](https://github.com/dkulyk "dkulyk (2 commits)")[![igorhim](https://avatars.githubusercontent.com/u/4836291?v=4)](https://github.com/igorhim "igorhim (2 commits)")[![nalbantferhat](https://avatars.githubusercontent.com/u/85131089?v=4)](https://github.com/nalbantferhat "nalbantferhat (1 commits)")

---

Tags

image-comparisonimage-hashingphpphp-libraryphp8

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/sapientpro-image-comparator/health.svg)

```
[![Health](https://phpackages.com/badges/sapientpro-image-comparator/health.svg)](https://phpackages.com/packages/sapientpro-image-comparator)
```

###  Alternatives

[milon/barcode

Barcode generator like Qr Code, PDF417, C39, C39+, C39E, C39E+, C93, S25, S25+, I25, I25+, C128, C128A, C128B, C128C, 2-Digits UPC-Based Extention, 5-Digits UPC-Based Extention, EAN 8, EAN 13, UPC-A, UPC-E, MSI (Variation of Plessey code)

1.5k13.3M39](/packages/milon-barcode)[bkwld/croppa

Image thumbnail creation through specially formatted URLs for Laravel

510496.0k23](/packages/bkwld-croppa)[goat1000/svggraph

Generates SVG graphs

132849.6k3](/packages/goat1000-svggraph)[cohensive/embed

Media Embed (for Laravel or as a standalone).

120370.4k](/packages/cohensive-embed)[netresearch/rte-ckeditor-image

Image support in CKEditor for the TYPO3 ecosystem - by Netresearch

63991.3k4](/packages/netresearch-rte-ckeditor-image)[humanmade/tachyon-plugin

Rewrites WordPress image URLs to use Tachyon

87338.5k2](/packages/humanmade-tachyon-plugin)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
