所以我将三个图像设为透明并将它们相互叠加。我按照这篇文章中的示例代码进行操作:用php删除图像背景并保存透明png https://stackoverflow.com/questions/10751227/remove-image-background-with-php-and-save-transparent-png但由于图像没有完整统一的单色背景,提取的图像周围都有难看的白色边框。尽管它们可能看起来是白色的,但实际上通常包含不同深浅的灰色甚至蓝色。
所以现在我想删除图像中这些丑陋的白色边框。
我在网上找到了一个java函数可以解决这个问题:http://www.logikdev.com/2011/10/05/make-image-backgrounds-transparent-with-tolerance/ 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 来做到这一点。
谁能帮助我?
您可以使用IMagick::paintTransparentImage http://php.net/manual/en/imagick.painttransparentimage.php
该方法的签名如下
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 参数才能获得您正在寻找的结果。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)