In the field of image processing, drawing graphics using PHP's GD library is a common skill. Among them, the imageopenpolygon function (the correct function name should be imagepolygon ) can be used to draw polygons of any number of edges. This article will introduce in detail how to dynamically generate coordinate arrays and use them to draw regular N-sides.
imagepolygon() is a function used in PHP GD library to draw polygons.
Its basic usage is as follows:
bool imagepolygon(
GdImage $image,
array $points,
int $num_points,
int $color
)
$image is an image resource;
$points is a point array, format such as [x1, y1, x2, y2, ..., xn, yn] ;
$num_points is the number of points (not the number of array elements, divided by 2);
$color is the color drawn.
To draw polygons of any number of edges (such as pentagons, octagons, etc.), we can use a simple trigonometric function to generate coordinates. The idea is as follows:
Set the center point of the polygon (cx, cy) ;
Set the radius r (i.e. the distance from the center to the vertex);
Distribute each vertex evenly at 360°;
The coordinates of each vertex can be calculated by sine ( sin ) and cosine ( cos ).
The formula is:
x = cx + r * cos(angle)
y = cy + r * sin(angle)
Note that the angles received by PHP cos() and sin() are radians, and it needs to be converted using deg2rad() .
Here is a complete example showing how to dynamically generate coordinates and draw a polygon of any number of edges:
<?php
// Set picture size
$width = 400;
$height = 400;
// Create a canvas
$image = imagecreatetruecolor($width, $height);
// Assign colors
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
// Fill the background
imagefill($image, 0, 0, $white);
// Polygon parameters
$number_of_sides = 7; // For example,Draw heptagons
$radius = 150; // radius
$centerX = $width / 2;
$centerY = $height / 2;
// Generate coordinate points
$points = [];
for ($i = 0; $i < $number_of_sides; $i++) {
$angle_deg = (360 / $number_of_sides) * $i - 90; // Start drawing from the top
$angle_rad = deg2rad($angle_deg);
$x = $centerX + $radius * cos($angle_rad);
$y = $centerY + $radius * sin($angle_rad);
$points[] = (int)$x;
$points[] = (int)$y;
}
// Draw polygons
imagepolygon($image, $points, $number_of_sides, $black);
// Output pictures to browser
header('Content-Type: image/png');
imagepng($image);
// Destroy resources
imagedestroy($image);
?>
Save the above code as a PHP file (such as polygon.php ), and access it through the browser, you can see a regular heptagon.
By adjusting the $number_of_sides variable, you can draw polygons with different numbers of sides;
$radius determines the size of the polygon;
The offset of the angle (here is -90 degrees) makes the first vertex at the top, making the effect more beautiful;
If you need to save the image to the server, you can use imagepng($image, 'path/to/file.png') .
For example, save the image to m66.net/uploads/polygon.png :
imagepng($image, '/var/www/m66.net/uploads/polygon.png');
Be careful to ensure that the save directory has write permission!
Using PHP's imagepolygon function combined with simple trigonometric operation, we can easily draw regular polygons of any number of edges. This is of great use when making graphic generators, verification codes, and chart components. In the future, if you want to draw a polygon with color fill, you can also combine the imagefilledpolygon() function to implement it.