PHP | imageline() Function

The imageline() function is an inbuilt function in PHP which is used to set the draws a line between the two given points.

Syntax:

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

Parameters: This function accepts six parameters as mentioned above and described below:

  • $image: It specifies the image resource to work on.
  • $x1: It specifies the starting x-coordinate.
  • $y1: It specifies the starting y-coordinate.
  • $x2: It specifies the ending x-coordinate.
  • $y2: It specifies the ending y-coordinate.
  • $color: It specifies the line color.

Return Value: This function returns TRUE on success or FALSE on failure.

Below examples illustrate the imageline() function in PHP:



Example 1: In this example add a line to a image.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create an image instance
$im = imagecreatefrompng(
  
// Prepare the line color
$text_color = imagecolorallocate($im, 255, 0, 0);
  
// Add a line
imageline($im, 40, 100, 640, 100, $text_color);
  
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

chevron_right


Output:

Example 2: In this example we will add a line to a drawing.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create an image instance
$im = imagecreate(700, 200);
  
// Initialize the colors
$yellow = imagecolorallocate($im, 255, 255, 0);
$blue = imagecolorallocate($im, 0, 0, 255);
  
// Add a yellow background
imageline($im, 0, 0, 700, 200, $yellow);
  
// Add a blue line
imageline($im, 0, 0, 840, 250, $blue);
  
// Output the image
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>

chevron_right


Output:

Reference: https://www.php.net/manual/en/function.imageline.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.