1

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

我尝试过使用以下功能,但是

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

有没有办法在 PHP GD 中做到这一点?我可以查看哪些功能?

4

1 回答 1

0

我刚刚创建了以下代码,它可以创造奇迹。

注意:如果将$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);
?>
于 2013-05-12T16:28:32.590 回答