Create the canvas, allocate colors and pass start/end coordinates to imageline().
<?php
$image = imagecreatetruecolor(600, 300);
$background = imagecolorallocate($image, 255, 255, 255);
$line = imagecolorallocate($image, 30, 100, 190);
imagefilledrectangle($image, 0, 0, 599, 299, $background);
imagesetthickness($image, 3);
imageline($image, 40, 250, 560, 50, $line);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>Current example first; the original tutorial examples below are retained and cleaned up for continued learning.
<?php
header ("Content-type: image/png");
$im = @ImageCreate (100, 200)
or die ("Cannot Initialize new GD image stream");
ImageColorAllocate ($im, 224, 234, 34); // background colour
$text_color = ImageColorAllocate ($im, 233, 14, 91); // Line colour
imageline ($im,30,25,95,175,$text_color); // Add line
ImagePng ($im); // Draw image
imagedestroy($im); //memory cleared.
?>

imageline($image,$x1,$y1,$x2,$y2,$color)
$image | Image created using ImageCreate() or imagecreatetruecolor() |
$x1 | X coordinate of start point ( X : Horizontal ) |
$y1 | Y coordinate of start point ( Y : Vertical) |
$x2 | X coordinate of end point ( X : Horizontal ) |
$y2 | Y coordinate of end point ( Y : Vertical ) |
$color | Color of the line by using imagecolorallocate(). |
$im = @ImageCreate (100, 200)
The above line creates and image identifier of width 100 and height 200. The top left coordinate is set as 0, 0 $background_color = ImageColorAllocate ($im, 234, 234, 234);
Background color is set here for the image with the identifier $im. Here we have set it to a light gray background. The color of the background can be changed by changing the values of red, green, blue ( now all are set to 234 to create light gray background) $text_color = ImageColorAllocate ($im, 233, 14, 91);
imageline ($im,$x1,$y1,$x2,$y2,$text_color);
The above line will show the line from where to where it is to be drawn. Staring from $x1,$x2 to $x2, $y2. Note that top left is at the coordinate 0,0. The lines below will give how the line will appear with different values of the coordinates used in imageline function.
ImagePng ($im);
With the above knowledge, you can see that the command imageline ($im,$x1,$y1,$x2,$y2,$text_color) draws lines between two points defined with two coordinates. To learn the different images we get ( or different lines ) with the change in value of coordinates ( x and y ) we will list a set of values and corresponding image link. Click the link right of the coordinates to check what image will be drawn with the value given at left. All images will be opened in a new window.
| imageline ($im,0,0,50,100,$text_color) | Display |
| imageline ($im,0,0,100,100,$text_color) | Display |
| imageline ($im,100,0,0,200,$text_color) | Display |
| imageline ($im,0,0,100,200,$text_color) | Display |
| imageline ($im,0,0,100,200,$text_color); imageline ($im,100,0,0,200,$text_color); | Display |
<?php
header ("Content-type: image/png");
$x1=$_GET['x1'];
$x2=$_GET['x2'];
$y1=$_GET['y1'];
$y2=$_GET['y2'];
$im = @ImageCreate (100, 200)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 224, 234, 234);
$text_color = ImageColorAllocate ($im, 233, 14, 91);
// imageline ($im,$x1,$y1,$x2,$y2,$text_color);
imageline ($im,0,0,100,200,$text_color);
imageline ($im,100,0,0,200,$text_color);
ImagePng ($im);
?>
Save the above code in a file and open it without adding any html code or data to it. With this learning on how to draw lines we will move to our next learning.
$i=600 can't be drawn at the edge of the image so we kept width and height as 601 and 401. You can experiment this by changing width height to 600 and 400.

<?php
header ("Content-type: image/jpeg");
$im = @ImageCreate (601, 401) // added one pixel for border
or die ("Cannot Initialize new GD image stream");
$gap=20; // change this value to increase the lines
$background_color = ImageColorAllocate ($im, 255, 255, 255);
$text_color = ImageColorAllocate ($im, 128, 255, 136);
//imagesetthickness($im, 5);
for($i=0;$i<=600;$i +=$gap){
imageline ($im,$i,0,$i,400,$text_color);
}
for($i=0;$i<=400;$i +=$gap){
imageline ($im,0,$i,600,$i,$text_color);
}
Imagejpeg ($im);
imagedestroy($im); //memory is removed.
?>
We can use different color for horizontal lines 
header ("Content-type: image/jpeg");
$im = @ImageCreate (601, 401) // added one pixel for border
or die ("Cannot Initialize new GD image stream");
$size=20; // change this value to increase the lines
$background_color = ImageColorAllocate ($im, 255, 255, 255);
$text_color = ImageColorAllocate ($im, 128, 255, 136);
//imagesetthickness($im, 5);
// vertical lines
for($i=0;$i<=600;$i +=$size){
imageline ($im,$i,0,$i,400,$text_color);
}
// Horizontal lines
$text_color = ImageColorAllocate ($im, 255, 128, 128);
for($i=0;$i<=400;$i +=$size){
imageline ($im,0,$i,600,$i,$text_color);
}
Imagejpeg ($im);
imagedestroy($im); //memory is removed.
By using random number generator rand(min,max) we can create all lines in different colours. 
header ("Content-type: image/jpeg");
$im = @ImageCreate (601, 401) // added one pixel for border
or die ("Cannot Initialize new GD image stream");
$size=20; // change this value to increase the lines
$background_color = ImageColorAllocate ($im, 255, 255, 255);
for($i=0;$i<=600;$i +=$size){
$r=rand(1,255);
$g=rand(1,255);
$b=rand(1,255);
$text_color = ImageColorAllocate ($im, $r, $g,$b);
imageline ($im,$i,0,$i,400,$text_color);
}
// Horizontal lines
for($i=0;$i<=400;$i +=$size){
$r=rand(1,255);
$g=rand(1,255);
$b=rand(1,255);
$text_color = ImageColorAllocate ($im, $r, $g,$b);
imageline ($im,0,$i,600,$i,$text_color);
}
Imagejpeg ($im);
imagedestroy($im); //memory is removed.

imagesetthickness($im, 5);
.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.