PHP GD - blendmode alpha calculation

Sonay

Sonay#0001
Mar 24, 2016
100
51
I have a problem: if I change my calculation for alpha in imagecolorallocatealpha is it wrong.
I can not describe it so well, but I hope anybody can help with it.

Image:

Image 2:

Image 3:

You can see, the dragon in image 2 has a bad gray tone and in image 1 is all right so well, so I think my calucation is wrong. Image 3 is the original image

Code:
function blendmode($dst, $src, int $alpha)
   {
       $w = imagesx($src);
       $h = imagesy($src);

       for ($x = 0; $x < $w; $x++) {
           for ($y = 0; $y < $h; $y++) {
               $rgbDst = imagecolorsforindex($dst, imagecolorat($dst, $x, $y));
               $rgbSrc = imagecolorsforindex($src, imagecolorat($src, $x, $y));

               $r = min($rgbSrc['red'] + $rgbDst['red'], 255);
               $g = min($rgbSrc['green'] + $rgbDst['green'], 255);
               $b = min($rgbSrc['blue'] + $rgbDst['blue'], 255);
               $a = $rgbDst['alpha'] / 255 * 127 - $alpha;

               imagesetpixel($src, $x, $y, imagecolorallocatealpha($src, $r, $g, $b, $a));
           }
       }

       return $src;
   }
I don't know if my "alpha" function alright but I hope anyone can fix it.
Code:
imagealphablending($src, false);
imagesavealpha($src, true);

$w = imagesx($src);
$h = imagesy($src);

for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$rgb = imagecolorat($src, $x, $y);

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

imagesetpixel($src, $x, $y, imagecolorallocatealpha($src, $r, $g, $b, round($alpha / 255 * 127)));
}
}


Greetings
 
Last edited:

Users who are viewing this thread

Top