Monday, December 22, 2025
HomeLanguagesPHP | imageline() Function

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.




<?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);
?>


Output:

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




<?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);
?>


Output:

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

RELATED ARTICLES

Most Popular

Dominic
32456 POSTS0 COMMENTS
Milvus
111 POSTS0 COMMENTS
Nango Kala
6823 POSTS0 COMMENTS
Nicole Veronica
11958 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12038 POSTS0 COMMENTS
Shaida Kate Naidoo
6958 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6911 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS