Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Pixelmatch Php Laravel Package

spatie/pixelmatch-php

PHP wrapper around pixelmatch for fast, simple image comparison. Generate diff images and measure mismatch percentages to spot visual regressions in tests or validate uploads. Works with common image formats via GD/Imagick depending on setup.

View on GitHub
Deep Wiki
Context7

Compare images using PHP

Latest Version on Packagist Tests Total Downloads

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.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport