PHP - 使图像背景透明,容忍 [英] PHP - Make image backgrounds transparent with tolerance

查看:124
本文介绍了PHP - 使图像背景透明,容忍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使三个图像透明,将它们层叠在一起。我按照这篇文章中的示例代码:使用php和保存透明png ,但提取的图像都有丑陋的白色边框,由于图像没有完全均匀的单色背景。即使他们可能出现白色,实际上通常有不同的灰度或甚至蓝色涉及。

so I am making three images transparent and layer them over each other. I followed example code in this post: Remove Image background with php and save transparent png but the extracted images all have ugly white borders around them due to the images do not have a complete uniform single-color background. Even though they may appear white, in fact there are usually different shades of grey or even blue involved.

现在我想删除图像中的这些丑陋的白色边框。
我在网上找到一个解决问题的java函数: http://www.logikdev.com/2011/10/05/make-image-backgrounds-transparent-with-tolerance/ 这是他使用的代码:

So now I want to remove these ugly white borders in the image. I found a java function online which solves the problem: http://www.logikdev.com/2011/10/05/make-image-backgrounds-transparent-with-tolerance/ Here is the code he uses:

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做到这一点。
谁能帮助我?

But I don't know how to do this with php. Anyone who can help me?

推荐答案

您可以使用 IMagick :: paintTransparentImage

此方法的签名如下

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

这种情况的一个示例是:

An example use case for this would be:

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

您必须使用$ fuzz参数播放arround才能获得所需的结果。

You will have to play arround with the $fuzz parameter to get the kind of results you are looking for.

这篇关于PHP - 使图像背景透明,容忍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆