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.5We 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.
You can install the package via composer:
composer require spatie/pixelmatch-phpIn your project, or on your server, you must have the JavaScript package Pixelmatch installed.
npm install pixelmatch... or Yarn.
yarn add pixelmatchMake sure you have installed Node 16 or higher.
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.5To 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 intYou 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 booleanTo 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);To ignore anti-aliased pixels, you can use the includeAa method.
$pixelmatch->includeAa();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.
composer testPlease see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.