1

I have a white icon (256x256) with a transparent background. Somehow, I want to be able to change the white icon, which has some transparent pixels in it (for anti-aliasing), to any RGB color.

I have tried using the following function but

imagefilter($im, IMG_FILTER_COLORIZE, 0, 255, 0)

Is there any way to do this in PHP GD? What functions can I look into?

Anonymous
  • 553
  • 5
  • 17
  • 41
  • This may help with preserving the transparency (which looks like the issue): http://stackoverflow.com/questions/5276939/using-gd-to-change-the-color-of-a-one-color-shape-on-a-transparent-background-wh – nickhar May 12 '13 at 15:51

1 Answers1

0

I just created the following code and it works wonders.

Beware: If you set $backgroundTransparent to false, the image may lose quality when the background is painted under it.

<?php

    $width = 256;
    $height = 256;

    $backgroundColor = array(0, 255, 0);
    $backgroundTransparent = true;

    $icon = imagecreatefrompng('Access-New.png');
    imagealphablending($icon, false);
    imagesavealpha($icon, true);

    imagefilter($icon, IMG_FILTER_BRIGHTNESS, -255);
    imagefilter($icon, IMG_FILTER_COLORIZE, 255, 0, 0);

    if($backgroundTransparent == false) {
        $background = imagecreatetruecolor($width, $height);

        imagefill($background, 0, 0, imagecolorallocate($background, $backgroundColor[0], $backgroundColor[1], $backgroundColor[2]));

        imagealphablending($icon, true);

        imagecopy($background, $icon, 0, 0, 0, 0, $width, $height);

        imagepng($background, NULL, 0, PNG_NO_FILTER);
    }
    else {
        imagepng($icon, NULL, 0, PNG_NO_FILTER);
    }

    header("Content-type: image/png");
    imagedestroy($background);
?>
Anonymous
  • 553
  • 5
  • 17
  • 41