2014-11-25 33 views
1

我是新使用imagick與PHP,我有一個問題: 我比較兩個圖像與compareImages()函數。我使用相同的代碼作爲documentation page與Imagick比較兩幅圖像後刪除背景

<?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]; 

?> 

此代碼工作的權利,但結果圖像具有與有點混濁的原始圖像的背景。

我一直在尋找,我發現我想要的結果,但與ImageMagick的命令: http://www.imagemagick.org/Usage/compare/

我現在的結果是一樣的第一個命令的圖像(「比較bag_frame1.gif bag_frame2.gif比較。 GIF「),我想在這個命令顯示的一樣的結果:

compare bag_frame1.gif bag_frame2.gif \ 
     -compose Src compare_src.gif 

是否存在與compareImages這樣做()imagick功能的方法嗎?

感謝

回答

2

你需要閱讀的第一張圖像之前使用Imagick::SetOption

<?php 
$image1 = new imagick(); // Init empty object 
$image2 = new imagick("image2.png"); 
// Set Lowlight (resulting background) to transparent, not "original image with a bit of opacity" 
$image1->setOption('lowlight-color','transparent'); 
// Switch the default compose operator to Src, like in example 
$image1->setOption('compose', 'Src'); 
// Now read image 
$image1->readImage("image1.png"); 
// Result will not have a background 
$result = $image1->compareImages($image2, Imagick::METRIC_MEANSQUAREERROR); 
+0

它的工作! :)謝謝 – Sogeking 2014-11-25 14:32:59

+0

如果我將Lowlight設置爲透明,我會得到黑色背景,而不是透明的1。 – dardo82 2017-03-13 09:31:05