2

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.

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);

}

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

Community
  • 1
  • 1
user2501142
  • 23
  • 1
  • 3

2 Answers2

2

You can use the IMagick::paintTransparentImage

The signature for this method is as follows

 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");

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

Orangepill
  • 24,500
  • 3
  • 42
  • 63
0
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]);