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