2013-06-19 28 views
2

所以我讓三個圖像透明並將它們疊加在一起。我在這篇文章中遵循了示例代碼:Remove Image background with php and save transparent png但由於圖像沒有完整的統一單色背景,所以提取的圖像周圍都有難看的白色邊框。儘管它們可能看起來是白色的,但實際上通常有不同的灰色或藍色。PHP - 使圖像背景變得透明容忍

所以現在我想刪除圖像中這些醜陋的白色邊框。 我發現一個Java功能的在線解決了這個問題:http://www.logikdev.com/2011/10/05/make-image-backgrounds-transparent-with-tolerance/這裏是他使用的代碼:

private Image makeColorTransparent(final BufferedImage im, final Color color, int tolerance) { 
int temp = 0; 
if (tolerance < 0 || tolerance > 100) { 
    System.err.println("The tolerance is a percentage, so the value has to be between 0 and 100."); 
    temp = 0; 
} else { 
    temp = tolerance * (0xFF000000 | 0xFF000000)/100; 
} 
final int toleranceRGB = Math.abs(temp); 

final ImageFilter filter = new RGBImageFilter() { 
    // The color we are looking for (white)... Alpha bits are set to opaque 
    public int markerRGBFrom = (color.getRGB() | 0xFF000000) - toleranceRGB; 
    public int markerRGBTo = (color.getRGB() | 0xFF000000) + toleranceRGB; 

    public final int filterRGB(final int x, final int y, final int rgb) { 
     if ((rgb | 0xFF000000) >= markerRGBFrom && (rgb | 0xFF000000) <= markerRGBTo) { 
      // Mark the alpha bits as zero - transparent 
      return 0x00FFFFFF & rgb; 
     } else { 
      // Nothing to do 
      return rgb; 
     } 
    } 
}; 

final ImageProducer ip = new FilteredImageSource(im.getSource(), filter); 
return Toolkit.getDefaultToolkit().createImage(ip); 

}

但我不知道如何用PHP做到這一點。 任何人都可以幫助我?

回答

2

可以使用IMagick::paintTransparentImage

簽名這個方法如下

bool Imagick::paintTransparentImage (mixed $target , float $alpha , float $fuzz); 

一個例子使用案例,這將是:

$im = new Imagick("test.jpg"); 
$im->paintTransparentImage(($im->getImagePixelColor(0, 0), 0, 1200)); 
$im->setImageFormat("png"); 
$im->writeImage("test.png"); 

你將不得不角落找尋玩用$ fuzz參數來獲得你正在尋找的結果。

+0

對於我們這些無法訪問iMagick的人是否有解決方案? –