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.4.0(1mo ago)62204.8k↓32.5%115MITPHPPHP ^8.2CI passing

Since Apr 24Pushed 1mo 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 2w ago

READMEChangelog (7)Dependencies (9)Versions (9)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.2 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;
```

If you want to compare using a single hash string input in `compare()` and `compareArray()`, you can create a serialized hash that includes both hash bits and average RGB:

```
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();

$serializedHash1 = $imageComparator->serializeImageHash($image1);
$serializedHash2 = $imageComparator->serializeImageHash($image2);

$similarity = $imageComparator->compare($serializedHash1, $serializedHash2);

echo $similarity //96.43;
```

Serialized hash format:

```
imgcmp-v1::::

```

`compareArray()` also accepts serialized hashes and mixed inputs:

```
$similarity = $imageComparator->compareArray(
    $serializedHash1,
    [
        'forest' => $serializedHash2,
        'anotherForest' => $image2
    ]
);
```

Note: rotation (`ImageRotationAngle`) is supported when image sources are passed to `compare()`. For serialized hashes, only default rotation (`D0`) is supported.

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

61

—

FairBetter than 99% of packages

Maintenance92

Actively maintained with recent releases

Popularity49

Moderate usage in the ecosystem

Community26

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 60% 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 ~195 days

Recently: every ~164 days

Total

7

Last Release

38d ago

PHP version history (2 changes)v1.0.0PHP ^8.1

v1.3.0PHP ^8.2

### 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 (27 commits)")[![D4lv1k](https://avatars.githubusercontent.com/u/92361604?v=4)](https://github.com/D4lv1k "D4lv1k (11 commits)")[![igorhim](https://avatars.githubusercontent.com/u/4836291?v=4)](https://github.com/igorhim "igorhim (2 commits)")[![dkulyk](https://avatars.githubusercontent.com/u/370042?v=4)](https://github.com/dkulyk "dkulyk (2 commits)")[![mic2100](https://avatars.githubusercontent.com/u/2852547?v=4)](https://github.com/mic2100 "mic2100 (1 commits)")[![nalbantferhat](https://avatars.githubusercontent.com/u/85131089?v=4)](https://github.com/nalbantferhat "nalbantferhat (1 commits)")[![fadisharif15](https://avatars.githubusercontent.com/u/54843730?v=4)](https://github.com/fadisharif15 "fadisharif15 (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

[laravel/framework

The Laravel Framework.

34.9k556.2M21.4k](/packages/laravel-framework)[illuminate/database

The Illuminate Database package.

2.8k55.8M13.1k](/packages/illuminate-database)[brick/money

Money and currency library

1.9k43.2M196](/packages/brick-money)[illuminate/validation

The Illuminate Validation package.

18838.8M2.0k](/packages/illuminate-validation)[flow-php/flow

PHP ETL - Extract Transform Load - Data processing framework

86337.5k](/packages/flow-php-flow)[flow-php/etl

PHP ETL - Extract Transform Load - Abstraction

378637.6k125](/packages/flow-php-etl)

PHPackages © 2026

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