Current Location: Home> Latest Articles> Use imageopenpolygon() to draw complex graphics such as stars and snowflakes

Use imageopenpolygon() to draw complex graphics such as stars and snowflakes

M66 2025-06-05

In PHP, imageopenpolygon() is a very interesting function that allows you to draw open (not closed) polygons through a set of coordinate points. This is especially useful when you need to draw complex graphics, such as star or snowflake patterns.

This article will explain through examples how to use imageopenpolygon() and show how to draw a simple five-pointed star and a basic snowflake structure.

Basic understanding: imageopenpolygon() function

 bool imageopenpolygon(
    GdImage $image,
    array $points,
    int $num_points,
    int $color
)
  • $image : The image resource to be drawn.

  • $points : an array containing all vertex coordinates in the form [x1, y1, x2, y2, ...] .

  • $num_points : The number of vertices.

  • $color : The color of the line.

Note: imageopenpolygon() connects these points in order, but does not automatically close the figure.

Example 1: Draw a five-pointed star

First, we create a canvas and draw a simple five-pointed star:

 <?php
// Create a canvas
$image = imagecreatetruecolor(300, 300);

// Assign colors
$backgroundColor = imagecolorallocate($image, 255, 255, 255); // White background
$starColor = imagecolorallocate($image, 255, 0, 0); // Red star

// Fill the background
imagefill($image, 0, 0, $backgroundColor);

// Define the vertex of the five-pointed star
$points = [
    150, 20,   // top
    179, 110,
    270, 110,
    197, 165,
    220, 250,
    150, 200,
    80, 250,
    103, 165,
    30, 110,
    121, 110
];

// Draw open pentagrams
imageopenpolygon($image, $points, count($points) / 2, $starColor);

// Output image
header('Content-Type: image/png');
imagepng($image);

// Destroy image resources
imagedestroy($image);
?>

After saving as star.php , access it in the browser, such as:

 https://m66.net/star.php

You can see the drawn five-pointed star.

Example 2: Draw a basic snowflake pattern

A simple snowflake can be simulated by centrally symmetrical radiation lines:

 <?php
$image = imagecreatetruecolor(300, 300);
$backgroundColor = imagecolorallocate($image, 255, 255, 255);
$snowColor = imagecolorallocate($image, 0, 0, 255);

imagefill($image, 0, 0, $backgroundColor);

// Snowflake Center
$centerX = 150;
$centerY = 150;
$length = 100;
$arms = 6;

// Calculate each point
$points = [];
for ($i = 0; $i < $arms; $i++) {
    $angle = deg2rad(360 / $arms * $i);
    $x = $centerX + cos($angle) * $length;
    $y = $centerY + sin($angle) * $length;
    $points[] = $centerX;
    $points[] = $centerY;
    $points[] = $x;
    $points[] = $y;
}

// Draw snowflakes
imageopenpolygon($image, $points, count($points) / 2, $snowColor);

header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>

After saving as snowflake.php , access in the browser:

 https://m66.net/snowflake.php

You can see a radial basic snowflake structure!

summary

imageopenpolygon() is a powerful tool for drawing open-type graphics. Combined with mathematical calculations (such as trigonometric functions), you can create various interesting and complex graphics, such as stars, snowflakes, and even more complex artistic patterns.

If you want to generate cooler complex graphics, you can consider combining loop logic and coordinate algorithms to further expand this drawing capability!