【问题标题】:PHP - Make image backgrounds transparent with tolerancePHP - 使图像背景透明且具有容差
【发布时间】:2020-12-02 17:54:40
【问题描述】:

所以我让三个图像透明并将它们叠加在一起。我在这篇文章中遵循了示例代码:Remove Image background with php and save transparent png,但由于图像没有完全统一的单色背景,因此提取的图像周围都有难看的白色边框。尽管它们可能看起来是白色的,但实际上通常包含不同深浅的灰色甚至蓝色。

所以现在我想删除图像中这些难看的白色边框。 我在网上找到了一个解决问题的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 来做到这一点。 谁能帮帮我?

【问题讨论】:

    标签: php computer-vision


    【解决方案1】:

    您可以使用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 参数来获得您正在寻找的结果。

    【讨论】:

    • 对于我们这些无法访问 iMagick 的人来说,是否有解决方案?
    【解决方案2】:
    function transparant($url){
        $img = urldecode(trim($url));
        $imgx = imagecreatefromjpeg($img);
        $img_w = imagesx($imgx);
        $img_h = imagesy($imgx);
        $im = imagecreatetruecolor($img_w, $img_h);
        imagesavealpha( $im, true );
        $rgb = imagecolorallocatealpha( $im, 0, 0, 0, 127 );
        imagefill( $im, 0, 0, $rgb );
        $color = imagecolorat( $imgx, 1, 1);
        $temp = 0;
        $tolerance = 20;
        $temp = $tolerance * (0xFF000000 | 0xFF000000) / 100;
        $toleranceRGB = abs($temp);
        $startcolor = $color - $toleranceRGB;
        $endcolor = $color + $toleranceRGB;
    
        for( $x = 0; $x < $img_w; $x++ ) {
            for( $y = 0; $y < $img_h; $y++ ) {
                $c = imagecolorat( $imgx, $x, $y );
                $startcolor."|".$c."|".$endcolor."<br>";
                if ($c > $startcolor && $c < $endcolor){}else{
                    imagesetpixel( $im, $x, $y, $c);
                }
            }
        }
    
        $filename = basename($img);
        header('Content-type: image/png');
        imagepng($im, null);
        imagedestroy($im);
        imagedestroy($imgx);
    }
    
    transparant([url encoded image]);
    

    【讨论】:

      猜你喜欢
      • 2011-12-05
      • 1970-01-01
      • 2012-05-26
      • 2017-03-22
      • 1970-01-01
      • 2016-12-08
      • 2015-05-15
      • 2014-02-27
      • 1970-01-01
      相关资源
      最近更新 更多