I try to turn an grey color (rgb: 235,235,240) to transparent in an existing image with php gdlib.
This is the code i use:
<?php
header("Content-type:image/png");
$picture = imagecreatefrompng("test.png");
$grey = imagecolorallocate($picture, 235, 235, 240);
imagecolortransparent($picture, $grey);
imagepng($picture);
imagedestroy($picture, "newpicture.png");
?>
When test.png has a lot of different colors on it, this code won't work. Otherwise, when there are only a small amount of colors on test.png, this code works perfectly. Why?
It doesn't work because you are not saving the modified picture to disk.
Your current code:
imagepng($picture);
will send the modified picture to the browser, but you are outputting HTML code too:
<img src="mytest.png" />
Modify your code this way:
imagepng($picture, 'mytest.png'); // save the picture to disk
Your HTML code will then display the modified picture.
Check the documentation: imagepng
You must use this line to store the grey color into $grey
:
$grey = imagecolorallocate($picture, 235, 235, 240);
imagecolorresolve does a completely different thing.