To use colors, you must first define them with the imagecolorallocate() function. The imagecolorallocate() function has four arguments: an image resource and four numbers between 0 and 255 representing red green and blue. imagecolorallocate() returns a variable called a color resource is used to define the colors of backgrounds, pixels, lines, shapes, fills and text. The first color you define will become the default (background) color of your image. The following block of code is the source code of the image below.
<?
header("Content-type: image/gif");
$image = imagecreate( 50, 50);
$color = imagecolorallocate( $image, 0, 0, 250);
imagegif($image);
imagedestroy($image);
?>
Use the imagecolortransparent() function to make a color transparent. imagecolortransparent() accepts two arguments, an image resource and a color resource. When the image is generated, the color resource will be transparent
imagecolortransparent( $image, $basecolor);
# makes $basecolor transparent
The block of code below creates an image resource and makes a white background color then creates a blue color resource and uses it to draw a line. Finally it uses imagecolortransparent() to make the white background color transparent and outputs the image below.
<?
header("Content-type: image/gif");
$image = imagecreate( 200, 80);
$basecolor = imagecolorallocate( $image, 255, 255, 255);
imagecolortransparent( $image, $basecolor);
$color = imagecolorallocate( $image, 0, 0, 250);
imagesetthickness( $image, 5 );
$x1=20;
$y1=20;
$x2=60;
$y2=180;
imageline($image, $x1, $y1, $y2, $x2, $color );
imagegif($image);
imagedestroy($image);
?>
|
|
|