
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
How to draw a line using imageline() function in PHP?
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
- Related Articles
- How to draw a string vertically using imagestringup() function in PHP?
- How to draw an ellipse using imageellipse() function in PHP?
- How to draw a filled polygon using an imagefilledpolygon() function in PHP?
- How to draw a partial arc and fill it using imagefilledarc() function in PHP?
- How to draw a text string image horizontally by using imagestring() function in PHP?
- How to draw a line in OpenCV using Java?
- How to draw a line in Android using Kotlin?
- How to draw a line in OpenCV using C++?
- How to draw an open polygon using the imageopenpolygon() function n PHP?
- How to set the style for line drawing using imagesetstyle() function in PHP?
- How to set the image thickness for line drawing using imgesetthickness() function in PHP?
- How to draw a dotted line with Polyline using FabricJS?
- How to draw rounded line ends using Matplotlib?
- How to draw a line in Android?
- How to draw an arrowed line in OpenCV using Java?

Advertisements