PHP Graphics: Drawing Line, Rectangle, Polygon, Arc, Ellipse, and Patterned Line
PHP provides many functions to draw lines, rectangles, polygons, arcs, ellipses, and much more. The GD library is utilized for dynamic picture creation. In PHP, we can easily use the GD library to make GIF, PNG, or JPG pictures quickly from our code. So there is no need to write HTML and CSS code. We can easily handle this using the PHP programming language.
If you are unsure that you have the GD library, you can run phpinfo() to check that GD support is enabled. On the off chance that you don't have it, you can download it for free.
PHP Draw Lines
PHP provides the imageline() function to draw a line between the two given points.
imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color ) : bool
Parameters
$image- An image resource, this is returned by the imagecreatetruecolor() function.
$x1- x coordinate for the first point.
$y1- y coordinate for the first point.
$x2- x coordinate for the second point.
$y2- y coordinate for the second point.
$color- color identified by the imagecolorallocate().
It returns a boolean value, TRUE on success and FALSE on failure.
Example: imageline()
<?php
$image = ImageCreateTrueColor(270, 150);
imagesavealpha($image, true);
$trans_colour = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $trans_colour);
$x1 = $y1 = 0 ;
$x2 = 270; $y2 = 150;
$color = #4a235a;
ImageLine($image, $x1, $y1, $x2, $y2, $color);
header('Content-type: image/png');
ImagePNG($image);
ImageDestroy($image);
?>
In the above example, the ImageCreateTrueColor() function creates a new true color image. imagesavealpha() function sets a flag to retain full alpha channel information. imagecolorallocatealpha() function allocates a color for an image, i.e., a transparent color, which is filled by the imagefill() function.
Output: ImageLine()
PHP Draw Rectangles
PHP provides the imagefilledrectangle() function to draw a filled rectangle.
imagefilledrectangle($image, $x1, $y1, $x2, $y2, $color)
Parameters
$image- An image resource. This is returned by the imagecreatetruecolor() function.
$x1, $y1- x and y coordinates for point 1.
$x2, $y2- x and y coordinates for point 2.
$color- color identified by imagecolorallocate().
Example
<?php
$x = 180; $y = 110;
$image=imagecreatetruecolor($x, $y);
//set red color
$red = imagecolorallocatealpha($image, 255, 0, 0, 75);
imagefilledrectangle($image, 0, 0, $x, $y, $red);
header('Content-type: image/png');
ImagePNG($image);
ImageDestroy($image);
?>
In the above example, we allocated a color for the rectangle using the imagecolorallocatealpha() function.
Output: Draw Rectangle
PHP Draw Polygons
PHP provides the ImagePolygon() function to draw polygons.
imagepolygon($image, $points, $numpoints, $color)
Parameters
$image- An image resource, this is returned by the imagecreatetruecolor() function.
$points- An array containing polygon vertices.
$numpoints- Total number of points.
$color- color identified by imagecolorallocate()
Example- imagepolygon()
<?php
$x = 250; $y = 210;
$image=imagecreatetruecolor($x, $y);
$white= imagecolorallocatealpha($image, 255, 255, 255, 75);
// Draw the polygon
imagepolygon($image, array(
10, 10,
50, 140,
100, 200,
220, 180
), 4, $white);
header('Content-type: image/png');
ImagePNG($image);
ImageDestroy($image);
?>
Output
PHP Draw Arcs
PHP provides the ImageArc() function for drawing arcs.
ImageArc($image, $x, $y, $width, $height, $start, $end, $color);
Parameters
$image - An image resource, this is returned by the imagecreatetruecolor() function.
$x, $y- To set the x and y coordinates of the center.
$width, $height- To set the width and height of an arc.
$start- To set the arc start angle in degree.
$end- To set the arc end angle in degree.
$color- Color identified by the imagecolorallocate().
Example- ImageArc()
<?php
$x = 200; $y = 200;
$image=imagecreatetruecolor($x, $y);
$white= imagecolorallocatealpha($image, 255, 255, 255, 75);
// Draw an arc
imagearc($image, 100, 100, 150, 150, 25, 155, $white);
header('Content-type: image/png');
ImagePNG($image);
ImageDestroy($image);
?>
Output
PHP Draw Ellipses
PHP provides the ImageEllipse() function to draw an ellipse.
ImageEllipse($image, $x, $y, $width, $height, $color);
Parameters
$image - An image resource, this is returned by the imagecreatetruecolor() function.
$x, $y - To set x and y coordinates of the center.
$width - To set the ellipse width.
$height - To set the ellipse height.
$color - Color identified by the imagecolorallocate().
Example- ImageEllipse()
<?php
$x = 150; $y = 200;
$image=imagecreatetruecolor($x, $y);
$yellow = imagecolorallocatealpha($image, 255, 255, 0, 75);
// Draw an ellipse
imageellipse($image, 70, 100, 100, 150, $yellow);
header('Content-type: image/png');
ImagePNG($image);
ImageDestroy($image);
?>
PHP Draw Patterned Lines
In PHP, we can draw patterned lines with the help of the ImageSetStyle() and the ImageFilledRectangle() functions. The ImageSetStyle() function sets the style for line drawing.
ImageSetStyle($image, $style)
Parameters
$image- An image resource, this is returned by imagecreatetruecolor() function.
$style- This is an array of pixel colors. To pass this, we have used IMG_COLOR_STYLE in ImageFilledRectangle() function.
Example- ImageSetStyle()
<?php
$x = 50; $y = 50;
$black = 0x000000;
$white = 0xFFFFFF;
$image=imagecreatetruecolor($x, $y);
$style = array($white, $white, $white, $white, $white,
$black, $black, $black, $black, $black);
ImageSetStyle($image, $style);
ImageFilledRectangle($image, 0, 0, 60, 60, IMG_COLOR_STYLED);
header('Content-type: image/png');
ImagePNG($image);
ImageDestroy($image);
?>
Output
Related Articles
How to add google reCAPTCHA v2 in registration form using PHPComplete HTML Form Validation in PHP
How to display PDF file in PHP from database
How to read CSV file in PHP and store in MySQL
Create And Download Word Document in PHP
PHP SplFileObject Standard Library
Simple File Upload Script in PHP
Sending form data to an email using PHP
Recover forgot password using PHP and MySQL
Php file based authentication
Simple PHP File Cache
How to get current directory, filename and code line number in PHP
PHP code to generate Captcha and add in contact form with Validation
How to store Emoji character in MySQL using PHP
PHP File Upload MIME Type Validation with Error Handler
File Upload Validation in PHP
Simple File Upload Script in PHP
jquery file upload progress bar
Star rating in PHP
JavaScript display PDF in the browser using Ajax call
jQuery loop over JSON result after AJAX Success
PHP secure password with password_hash() and verify with password_verify()
PHP secure random password generator