In PHP's GD graphics processing library, imageopenpolygon() is a very practical function that allows developers to draw an "open" polygon on an image resource, that is, it does not automatically connect the last point of the polygon to the first point. This is different from the imagepolygon() function (which will enclose the path). Next, we will learn more about the parameters, usage and practical application examples of imageopenpolygon() .
bool imageopenpolygon(
GdImage $image,
array $points,
int $num_points,
int $color
)
Note: imageopenpolygon() requires PHP 8.0.0 and above, and is supported by the GD library.
image
Type: GdImage
Description: Target image resource, usually created by functions such as imagecreatetruecolor() or imagecreate() .
points
Type: array
Description: A one-dimensional array containing vertex coordinates, the array must be arranged in the order of (x, y) pairs. For example [x1, y1, x2, y2, x3, y3, ...] .
num_points
Type: int
Description: Number of vertices, not number of array elements (the number of elements is twice the number of vertices).
color
Type: int
Description: The color used to draw polygon lines. Get it through the imagecolorallocate() function.
Let's create a simple PHP script to draw an open triangle on a picture:
<?php
// Create a 200x200 Blank canvas
$image = imagecreatetruecolor(200, 200);
// Assign colors
$white = imagecolorallocate($image, 255, 255, 255);
$blue = imagecolorallocate($image, 0, 0, 255);
// Filled background with white
imagefill($image, 0, 0, $white);
// Define the three vertices of a triangle
$points = [
50, 50, // vertex1 (x1, y1)
150, 50, // vertex2 (x2, y2)
100, 150 // vertex3 (x3, y3)
];
// Draw an open triangle
imageopenpolygon($image, $points, 3, $blue);
// Output image to browser
header('Content-Type: image/png');
imagepng($image);
// Destroy resources
imagedestroy($image);
?>
When running the above code in the browser, you will see a blue open triangle with its bottom edge not automatically closed.
Imageopenpolygon() is very useful when you are developing an image editor that allows users to manually draw custom graphics, and users want to draw "routes" instead of "closed graphics".
For example, in a map application, a user can draw an unclosed polyline path to represent the route:
<?php
$image = imagecreatetruecolor(600, 400);
$background = imagecolorallocate($image, 240, 240, 240);
$pathColor = imagecolorallocate($image, 255, 0, 0);
imagefill($image, 0, 0, $background);
// Assume that the path drawn by the user
$routePoints = [
50, 100,
200, 80,
300, 150,
450, 120
];
// Draw the opening route
imageopenpolygon($image, $routePoints, 4, $pathColor);
// Save to server
imagepng($image, '/var/www/m66.net/uploads/path_example.png');
// Clean up
imagedestroy($image);
echo "Roadmap saved,View address:https://m66.net/uploads/path_example.png";
?>
In this example, the system will generate a picture with red route and save it in the server directory of m66.net .
The order of points is very important, and the wrong order of points can lead to unexpected paths.
The number of elements of the points array should be num_points * 2 , otherwise a warning may be thrown.
imageopenpolygon() will not automatically connect the head and tail points. If you need to close, please use imagepolygon() .
imageopenpolygon() is suitable for drawing unclosed polygon paths, such as path planning, dynamic trajectory, non-closed patterns and other scenarios. Mastering it can make your PHP graphics processor more flexible and versatile.
If you are developing a web application that needs drawing, don't forget to use it flexibly to take the user experience to the next level!