PHP Tutorial - PHP imageline() function

Draws a line between the two given points.


PHP imageline() function has the following syntax.

bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )


PHP imageline() function has the following parameters.

  • image - An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().
  • x1 - x-coordinate for first point.
  • y1 - y-coordinate for first point.
  • x2 - x-coordinate for second point.
  • y2 - y-coordinate for second point.
  • color - The line color. A color identifier created with imagecolorallocate().


Returns TRUE on success or FALSE on failure.


In this simple example you draw a straight line between two points, then output the resulting image to the browser.

  $myImage = imagecreate( 200, 100 ); 
  $myGray = imagecolorallocate( $myImage, 204, 204, 204 ); 
  $myBlack = imagecolorallocate( $myImage, 0, 0, 0 ); 
  imageline( $myImage, 15, 35, 120, 60, $myBlack ); 
  header( "Content-type: image/png" ); 
  imagepng( $myImage ); 
  imagedestroy( $myImage ); 


If you receive an undefined function imagecreate() error message on Windows, enable the GD2 extension.

Edit your php.ini file and remove the semicolon from the start of the following line:


Restart your server after you've made this change.