spatie / pixelmatch-php
Compare images using PHP
Fund package maintenance!
spatie
Installs: 7 282
Dependents: 6
Suggesters: 1
Security: 0
Stars: 42
Watchers: 4
Forks: 4
Open Issues: 0
Requires
- php: ^8.1
- symfony/process: ^6.3|^7.0
Requires (Dev)
- laravel/pint: ^1.13
- pestphp/pest: ^2.0
- phpstan/phpstan: ^1.10
- spatie/ray: ^1.28
README
This package can compare two images and return the percentage of matching pixels. It's a PHP wrapper around the Pixelmatch JavaScript library.
Here's a quick example on how to use the package.
use Spatie\Pixelmatch\Pixelmatch; $pixelmatch = Pixelmatch::new("path/to/file1.png", "path/to/file2.png"); $pixelmatch->matchedPixelPercentage(); // returns a float, for example 97.5 $pixelmatch->mismatchedPixelPercentage(); // returns a float, for example 2.5
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
composer require spatie/pixelmatch-php
In your project, or on your server, you must have the JavaScript package Pixelmatch
installed.
npm install pixelmatch
... or Yarn.
yarn add pixelmatch
Make sure you have installed Node 16 or higher.
Usage
Here's how you can get the percentage of matching pixels between two images.
use Spatie\Pixelmatch\Pixelmatch; $pixelmatch = Pixelmatch::new("path/to/file1.png", "path/to/file2.png"); $pixelmatch->matchedPixelPercentage(); // returns a float, for example 97.5 $pixelmatch->mismatchedPixelPercentage(); // returns a float, for example 2.5
To get the amount of matched and mismatched pixels, you can use these methods.
use Spatie\Pixelmatch\Pixelmatch; $pixelmatch = Pixelmatch::new("path/to/file1.png", "path/to/file2.png"); $pixelmatch->matchedPixels(); // returns an int $pixelmatch->mismatchedPixels(); // returns an int
You can use the matches function
to check if the images match.
use Spatie\Pixelmatch\Pixelmatch; $pixelmatch = Pixelmatch::new("path/to/file1.png", "path/to/file2.png"); $pixelmatch->matches(); // returns a boolean $pixelmatch->doesNotMatch(); // returns a boolean
Setting a threshold
To set the threshold for the amount of mismatching pixels, you can use the threshold
method. Higher values will make the comparison more sensitive. The threshold should be between 0 and 1.
If you don't set a threshold, we'll use the default value of 0.1
.
$pixelmatch->threshold(0.05);
Ignoring anti-aliasing
To ignore anti-aliased pixels, you can use the includeAa
method.
$pixelmatch->includeAa();
Limitations
The package can only compare png images.
Images with different dimensions cannot be compared. If you try to do this, a Spatie\Pixelmatch\Exceptions\CouldNotCompare
exception will be thrown.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.