Replace transparent color in PHP

I have a set of images. These images have a green alpha color. I need to replace this transparent color with a different color (I want to replace with white alpha color).

His code is:

$img = imagecreatefrompng($path . $file); 
$white_color_transparent = imagecolorallocatealpha($img, 255, 255, 255, 127);
for ($y = 0; $y < imagesy($img); $y++) {
    for ($x = 0; $x < imagesx($img); $x++) {
        $rgb = imagecolorat($img, $x, $y);
        $pixel_color = imagecolorsforindex($img, $rgb);
        if ($pixel_color['alpha'] != 0 && $pixel_color['alpha'] != 127){
            imagesetpixel($img, $x, $y, $white_color_transparent);
        }
    }
}

imagealphablending($img, false);
imagesavealpha($img, true);            
imagepng($img, $path .  $file);

And my result blooms the same as the original id image when I add to

imagesetpixel($img, $x, $y, $white_color_transparent);

this line:

imagesetpixel($img, $x, $y, $white_color);

I get only white color without transparency.

+5
source share
2 answers

Without seeing my image, I'm not sure there is an easy way to do this.

EDIT 2:

One thing that I noticed from my first edit is that it did not work for an image with transparency and did not have an opaque layer.

, . . .

<?php
//These colors here are the rgba values of the green transparency I used in my test image. You will need to know this to reverse the rgb blending.
$red = 28;
$green = 198;
$blue = 72;
$alpha = .48;

$img = imagecreatefrompng('test.png');

//Adding in the image blending that Zombaya brought up. Need this so it overwrites the pixel instead of blending it
imagealphablending($img, false);
for ($y = 0; $y < imagesy($img); $y++) {
    for ($x = 0; $x < imagesx($img); $x++) {
        $rgb = imagecolorat($img, $x, $y);
        $pixel_color = imagecolorsforindex($img, $rgb);

        //If pixel color matches our alpha layer color, we change it white transparent
        //Not sure how accurate the rounding for the alpha conversion is. Maybe the reason for the green edges in this example:
        if($pixel_color['red'] == $red && 
            $pixel_color['green'] == $green &&
            $pixel_color['blue'] == $blue &&
            $pixel_color['alpha'] == round(127 - ($alpha * 127))){
            $color = imagecolorallocatealpha($img, 255, 255, 255, 127);
        } else {
            //This is the algorithm from the link. But reordered to get the old color before the transparent color went over it.
            $oldR = ($pixel_color['red'] - $alpha * $red) /  (1 - $alpha);
            $oldG = ($pixel_color['green'] - $alpha * $green ) / (1 - $alpha);
            $oldB = ($pixel_color['blue'] - $alpha * $blue) / (1 - $alpha);

            $color = imagecolorallocatealpha($img, $oldR, $oldG, $oldB, $pixel_color['alpha']);
        }
        imagesetpixel($img, $x, $y, $color);
    }
}
imagesavealpha($img, true);            
imagepng($img, 'test_result.png');

enter image description here

enter image description here

enter image description here → . . , , .

EDIT:

, : RGB, -?

, . , , . , .

<?php
//These colors here are the rgba values of the green transparency I used in my test image. You will need to know this to reverse the rgb blending.
$red = 44;
$green = 215;
$blue = 56;
$alpha = .45;

$img = imagecreatefrompng('test2.png');

for ($y = 0; $y < imagesy($img); $y++) {
    for ($x = 0; $x < imagesx($img); $x++) {
        $rgb = imagecolorat($img, $x, $y);
        $pixel_color = imagecolorsforindex($img, $rgb);
            //This is the algorithm from the link. But reordered to get the old color before the transparent color went over it.
        $oldR = ($pixel_color['red'] - $alpha * $red) /  (1 - $alpha);
        $oldG = ($pixel_color['green'] - $alpha * $green ) / (1 - $alpha);
        $oldB = ($pixel_color['blue'] - $alpha * $blue) / (1 - $alpha);

        $color = imagecolorallocate($img, $oldR, $oldG, $oldB);
        imagesetpixel($img, $x, $y, $color);
    }
}
imagesavealpha($img, true);            
imagepng($img, 'test_result.png');

enter image description here -

enter image description here -

rgb, . :

out = alpha * new + (1 - alpha) * old

out - .

, , , :

old = (out - alpha * new) / (1 - alpha)

OLD ANSWER:

:

$white_color_transparent = imagecolorallocatealpha($img, 255, 255, 255, 127);

127 , , . .

:

$white_color = imagecolorallocatealpha($img, 255, 255, 255, 0); 

$white_color = imagecolorallocate($img, 255, 255, 255);

(0 ), .

, :

$white_color_semi_transparent = imagecolorallocatealpha($img, 255, 255, 255, 40); 

, , , .

. :

Starting Image -

enter image description here - 255,255,255,40

, :

  • -, , , .
  • -, , . . , , Photoshop, , . , PNG .

, . . , , , , , . , .

+18

Gohn67 .

, -, . , imagesetpixel(), .

imagealphablending:

-, .

:

$img = imagecreatefrompng($path.$file);
imagealphablending($img, false); // Turn off blending
$white_color_transparent = imagecolorallocatealpha($img, 255, 255, 255, 127);
for ($y = 0; $y < imagesy($img); $y++) {
    for ($x = 0; $x < imagesx($img); $x++) {
        $rgb = imagecolorat($img, $x, $y);
        $pixel_color = imagecolorsforindex($img, $rgb);
        if ($pixel_color['alpha'] != 0 && $pixel_color['alpha'] != 127){
            imagesetpixel($img, $x, $y, $white_color_transparent);
        }
    }
}
imagesavealpha($img, true);            
imagepng($img, $path.$file);

original image

resulting image
( , )

, . -, - :

imagesetpixel($img, $x, $y, imagecolorallocatealpha($img, 255, 255, 255, $pixel_color['alpha']));

, , .

0

All Articles