DJ Mike's Tutorials: PHP

Images Pt.3


< ^ <

Using Colors

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 imagecreate5050);
$color imagecolorallocate$image00250);
imagegif($image);
imagedestroy($image);
?>


Transparency

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 imagecreate20080);
$basecolor imagecolorallocate$image255255255);
imagecolortransparent$image$basecolor);
$color imagecolorallocate$image00250);
imagesetthickness$image);
$x1=20;
$y1=20;
$x2=60;
$y2=180;
imageline($image,  $x1$y1$y2$x2$color );
imagegif($image);
imagedestroy($image);
?>



< ^ <



Created by DJ Mike from Santa Barbara

DJ Mike


Dance Away Santa Barbara's Home Page
<a href="http://www.statcounter.com/" target="_blank"> <img src="http://c5.statcounter.com/counter.php?sc_project=1321035&java=0&security=da2193dc" alt="counter free hit invisible" border="0" /></a>