DJ Mike's Tutorials: PHP

< ^ >


Images Pt.4: Points and Lines

imagesetpixel()

You usually can't see a single pixel with webtv but you can set the color of single pixel imagesetpixel() using the following format:



imagesetpixel( $image_resource, $x, $y, $color_resource );


with $x and $y the x and y coordinates from the upper left corner. For the y axis, positive numbers go down.

imageline()

You can make a line with the imageline() function which has the same format as imagesetpixel() with the addition of a second cooridinate pair to give a start and end point:



imageline($image_resource,  $x1, $y1, $y2, $x2, $color_resource );


The following block of code draws a blue line from (20,20) to (60, 180) and outputs the image below:



<?
header
("Content-type: image/gif");
$image imagecreate20080);
$basecolor imagecolorallocate$image255255255);
$color imagecolorallocate$image00250);

$x1=20;
$y1=20;
$x2=60;
$y2=180;

imageline($image,  $x1$y1$y2$x2$color );

imagegif($image);
imagedestroy($image);
?>


imagesetthickness()

If you want a thicker line than imageline() makes, you can change the default thickness with imagesetthickness(). imagesetthickness() also changes the thickness of lines drawn with imageellipse, imagerectangle, imagearc and imagepolygon. imagesetthickness uses this the format:



imagesetthickness( $image_resource, $thickness_in_pixels );


The following block of code is the same as the block above except it uses imagesetthickness() to change the default line thickness to 5 pixels:



<?
header
("Content-type: image/gif");
$image imagecreate20080);
$basecolor imagecolorallocate$image255255255);
$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>