How to draw a line using imageline() function in PHP?

PHPServer Side ProgrammingProgramming

imageline() is an inbuilt function in PHP that is used to draw a line between two given points.

Syntax

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

Parameters

imageline() takes six different parameters: $image, $x1, $y1, $x2, $y2 and $color.

  • $image − Specifies the image resource to work on.

  • $x1 − Specifies the starting x-coordinate.

  • $y1 − Specifies the starting y-coordinate.

  • $x2 − Specifies the ending x-coordinate.

  • $y2 − Specifies the ending y-coordinate.

  • $color − Specifies the line color and a color identifier created using imagecolorallocate() function.

Return Values

imageline() returns True on success or False on failure.

Example 1 − Add a line to an image

<?php
   // Create an image using imagecreatefrompng() function
   $img = imagecreatefrompng('C:\xampp\htdocs\test\515.png');

   // allocated the line color
   $text_color = imagecolorallocate($img, 255, 255, 0);

   // Set the thickness of the line
   imagesetthickness($img, 5);

   // Add a line using imageline() function.
   imageline($img, 80, 300, 1140, 300, $text_color);

   // Output of the image
   header('Content-type: image/png');
   imagepng($img);
   imagedestroy($img);
?>

Output

Example 2

<?php
   // Create an image using imagecreate() function
   $img = imagecreate(700, 300);
   
   // Allocate the colors
   $grey = imagecolorallocate($img, 122, 122, 122);
   $blue = imagecolorallocate($img, 0, 0, 255);

   // Set the thickness of the line
   imagesetthickness($img, 15);

   // Add a grey background color
   imageline($img, 0, 0, 550, 400, $grey);

   // Add a blue line
   imageline($img, 0, 0, 550, 400, $blue);

   // Output the image
   header('Content-type: image/png');
   imagepng($img);
   imagedestroy($img);
?>

Output

raja
Published on 09-Aug-2021 12:36:40
Advertisements