Current Location: Home> Latest Articles> How to Use imageopenpolygon() and imagecolorallocate() to Set Fill Color for Polygons?

How to Use imageopenpolygon() and imagecolorallocate() to Set Fill Color for Polygons?

M66 2025-06-11

When working with images in PHP, imageopenpolygon() and imagecolorallocate() are two very useful functions commonly used to draw polygons and set their fill colors. This article will explain in detail how to use these two functions to create polygons and add color to them.

1. Introduction to the imageopenpolygon() Function

imageopenpolygon() is used to draw a polygon on an image. The polygon is defined by a set of coordinates, and PHP draws the corresponding shape on the image based on these coordinates.

Function prototype:

bool imageopenpolygon(resource $image, array $points, int $num_points)
  • $image: The image resource, usually created by imagecreatetruecolor() or similar functions.

  • $points: An array containing the coordinates of the polygon’s vertices, formatted as [x1, y1, x2, y2, ..., xn, yn].

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

This function draws the polygon based on the given coordinates, but the polygon will be hollow inside.

2. Introduction to the imagecolorallocate() Function

imagecolorallocate() is used to allocate a color for an image and returns an identifier for that color. Using this identifier, you can specify the fill or border color for the polygon.

Function prototype:

int imagecolorallocate(resource $image, int $red, int $green, int $blue)
  • $image: The image resource.

  • $red: The red component of the color (0-255).

  • $green: The green component of the color (0-255).

  • $blue: The blue component of the color (0-255).

This function returns a color identifier used for drawing or filling colors on the image.

3. Setting Fill Color for Polygons

To set a fill color for a polygon, you need to combine imageopenpolygon() with the imagefill() function. The imagefill() function allows filling color inside a specified point.

First, draw the polygon using imageopenpolygon(). Then, allocate a color identifier using imagecolorallocate() and fill the inside of the polygon with imagefill().

Example code:

<?php
// Create a blank image resource
$image = imagecreatetruecolor(400, 400);
<p>// Allocate colors<br>
$white = imagecolorallocate($image, 255, 255, 255); // Background color<br>
$blue = imagecolorallocate($image, 0, 0, 255); // Polygon fill color</p>
<p>// Set background color<br>
imagefill($image, 0, 0, $white);</p>
<p>// Define polygon vertices<br>
$points = array(<br>
100, 100,<br>
200, 50,<br>
300, 100,<br>
300, 200,<br>
200, 250,<br>
100, 200<br>
);</p>
<p>// Draw polygon with imageopenpolygon<br>
imageopenpolygon($image, $points, count($points) / 2);</p>
<p>// Fill polygon with color<br>
imagefilledpolygon($image, $points, count($points) / 2, $blue);</p>
<p>// Output image to browser<br>
header("Content-Type: image/png");<br>
imagepng($image);</p>
<p>// Free memory<br>
imagedestroy($image);<br>
?><br>

Code explanation:

  1. Creates a 400x400 image resource.

  2. Allocates colors for the background and polygon fill using imagecolorallocate().

  3. Defines an array $points containing the polygon’s vertices.

  4. Draws a hollow polygon using imageopenpolygon().

  5. Fills the polygon with blue using imagefilledpolygon().

  6. Outputs the generated image with imagepng().

4. Conclusion

From the code example above, you can see how to use imageopenpolygon() to draw a polygon and set its fill color with imagecolorallocate(). You can adjust the polygon’s vertices and colors as needed to meet specific requirements.

Hopefully, this article helps you understand how to use PHP’s image functions to draw polygons and set their colors!