user1036504
user1036504

Reputation: 3

Blending colors GD - PHP

Ok, I have a 64x64 pixels image, some pixels are white, some are grey, and some darker, so I have another 64x64 pixel image with some yellow pixels which will determine which pixels of the first image must be changed. So far I could change the colors on the first image with the following code, but the thing is I have no idea how to "blend" the given color with the colors that were already on the first image.

For example, if a pixel is white (255,255,255) and the new color is red (255,0,0) the result will be (255,0,0) but if the pixel is a bit darker, the new red should also be darker. Any ideas?

$image = 'o1.png';
$overlay = 'o2.png';

$background = imagecreatefrompng($image);

imagealphablending($background, true);

// Create overlay image
$overlay = imagecreatefrompng($overlay);

// get size
$size = getimagesize("o2.png");
$L=$size[0];
$H=$size[1];

for($j=0;$j<$H;$j++){
    for($i=0;$i<$L;$i++){

        $rgb = imagecolorat($overlay, $i, $j);

        $red = (isset($_GET['r']) ? $_GET['r'] : 0);
        $green = (isset($_GET['g']) ? $_GET['g'] : 0);
        $blue = (isset($_GET['b']) ? $_GET['b'] : 0);

        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;

        if(($r==255)&&($g==255)&&($b==0)) {

            $color = imagecolorallocate($background, $red, $green, $blue);
            imagesetpixel($background, $i, $j, $color);

        }

    }
}


header("Content-type: image/png");
header("Content-Disposition: filename=" . $image);

imagepng($background);

// Destroy the images
imagedestroy($background);
imagedestroy($overlay);

Upvotes: 0

Views: 1665

Answers (1)

David Gallagher
David Gallagher

Reputation: 733

I think you are talking about multiply blend mode. The formula for this according to Wikipedia is:

Result Color = (Top Color) * (Bottom Color) /255

Using this formula the resulting image will be darker where the background color is darker.

Upvotes: 2

Related Questions