Current Location: Home> Latest Articles> Basic Usage of imagepolygon() Function: How to Quickly Draw Polygons?

Basic Usage of imagepolygon() Function: How to Quickly Draw Polygons?

M66 2025-06-22

In PHP, the imagepolygon() function is used to draw polygons on images. It helps developers create simple geometric shapes like triangles, rectangles, pentagons, and more. This function is based on the GD graphics library, so make sure the GD library is installed and enabled on your server before using it.

Basic Syntax

bool imagepolygon(resource $image, array $points, int $num_points, int $color)
  • $image: The target image resource. Usually created using functions like imagecreatetruecolor() or imagecreatefromjpeg().

  • $points: An array containing all the vertex coordinates of the polygon. Each point in the array is a coordinate pair in the form of [x1, y1, x2, y2, ...].

  • $num_points: The number of vertices of the polygon.

  • $color: The color used to draw the polygon, which can be obtained using the imagecolorallocate() function.

Example Code

<?php
// Create a true color image
$image = imagecreatetruecolor(200, 200);
<p>// Allocate colors<br>
$bg_color = imagecolorallocate($image, 255, 255, 255); // White background<br>
$polygon_color = imagecolorallocate($image, 0, 0, 255); // Blue polygon</p>
<p>// Fill the background color<br>
imagefill($image, 0, 0, $bg_color);</p>
<p>// Define the vertices of the polygon<br>
$points = [<br>
50, 50,<br>
150, 50,<br>
150, 150,<br>
50, 150<br>
];</p>
<p>// Draw the polygon<br>
imagepolygon($image, $points, 4, $polygon_color);</p>
<p>// Output the image to the browser<br>
header('Content-Type: image/png');<br>
imagepng($image);</p>
<p>// Destroy the image resource<br>
imagedestroy($image);<br>
?><br>

Code Explanation

  1. Create Image: Creates a 200x200 true color image using imagecreatetruecolor(200, 200).

  2. Allocate Colors: Uses imagecolorallocate() to create the background color and polygon color, which are white and blue respectively.

  3. Draw Polygon: Defines the vertices of the polygon and passes them to the imagepolygon() function. The $points parameter contains the array of vertex coordinates. Here, a quadrilateral is defined with vertices at (50, 50), (150, 50), (150, 150), and (50, 150).

  4. Output Image: Sets the response header using header('Content-Type: image/png') to indicate that a PNG image is returned, then outputs the image with imagepng().

  5. Destroy Image Resource: Releases the image resource with imagedestroy() to prevent memory leaks.

Drawing Complex Polygons with imagepolygon()

If you want to draw a more complex polygon, such as a hexagon or pentagon, simply adjust the vertex coordinates accordingly. For example, to draw a hexagon, you can set the coordinates like this:

$points = [
    100, 20,
    140, 40,
    140, 80,
    100, 100,
    60, 80,
    60, 40
];

By modifying the $points array, you can easily create polygons of various shapes.

Notes

  • imagepolygon() only draws the outline of the polygon and does not fill it. If you want to fill the polygon with color, you can use imagefill() or imagefilledpolygon().

  • The vertex coordinates are defined based on the image coordinate system, where the origin (0, 0) is at the top-left corner of the image, the x-coordinate increases to the right, and the y-coordinate increases downward.

Advanced Application: Adding Gradient Colors

If you want your polygon to have a gradient effect, you can calculate the gradient color values. For example, you can assign different colors to each vertex of the polygon to create colorful shapes.

Conclusion

The imagepolygon() function is a very practical tool in PHP, suitable for drawing various simple polygon shapes. By adjusting vertex coordinates, you can easily create different patterns. Combined with PHP’s GD graphics library, developers can achieve more advanced image processing functions. To further enhance image effects, you can try using other GD functions such as adding gradients, filling polygons, and drawing circles.