I want to colorize some PNGs using PHP GD. For testing purpose i hardcoded the color red (255,0,0) which later will be replace by a dynamic variable.
For example i have these two images:
Image 1:
Image 2:
Using my code, only image 2 works as it should.
The dog image however has some sort of gray box, don't know where the heck this comes from.
Here is the code I'm using:
<?php
$im = imagecreatefrompng('dog.png');
imagealphablending($im, false);
imagesavealpha($im, true);
$w = imagesx($im);
$h = imagesy($im);
for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$color = imagecolorsforindex($im, imagecolorat($im, $x, $y));
$r = ($color['red'] * 255) / 255;
$g = ($color['green'] * 0) / 255;
$b = ($color['blue'] * 0) / 255;
imagesetpixel($im, $x, $y, imagecolorallocatealpha($im, $r, $g, $b, $color['alpha']));
}
}
imagepng($im, 'result.png');
imagedestroy($im);
Why does it work with image 2 but not image 1? I only can think of some sort of alpha mask going on with image 1.
Hope somebody can help me