imagecopymerge PHP function helped me to create a fast method that replaces one color by another in true color images.
Beforehand for this purpose I had to loop over all the pixels of an image and replace color pixel by pixel using imagecolorat and imagesetpixel; this method is really slow for large images.
So here is the fast one:
<?php
function replaceColorInImage ($image, $old_r, $old_g, $old_b, $new_r, $new_g, $new_b)
{
imagecolortransparent ($image, imagecolorallocate ($image, $old_r, $old_g, $old_b));
$w = imagesx ($image);
$h = imagesy ($image);
$resImage = imagecreatetruecolor ($w, $h);
imagefill ($resImage, 0, 0, imagecolorallocate ($resImage, $new_r, $new_g, $new_b));
imagecopymerge ($resImage, $image, 0, 0, 0, 0, $w, $h, 100);
return $resImage;
}
?>