如果您收到以下非描述性錯誤訊息
Uncaught exception 'ImagickException' with message 'Compare images failed'
請檢查您的圖片尺寸!我將一個 21x20 的 png 與一個 20x20 的 png 進行比較,結果產生了該錯誤。我花了很久才弄清楚尺寸必須完全相同。
(PECL imagick 2, PECL imagick 3)
Imagick::compareImages — 比較影像與重建的影像
傳回一個包含重建影像和影像之間差異的陣列。
發生錯誤時拋出 ImagickException。
範例 1 使用 Imagick::compareImages()
比較影像並顯示重建的影像
<?php
$image1 = new imagick("image1.png");
$image2 = new imagick("image2.png");
$result = $image1->compareImages($image2, Imagick::METRIC_MEANSQUAREERROR);
$result[0]->setImageFormat("png");
header("Content-Type: image/png");
echo $result[0];
?>
如果您收到以下非描述性錯誤訊息
Uncaught exception 'ImagickException' with message 'Compare images failed'
請檢查您的圖片尺寸!我將一個 21x20 的 png 與一個 20x20 的 png 進行比較,結果產生了該錯誤。我花了很久才弄清楚尺寸必須完全相同。
「*絕對誤差*」metric 未列為可用的 metric 常數。但是,如果需要,您仍然可以使用它,方法是傳遞 AE 的內部常數定義,也就是 1。當您希望使用所需的模糊因子進行比較時,這非常有用。範例
<?php
// 初始化影像物件
$image1 = new imagick();
$image2 = new imagick();
// 設定模糊因子 (必須在讀入影像之前完成)
$image1->SetOption('fuzz', '2%');
// 讀入影像
$image1->readImage("php_step29_actual.png");
$image2->readImage("php_step29_correct.png");
// 使用 METRIC=1 (絕對誤差) 比較影像
$result = $image1->compareImages($image2, 1);
// 列印出結果
echo "影像比較 2% 模糊因子為: " . $result[1];
?>