etutorialspoint
  • Home
  • PHP
  • MySQL
  • MongoDB
  • HTML
  • Javascript
  • Node.js
  • Express.js
  • Python
  • Jquery
  • R
  • Kotlin
  • DS
  • Blogs
  • Theory of Computation

PHP Graphics: Drawing Line, Rectangle, Polygon, Arc, Ellipse, 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.





Drawing 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 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 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., transparent color, which is filled by imagefill() function.

Output: ImageLine()

PHP Graphics



Drawing Rectangles

PHP provides 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 allocate a color for the rectangle using imagecolorallocatealpha() function.

Output: Draw Rectangle

PHP Graphics draw rectangle




Drawing Polygons

PHP provides ImagePolygon() function to draw polygon.

imagepolygon($image, $points, $numpoints, $color)

Parameters

$image - An image resource, this is returned by 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 Graphics draw polygons

Drawing Arcs

PHP provides ImageArc() function for drawing arcs.

ImageArc($image, $x, $y, $width, $height, $start, $end, $color);

Parameters

$image - An image resource, this is returned by 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 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 Graphics draw polygons



Drawing Ellipses

PHP provides ImageEllipse() function to draw an ellipse.

ImageEllipse($image, $x, $y, $width, $height, $color);

Parameters

$image - An image resource, this is returned by 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 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 Graphics ellipse polygons

Drawing Patterned Lines

In PHP, we can draw patterned lines with the help of ImageSetStyle() and 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


PHP Graphics ellipse polygons



Related Articles

How to add google reCAPTCHA v2 in registration form using PHP
Complete 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




◀ Previous Article
PHP secure password with password_hash() and verify with password_verify()
Next Article ▶
PHP secure random password generator
Most Popular Development Resources
Retrieve Data From Database Without Page refresh Using AJAX, PHP and Javascript
-----------------
PHP Create Word Document from HTML
-----------------
How to get data from XML file in PHP
-----------------
PHP code to send email using SMTP
-----------------
Hypertext Transfer Protocol Overview
-----------------
Characteristics of a Good Computer Program
-----------------
How to encrypt password in PHP
-----------------
Create Dynamic Pie Chart using Google API, PHP and MySQL
-----------------
PHP MySQL PDO Database Connection and CRUD Operations
-----------------
Splitting MySQL Results Into Two Columns Using PHP
-----------------
Dynamically Add/Delete HTML Table Rows Using Javascript
-----------------
How to get current directory, filename and code line number in PHP
-----------------
How to add multiple custom markers on google map
-----------------
Get current visitor\'s location using HTML5 Geolocation API and PHP
-----------------
Simple star rating system using PHP, jQuery and Ajax
-----------------
How to Sort Table Data in PHP and MySQL
-----------------
Fibonacci Series Program in PHP
-----------------
Simple pagination in PHP with MySQL
-----------------
How to generate QR Code in PHP
-----------------
PHP MYSQL Advanced Search Feature
-----------------
jQuery loop over JSON result after AJAX Success
-----------------
Submit a form data using PHP, AJAX and Javascript
-----------------
Recover forgot password using PHP7 and MySQLi
-----------------
PHP Server Side Form Validation
-----------------
jQuery File upload progress bar with file size validation
-----------------
PHP user registration and login/ logout with secure password encryption
-----------------
To check whether a year is a leap year or not in php
-----------------
Php file based authentication
-----------------
Simple File Upload Script in PHP
-----------------
PHP User Authentication by IP Address
-----------------
Simple PHP File Cache
-----------------
Calculate the distance between two locations using PHP
-----------------
PHP Secure User Registration with Login/logout
-----------------
Polling system using PHP, Ajax and MySql
-----------------
How to print specific part of a web page in javascript
-----------------
Detect Mobile Devices in PHP
-----------------
Simple Show Hide Menu Navigation
-----------------
Simple way to send SMTP mail using Node.js
-----------------
SQL Injection Prevention Techniques
-----------------
Get Visitor\'s location and TimeZone
-----------------
Preventing Cross Site Request Forgeries(CSRF) in PHP
-----------------
Google Street View API Example
-----------------
PHP Sending HTML form data to an Email
-----------------
CSS Simple Menu Navigation Bar
-----------------
Date Timestamp Formats in PHP
-----------------
Driving route directions from source to destination using HTML5 and Javascript
-----------------
Convert MySQL to JSON using PHP
-----------------
PHP Programming Error Types
-----------------
Set and Get Cookies in PHP
-----------------
How to add google map on your website and display address on click marker
-----------------
How to select/deselect all checkboxes using Javascript
-----------------
PHP Getting Document of Remote Address
-----------------
File Upload Validation in PHP
-----------------
How to display PDF file in web page from Database in PHP
-----------------
PHP FTP Connection and File Handling
-----------------


Most Popular Blogs
Most in demand programming languages
Best mvc PHP frameworks in 2019
MariaDB vs MySQL
Most in demand NoSQL databases for 2019
Best AI Startups In India
Kotlin : Android App Development Choice
Kotlin vs Java which one is better
Top Android App Development Languages in 2019
Web Robots
Data Science Recruitment of Freshers - 2019


Interview Questions Answers
Basic PHP Interview
Advanced PHP Interview
MySQL Interview
Javascript Interview
HTML Interview
CSS Interview
Programming C Interview
Programming C++ Interview
Java Interview
Computer Networking Interview
NodeJS Interview
ExpressJS Interview
R Interview


Popular Tutorials
PHP Tutorial (Basic & Advance)
MySQL Tutorial & Exercise
MongoDB Tutorial
Python Tutorial & Exercise
Kotlin Tutorial & Exercise
R Programming Tutorial
HTML Tutorial
jQuery Tutorial
NodeJS Tutorial
ExpressJS Tutorial
Theory of Computation Tutorial
Data Structure Tutorial
Javascript Tutorial




General Knowledge

listen
listen
listen
listen
listen
listen
listen
listen
listen


Learn Popular Language

listen
listen
listen
listen
listen

Blogs

  • Jan 3

    Stateful vs Stateless

    A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities...

  • Dec 29

    Best programming language to learn in 2021

    In this article, we have mentioned the analyzed results of the best programming language for 2021...

  • Dec 20

    How is Python best for mobile app development?

    Python has a set of useful Libraries and Packages that minimize the use of code...

  • July 18

    Learn all about Emoji

    In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...

  • Jan 10

    Data Science Recruitment of Freshers

    In this article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...

Follow us

  • etutorialspoint facebook
  • etutorialspoint twitter
  • etutorialspoint linkedin
etutorialspoint youtube
About Us      Contact Us


  • eTutorialsPoint©Copyright 2016-2022. All Rights Reserved.