In PHP, imageopenpolygon() is a function used to draw open polygons (i.e. polygons that do not automatically connect the starting point and end point line segments). It is very useful when dealing with graphic drawing, especially custom line shapes. However, there are indeed some differences in the support and performance of this function in different versions of PHP. Let's take a closer look below.
The imageopenpolygon() function was introduced in PHP version 7.2.0 . This function was not available in earlier versions (such as PHP 5.x or PHP 7.0/7.1), so if used in older versions, it will directly lead to fatal errors :
// PHP 7.1 Execute earlier version
$im = imagecreatetruecolor(200, 200);
$points = [50, 50, 150, 50, 150, 150, 50, 150];
// This will result in a fatal error,Because the function does not exist
imageopenpolygon($im, $points, 4, imagecolorallocate($im, 255, 0, 0));
In PHP 7.2 and later, the above code is able to execute normally and draw an open quad.
The basic parameter definition of imageopenpolygon() has been fixed since PHP 7.2, and the subsequent version has not changed. Its parameters are:
bool imageopenpolygon(
GdImage $image,
array $points,
int $num_points,
int $color
)
$image : An image resource created by imagecreatetruecolor() or similar function.
$points : A one-dimensional array containing a series of X and Y coordinates.
$num_points : Number of vertices (rather than number of array elements).
$color : Line color, created by imagecolorallocate() .
After PHP 8.0, with the strengthening of the overall type system, the resource type resource (resource) was officially converted into a GdImage object . So in PHP 8.0+, it is strictly required that $image must be a GdImage instance, otherwise TypeError will be triggered.
Example (PHP 8.0+):
$im = imagecreatetruecolor(200, 200);
$red = imagecolorallocate($im, 255, 0, 0);
$points = [50, 50, 150, 50, 150, 150, 50, 150];
imageopenpolygon($im, $points, 4, $red);
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
In all supported versions (PHP 7.2+), imageopenpolygon() returns a boolean:
Success : Return true
Failed : Return false
Note that after PHP 8.0, if an invalid parameter is passed (such as non- GdImage type), a TypeError exception will be directly thrown without returning false .
PHP 7.2 - 7.4 : Pass in the error parameter type, usually returns false , and triggers an E_WARNING.
PHP 8.0+ : The error parameter type directly throws a TypeError exception, no longer just a warning.
Example (Error handling demonstration):
try {
$invalidImage = 'not a resource';
$points = [10, 10, 20, 20, 30, 10];
imageopenpolygon($invalidImage, $points, 3, 0xFF0000);
} catch (TypeError $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
To give a complete example, use imageopenpolygon() to draw a simple open polygon and output the image to the browser:
<?php
$im = imagecreatetruecolor(300, 300);
$background = imagecolorallocate($im, 255, 255, 255); // White background
$lineColor = imagecolorallocate($im, 0, 0, 255); // Blue lines
$points = [
50, 50,
250, 50,
250, 250,
50, 250
];
imagefill($im, 0, 0, $background);
imageopenpolygon($im, $points, 4, $lineColor);
// Output to browser
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
The browser accesses this PHP file, for example:
https://m66.net/draw_polygon.php
You can see an open rectangular box drawn with blue lines.
PHP Version | Support | Remark |
---|---|---|
< 7.2 | Not supported, the call is directly error | |
7.2 - 7.4 | Supported, loose parameter types | The resource is resource type |
8.0+ | Supported, strict parameter requirements | GdImage type, error throws exception directly |
Therefore, in development, if you need to be compatible with PHP 7.1 and below, you should avoid using imageopenpolygon() , or do version checks in your code. In the PHP 8.0+ environment, attention should be paid to type strictness and exception handling issues.