PHP | imagepolygon() Function
The imagepolygon() function is an inbuilt function in PHP which is used to draw a polygon. This function Returns TRUE on success and returns FALSE otherwise.
Syntax:
bool imagepolygon( $image, $points, $num_points, $color )
Parameters: This function accepts four parameters as mentioned above and described below:
- $image: The imagecreatetruecolor() function is used to create a blank image in a given size.
- $points: This parameter is used to hold the consecutive vertices of polygon.
- $num_points: This parameter contains total number of vertices in a polgon. It must be greater then 3, because minimum three vertices required to create a polygon.
- $color: This variable contains the filled color identifier. A color identifier created with imagecolorallocate() function.
Return Value: This function returns TRUE on success or FALSE on failure.
Below programs illustrate the imagepolygon() function in PHP.
Program 1:
<?php // Set the vertices of polygon $values = array( 150, 50, // Point 1 (x, y) 50, 250, // Point 2 (x, y) 250, 250 // Point 3 (x, y) ); // Create the size of image or blank image $image = imagecreatetruecolor(300, 300); // Set the background color of image $background_color = imagecolorallocate($image, 0, 153, 0); // Fill background with above selected color imagefill($image, 0, 0, $background_color); // Allocate a color for the polygon $image_color = imagecolorallocate($image, 255, 255, 255); // Draw the polygon imagepolygon($image, $values, 3, $image_color); // Output the picture to the browser header('Content-type: image/png'); imagepng($image); ?> |
Output:

Program 2:
<?php // Set the vertices of polygon $values = array( 150, 50, // Point 1 (x, y) 55, 119, // Point 2 (x, y) 91, 231, // Point 3 (x, y) 209, 231, // Point 4 (x, y) 245, 119 // Point 5 (x, y) ); // Create the size of image or blank image $image = imagecreatetruecolor(300, 300); // Set the background color of image $background_color = imagecolorallocate($image, 0, 153, 0); // Fill background with above selected color imagefill($image, 0, 0, $background_color); // Allocate a color for the polygon $col_poly = imagecolorallocate($image, 255, 255, 255); // Draw the polygon imagepolygon($image, $values, 5, $col_poly); // Output the picture to the browser header('Content-type: image/png'); imagepng($image); ?> |
Output:

Related Articles:
- PHP | imagefilledpolygon() Function
- PHP | imageellipse() Function
- PHP | imagefilledellipse() Function
Reference: http://php.net/manual/en/function.imagepolygon.php
Recommended Posts:
- How to get the function name from within that function using JavaScript ?
- PHP | exp() Function
- PHP | Ds\Map get() Function
- p5.js | str() function
- PHP | ord() Function
- PHP | dir() Function
- D3.js | d3.sum() function
- D3.js | d3.mean() function
- p5.js | tan() function
- p5.js | sin() function
- CSS | rgb() Function
- p5.js | int() function
- CSS | hsl() Function
- p5.js | arc() Function
- p5.js | log() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



