PHP | imagepolygon() Function (original) (raw)
Last Updated : 11 Jul, 2025
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,image, image,points, numpoints,num_points, numpoints,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 polygon. It must be greater than 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 `
backgroundcolor=imagecolorallocate(background_color = imagecolorallocate(backgroundcolor=imagecolorallocate(image, 0, 153, 0); // Fill background with above selected color imagefill($image, 0, 0, $background_color); // Allocate a color for the polygon imagecolor=imagecolorallocate(image_color = imagecolorallocate(imagecolor=imagecolorallocate(image, 255, 255, 255); // Draw the polygon imagepolygon($image, values,3,values, 3, values,3,image_color); // Output the picture to the browser header('Content-type: image/png'); imagepng($image); ?>`
Output:

Program 2:
php `
backgroundcolor=imagecolorallocate(background_color = imagecolorallocate(backgroundcolor=imagecolorallocate(image, 0, 153, 0); // Fill background with above selected color imagefill($image, 0, 0, $background_color); // Allocate a color for the polygon colpoly=imagecolorallocate(col_poly = imagecolorallocate(colpoly=imagecolorallocate(image, 255, 255, 255); // Draw the polygon imagepolygon($image, values,5,values, 5, 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: https://www.php.net/manual/en/function.imagepolygon.php