1. Home
  2. Computing & Technology
  3. PHP / MySQL

GD Library - The Basics of Drawing with PHP

By Angela Bradley, About.com

4 of 7

Drawing Lines

Image created with GD Library

Our code produces this graphic

<?php header ("Content-type: image/png");
$handle = ImageCreate (130, 50) or die ("Cannot Create image");
$bg_color = ImageColorAllocate ($handle, 255, 0, 0);
$txt_color = ImageColorAllocate ($handle, 255, 255, 255);
$line_color = ImageColorAllocate ($handle, 0, 0, 0);
ImageLine($handle, 65, 0, 130, 50, $line_color);
ImageString ($handle, 5, 5, 18, "PHP.About.com", $txt_color);
ImagePng ($handle);
?>
In this code, we use ImageLine () to to draw a line. The first parameter is our handle, followed by our starting X and Y, our ending X and Y, and finally our color.

To make a cool volcano like we have in our example, we simply put this into a loop, keeping our starting coordinates the same, but moving along the x axis with our finishing coordinates.

<?php
header ("Content-type: image/png");
$handle = ImageCreate (130, 50) or die ("Cannot Create image");
$bg_color = ImageColorAllocate ($handle, 255, 0, 0);
$txt_color = ImageColorAllocate ($handle, 255, 255, 255);
$line_color = ImageColorAllocate ($handle, 0, 0, 0);
for($i=0;$i<=129;$i=$i+5)
{
ImageLine($handle, 65, 0, $i, 50, $line_color);
}
ImageString ($handle, 5, 5, 18, "PHP.About.com", $txt_color);
ImagePng ($handle);
?>
Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Graphics- GD Library
  5. GD Library - PHP GD Library - GD Image Library

©2009 About.com, a part of The New York Times Company.

All rights reserved.