0

I'm trying to replace a background of an image with a new background.

The input image will be a greenbox image, which should get its green background replaced to transparent, afterwards a new background image should be inserted.

it should go something like that

at the moment im still struggeling at replacing the green pixels with transparent background, as the green pixels rgb-values are changing and not just some static rgb-value.

how could this be archived?

<?php

// rgbs
$red = 44;
$green = 255;
$blue = 56;
$alpha = .45;

// input img
$img = imagecreatefromjpeg('./img.jpg');

// new background
$bg  = imagecreatefromjpeg('./bg.jpg');

// replace background
for ($y = 0; $y < imagesy($img); $y++) {
    for ($x = 0; $x < imagesx($img); $x++) {
        $rgb = imagecolorat($img, $x, $y);
        $pixel_color = imagecolorsforindex($img, $rgb);

        $oldR = ($pixel_color['red'] - $alpha * $red) /  (1 - $alpha);
        $oldG = ($pixel_color['green'] - $alpha * $green ) / (1 - $alpha);
        $oldB = ($pixel_color['blue'] - $alpha * $blue) / (1 - $alpha);

        // replace green with transparent
        $color = imagecolorallocate($img, $oldR, $oldG, $oldB);
        imagesetpixel($img, $x, $y, $color);
        imagecolortransparent($img, $color);
    }
}

// save new img
imagesavealpha($img, true);            
imagepng($img, './output.png');

?>
  • 4
    And where is the question that belongs to this...uhm...question? – Till Helge May 22 '13 at 12:49
  • how would you replace the green background with a transparent background? the problem is, that not the whole background has the same rgb-value (even if it looks like that).. –  May 22 '13 at 12:52
  • I think you need to add some kind of approximation to "green". – Maxim Khan-Magomedov May 22 '13 at 12:53
  • Maybe you should add that to your ... thing. – webmaster777 May 22 '13 at 12:53
  • @MaximKhan-Magomedov: yep, that is what im looking for. unfortunally i have no clue how this could be archived. –  May 22 '13 at 12:55
  • I'd use "green" and +/- 10 on each channel, that would be about 2000 steps. – Maxim Khan-Magomedov May 22 '13 at 12:56
  • when we had our image processing class at school, i also had this problem. you should have a range of 'accepted' values of what would be tagged as 'green'. try separating the RGB values and put a range that if red < x, blue < y, and green > z where x,y and z are the thresholds you need to figure out. hope this gave you an idea on what to do next.. ;) – reikyoushin May 22 '13 at 12:57
  • Since RGB colors are defined by their Red, Green and Blue values, you should try to determine wich RGB values absolutely count as background. For example, the ones with Extremely high G values, compared to the Red and Blue. (that sounds weird) – webmaster777 May 22 '13 at 12:58
  • Will be quite difficult to achieve that with GD. ImageMagick might be a much better idea for advanced stuff like that. Have a look at [`Imagick::paintTransparentImage()`](http://www.php.net/manual/en/imagick.painttransparentimage.php). It allows setting a threshold for the color to be replaced. – Till Helge May 22 '13 at 13:01

0 Answers0