PHP imageline() function

In this chapter you will learn:

  1. What is PHP imageline() function
  2. Syntax for PHP imageline() function
  3. Parameter for PHP imageline() function
  4. Return value for PHP imageline() function
  5. Example -
  6. Note for imagecreate() error

Description

Draws a line between the two given points.

Syntax

PHP imageline() function has the following syntax.

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

Parameter

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().

Return

Returns TRUE on success or FALSE on failure.

Example

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


<?php /* j a va 2  s. c  o m*/
  $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 ); 
?> 

Note

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:

;extension=php_gd2.dll

Restart your server after you've made this change.

Next chapter...

What you will learn in the next chapter:

  1. What is PHP imagepolygon() function
  2. Syntax for PHP imagepolygon() function
  3. Parameter for PHP imagepolygon() function
  4. Point Format
  5. Return value for PHP imagepolygon() function
  6. Example - PHP imagepolygon() function
Home » PHP Tutorial » PHP Image Functions
PHP imagearc() function
PHP imagecreate() function
PHP imagecreatetruecolor () function
PHP imageellipse() function
PHP imagefilledarc() function
PHP imageline() function
PHP imagepolygon() function
PHP imagerectangle() function
PHP imagesetpixel() function
PHP imagettftext() function