Current Location: Home> Latest Articles> Polygon drawing combines GD library to generate verification code images

Polygon drawing combines GD library to generate verification code images

M66 2025-05-29

In PHP, the GD library provides powerful image processing capabilities that can draw various graphics, including polygons. If you want to generate a verification code with a random polygon pattern to improve the anti-crackability of the verification code, the imageopenpolygon function will be very useful.

This article will introduce in detail how to draw polygons using imageopenpolygon and GD library and generate a simple verification code image.

Environmental Requirements

  • PHP 7.0 and above

  • GD extensions have been installed and enabled (you can check whether they are enabled via phpinfo() )

What is imageopenpolygon ?

imageopenpolygon is one of the functions provided by the GD library to draw open polygons on an image (i.e., lines are not connected at the beginning and end). Unlike imagepolygon (drawing closed polygons), imageopenpolygon is more suitable for scenes where open curves are needed, such as interfering lines of verification codes.

The function definition is as follows:

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

  • $points : an array containing point coordinates [x0, y0, x1, y1, x2, y2, ...] .

  • $num_points : The number of vertices.

  • $color : Line color.

Example: Draw polygon verification code

Here is a complete example of how to generate a verification code image with random polygons:

 <?php
// Set content type to picture
header('Content-Type: image/png');

// Create a canvas
$width = 200;
$height = 70;
$image = imagecreatetruecolor($width, $height);

// Color settings
$backgroundColor = imagecolorallocate($image, 255, 255, 255); // White background
$textColor = imagecolorallocate($image, 0, 0, 0);             // Black font
$polygonColor = imagecolorallocate($image, 100, 100, 255);    // Blue polygon

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

// Generate random verification code text
$characters = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
$captchaText = '';
for ($i = 0; $i < 5; $i++) {
    $captchaText .= $characters[rand(0, strlen($characters) - 1)];
}

// Write verification code text
$fontSize = 5; // Built-in font size
$x = ($width - imagefontwidth($fontSize) * strlen($captchaText)) / 2;
$y = ($height - imagefontheight($fontSize)) / 2;
imagestring($image, $fontSize, $x, $y, $captchaText, $textColor);

// Randomly draw multiple open polygons
for ($i = 0; $i < 3; $i++) {
    $points = [];
    $numPoints = rand(3, 6); // Triangle to Hexagon
    for ($j = 0; $j < $numPoints; $j++) {
        $points[] = rand(0, $width);
        $points[] = rand(0, $height);
    }
    imageopenpolygon($image, $points, $numPoints, $polygonColor);
}

// Output image
imagepng($image);

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

Running effect

When running the PHP file above, you will see a verification code with a white background, a set of random alphanumeric characters in the center, and a number of randomly drawn blue open polygons around it, which increases the interference of the image.

Things to note

  1. GD extensions must be enabled . If not enabled, it can be enabled by installing commands such as apt install php-gd or editing php.ini .

  2. Improved verification code complexity : The verification code can be made more complex by increasing the number of polygons, modifying the color and graphic size, and preventing automatic recognition.

  3. Safe output : Do not have any HTML output before generating the image, otherwise the image file will be corrupted.

Expand application

If you want to make the verification code more personalized, such as adding curved text, rotation effects, and background noise, you can use more advanced libraries such as Captcha Builder for PHP for development.