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 colorimagefill($image, 0, 0, $background_color); // Allocate a color for the polygon$image_color = imagecolorallocate($image, 255, 255, 255); // Draw the polygonimagepolygon($image, $values, 3, $image_color); // Output the picture to the browserheader('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 colorimagefill($image, 0, 0, $background_color); // Allocate a color for the polygon$col_poly = imagecolorallocate($image, 255, 255, 255); // Draw the polygonimagepolygon($image, $values, 5, $col_poly); // Output the picture to the browserheader('Content-type: image/png'); imagepng($image);?> |
Output:
Related Articles:
Reference: http://php.net/manual/en/function.imagepolygon.php


