简体   繁体   English

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

[英]PHP - change background color to transparent

I need to change background color of PNG picture ( 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 ) to transparent.我需要改变PNG图片的背景颜色( 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 ) 到透明。 I use the following code which seems to me right:我使用以下在我看来正确的代码:

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

I used imagecolorat too to detect accurate color, but the result is the same - it doesn't work(the background is white(not transparent))!我也使用imagecolorat来检测准确的颜色,但结果是一样的 - 它不起作用(背景是白色(不透明))! What is my mistake?我的错误是什么?

Possible duplicates: PHP: How to make a green area (background) in an image transparent?可能的重复项: PHP:如何使图像中的绿色区域(背景)透明? and imagecolortransparent in PHP not working PHP 中的 imagecolortransparent 不起作用

I have solved this issue:我已经解决了这个问题:

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

I hope it helps someone else.我希望它可以帮助别人。

<?php
  $img = imagecreatefrompng("https://i.stack.imgur.com/4NoTj.png");  //image obj from url
  $bg_color = imagecolorat($img,1,1);  //get color of top-left pixel
  imagecolortransparent($img, $bg_color);  //matching px => transparent
  imagepng($img);  //output png
  imagedestroy($img);  //cleanup
?> 

Documentation: imagecolortransparent , imagepng文档: imagecolortransparentimagepng

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM