略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: Imagick::compareImages

2024-12-22

Imagick::compareImages

(PECL imagick 2, PECL imagick 3)

Imagick::compareImagesCompares an image to a reconstructed image

说明

public Imagick::compareImages(Imagick $compare, int $metric): array

Returns an array containing a reconstructed image and the difference between images.

参数

compare

An image to compare to.

metric

Provide a valid metric type constant. Refer to this list of metric constants.

返回值

Returns an array containing a reconstructed image and the difference between images.

错误/异常

错误时抛出 ImagickException。

范例

示例 #1 Using Imagick::compareImages():

Compare images and display the reconstructed image

<?php

$image1 
= new imagick("image1.png");
$image2 = new imagick("image2.png");

$result $image1->compareImages($image2Imagick::METRIC_MEANSQUAREERROR);
$result[0]->setImageFormat("png");

header("Content-Type: image/png");
echo 
$result[0];

?>
add a noteadd a note

User Contributed Notes 2 notes

up
7
info at celeste-design dot de
7 years ago
If you get the following non-descriptive error message:

Uncaught exception 'ImagickException' with message 'Compare images failed'

Check your picture dimensions! i compared a 21x20 png to a 20x20 png which resulted in that error. Took me forever to figure out that the dimensions have to be exactly the same.
up
0
Tim K and Sam M @netflix
8 years ago
The *Absolute Error* metric is not listed as an available metric constant. However, you can still use it if needed by passing the internal constant definition for AE which is 1. This is useful when you wish to compare using a desired fuzz factor. Example:

<?php
// init the image objects
$image1 = new imagick();
$image2 = new imagick();

// set the fuzz factor (must be done BEFORE reading in the images)
$image1->SetOption('fuzz', '2%');

// read in the images
$image1->readImage("php_step29_actual.png");
$image2->readImage("php_step29_correct.png");

// compare the images using METRIC=1 (Absolute Error)
$result = $image1->compareImages($image2, 1);

// print out the result
echo "The image comparison 2% Fuzz factor is: " . $result[1];
?>

官方地址:https://www.php.net/manual/en/imagick.compareimages.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3