Tutorial :How to change background color of image to green?



Question:

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($im);  }  


Solution:1

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


Solution:2

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »