2013-05-12 45 views
1

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

我曾尝试使用下面的功能,但

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

有没有办法在PHP GD做这个尝试?我可以查看哪些功能?

+0

这可能有助于保持透明度(看起来像问题):http://stackoverflow.com/questions/5276939/using-gd-to-change-the-color-of-one -color-形状上一个透明背景-WH – nickhar 2013-05-12 15:51:21

回答

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