定制 spatie/pixelmatch-php 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

spatie/pixelmatch-php

最新稳定版本:1.2.1

Composer 安装命令:

composer require spatie/pixelmatch-php

包简介

Compare images using PHP

README 文档

README

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.

统计信息

  • 总下载量: 64.24k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 56
  • 点击次数: 1
  • 依赖项目数: 7
  • 推荐数: 1

GitHub 信息

  • Stars: 56
  • Watchers: 2
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 未知

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固