I have searched a lot and I found only few solutions (on google and stackoverflow so please don't mark this one as a duplicate unless there's really duplicate question), but problems are hard edges. Is there any proper way of changing base color of, let's say black shape png image with transparent background but to preserve soft edges?
This is an example image:
I want it to look like this:
but the solutions I found give me this one:
Since I will be using this on my localhost, only for personal use, any php library that could help achieve this is appreciated.
UPDATE:
This is the function that gives me 3rd image:
function LoadPNG($imgname)
{
$im = imagecreatefrompng ($imgname);
imagetruecolortopalette($im,false, 255);
$index = imagecolorclosest ( $im, 0,0,0 ); // GET BLACK COLOR
imagecolorset($im,$index,0,150,255); // SET COLOR TO BLUE
$name = basename($imgname);
imagepng($im, getcwd()."/tmp/$name" ); // save image as png
imagedestroy($im);
}
$dir = getcwd()."/img/";
$images = glob($dir."/*.png",GLOB_BRACE);
foreach($images as $image) {
LoadPNG($image);
}
Originally, this function was a solution for GIF images (palette of 255 colors) so I guess that's why there are hard edges. I am looking for a solution (improvement to this script) to preserve transparency and soft edges of PNG image.
EDIT 2:
I have found an interesting approach using html5 canvas and javascript here: http://users7.jabry.com/overlord/mug.html
Maybe someone could have an idea how to translate this into PHP if even possible.
NEW SOLUTION
In answers