在PHP GD中为白色图标添加颜色


Adding color to a white icon in PHP GD

我有一个透明背景的白色图标(256x256)。不知何故,我希望能够改变白色图标,其中有一些透明像素(抗锯齿),为任何RGB颜色。

我已经尝试使用以下函数,但是

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

是否有任何方法在PHP GD中做到这一点?我可以研究哪些函数?

我刚刚创建了下面的代码,它的工作很神奇。

注意:如果您将$backgroundTransparent设置为false,则在其下方绘制背景时图像可能会失去质量。

<?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);
?>