PHP-将背景颜色更改为透明

PHP-将背景颜色更改为透明,php,gd,Php,Gd,我需要将PNG图片()的背景色更改为透明。在我看来,我使用的代码如下: $picture = imagecreatefrompng("https://chart.googleapis.com/chart?cht=lxy&chd=e:AACIERGZIiKqMzO7RETMVVXdZmbud3f.iIkQmZohqqsyu7xDzM1U3d5l7u92,hhiIivfFmZZmcCY.YYZmTgdQjWd3kk6g880asfu7r4sf4E22tGtGsfzzmZj9&chds

我需要将PNG图片()的背景色更改为透明。在我看来,我使用的代码如下:

$picture = imagecreatefrompng("https://chart.googleapis.com/chart?cht=lxy&chd=e:AACIERGZIiKqMzO7RETMVVXdZmbud3f.iIkQmZohqqsyu7xDzM1U3d5l7u92,hhiIivfFmZZmcCY.YYZmTgdQjWd3kk6g880asfu7r4sf4E22tGtGsfzzmZj9&chds=0.0,1.0&chs=105x75&chma=1,0,1,1&chco=42b6c9ff&chls=2.5,1.0,0.0&chxl=0:%7C%7C1:%7C%7C2:%7C");

$img_w = imagesx($picture);
$img_h = imagesy($picture);

imagealphablending($picture, false);        
imagesavealpha($picture, true);

$rgb =  imagecolorexact ($picture, 255,255,255);//imagecolorat($picture, 50, 50);
imagecolortransparent($picture, $rgb);

imagepng($picture, '../images/chart2.png');
imagedestroy($picture);
我也使用了
imagecolorat
来检测准确的颜色,但结果是一样的——它不起作用(背景是白色的(不是透明的))!我犯了什么错

可能的重复项: 及



文档:,

我已解决此问题:

<? 
$picture = imagecreatefrompng("../images/chart.png");

$img_w = imagesx($picture);
$img_h = imagesy($picture);

$newPicture = imagecreatetruecolor( $img_w, $img_h );
imagesavealpha( $newPicture, true );
$rgb = imagecolorallocatealpha( $newPicture, 0, 0, 0, 127 );
imagefill( $newPicture, 0, 0, $rgb );

$color = imagecolorat( $picture, $img_w-1, 1);

for( $x = 0; $x < $img_w; $x++ ) {
    for( $y = 0; $y < $img_h; $y++ ) {
        $c = imagecolorat( $picture, $x, $y );
        if($color!=$c){         
            imagesetpixel( $newPicture, $x, $y,    $c);             
        }           
    }
}

imagepng($newPicture, '../images/chart2.png');
imagedestroy($newPicture);
imagedestroy($picture);

<? 
$picture = imagecreatefrompng("../images/chart.png");

$img_w = imagesx($picture);
$img_h = imagesy($picture);

$newPicture = imagecreatetruecolor( $img_w, $img_h );
imagesavealpha( $newPicture, true );
$rgb = imagecolorallocatealpha( $newPicture, 0, 0, 0, 127 );
imagefill( $newPicture, 0, 0, $rgb );

$color = imagecolorat( $picture, $img_w-1, 1);

for( $x = 0; $x < $img_w; $x++ ) {
    for( $y = 0; $y < $img_h; $y++ ) {
        $c = imagecolorat( $picture, $x, $y );
        if($color!=$c){         
            imagesetpixel( $newPicture, $x, $y,    $c);             
        }           
    }
}

imagepng($newPicture, '../images/chart2.png');
imagedestroy($newPicture);
imagedestroy($picture);