So far, we've just drawn a blank image using light gray as a background color. How about drawing a visible figure? We'll start by drawing lines using the imageline function, which works like this: imageline(resource image, int x1, int y1, int x2, int y2, int color) This function draws a line from (x1, y1) to (x2, y2) in image image of color color. Note that the top left of the image is (0, 0), and all measurements are in pixels. Here's an example where we'll draw some lines in a JPEG image using black as a drawing color: $image_height = 100; $image_width = 300; $image = imagecreate($image_width, $image_height); $back_color = imagecolorallocate($image, 200, 200, 200); $draw_color = imagecolorallocate($image, 0, 0, 0); imageline($image, 20, 20, 80, 80, $draw_color); imageline($image, 20, 90, 200, 10, $draw_color); imageline($image, 120, 20, 160, 80, $draw_color); After drawing these lines, you can send the new image back to the browser with imagejpeg, as you see in Example 3, phpline.php. Example 3. Drawing lines, phpline.php<?php $image_height = 100; $image_width = 300; $image = imagecreate($image_width, $image_height); $back_color = imagecolorallocate($image, 200, 200, 200); $draw_color = imagecolorallocate($image, 0, 0, 0); imageline($image, 20, 20, 80, 80, $draw_color); imageline($image, 20, 90, 200, 10, $draw_color); imageline($image, 120, 20, 160, 80, $draw_color); header('Content-Type: image/jpeg'); imagejpeg($image); imagedestroy($image); ?> Here's an HTML document, phpline.html, which will display our JPEG: <HTML> <HEAD> <TITLE> Drawing lines on the server </TITLE> </HEAD> <BODY> <CENTER> <H1> Drawing lines on the server </H1> Here are some lines in an image created on the server: <BR> <IMG src="/books/1/265/1/html/2/phpline.php"> </CENTER> </BODY> </HTML> The new image appears in Figure 3. Not bad. Figure 3. Displaying lines in an HTML page. |