开发者

How to change background color of image to green?

Now it's black.

function show(){
    $word=array("a","b","c","d","e","f");
    $name=$word[rand(0,5)];
    $im=imagecreatetruecolor(62,20);
    $black=imagecolorallocate($im,rand(20,100),rand(20,100),rand(20,100));
    $white=imagecolorallocate($im,255,255,255);

    imagettftext($im,14,1,15,17,$white,'simkai.ttf',$name);

    for($i=0;$i<200;$i++)
    {
           $randcolor  =  ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
           imagesetpixel($im,  rand()%70  ,  rand()%30  ,  $randcolor);
    }
    header("content-type: image/jpeg");
    imagejpeg($i开发者_开发技巧m);
}


you can use imagefill to fill the background with a give color. Also please not you add the $black variable but you never use it ? is that normal ?

function show(){
    $word=array("a","b","c","d","e","f");
    $name=$word[rand(0,5)];
    $im=imagecreatetruecolor(62,20);
    $black=imagecolorallocate($im,rand(20,100),rand(20,100),rand(20,100));
    $white=imagecolorallocate($im,255,255,255);
    imagefill ( $im , 0 ,0 , 0 )
    imagettftext($im,14,1,15,17,$white,'simkai.ttf',$name);

    for($i=0;$i<200;$i++)
    {
           $randcolor  =  ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
           imagesetpixel($im,  rand()%70  ,  rand()%30  ,  $randcolor);
    }
    header("content-type: image/jpeg");
 imagejpeg($im);
}


if you iterate over the pixels and know which color is the ''background'', just add a condition. But a better aproach might be to use an image with transparency for the background.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜