Current Location: Home> Latest Articles> Image generation verification code: GD2 image dynamically add text

Image generation verification code: GD2 image dynamically add text

M66 2025-05-29

In web development, verification code images are often used to prevent automatic submission of forms. PHP provides rich image processing functions to generate verification code images. Among them, imagecreatefromgd2() is a function used to create image resources from a GD2 file, but it is usually used to read existing image resources rather than directly generating images. However, we can combine the imagegd2() function to save the dynamically generated image into GD2 format, and then read and process it through imagecreatefromgd2() .

This article will demonstrate how to dynamically generate a verification code image with random text and use imagecreatefromgd2() to process the image.

Steps Overview

  1. Create an image resource

  2. Add background color and random verification code text

  3. Save as .gd2 format

  4. Read the .gd2 file and output it as the final image

Sample code

 <?php
// Step 1: Dynamically create verification code images
$width = 150;
$height = 50;
$image = imagecreatetruecolor($width, $height);

// Set background colors
$bgColor = imagecolorallocate($image, 255, 255, 255); // White
imagefill($image, 0, 0, $bgColor);

// Set text color
$textColor = imagecolorallocate($image, 0, 0, 0); // black

// Generate random verification code content
$captcha = '';
$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
for ($i = 0; $i < 6; $i++) {
    $captcha .= $chars[rand(0, strlen($chars) - 1)];
}

// Add text to image
$fontSize = 5; // 1 arrive 5 Font size
$x = 10;
$y = ($height - imagefontheight($fontSize)) / 2;
imagestring($image, $fontSize, $x, $y, $captcha, $textColor);

// Step 2: Save the image as .gd2 document
$gd2Path = 'captcha.gd2';
imagegd2($image, $gd2Path);

// Destroy original image resources
imagedestroy($image);

// Step 3: use imagecreatefromgd2 Read the image
$gd2Image = imagecreatefromgd2($gd2Path);

// set up HTTP Header output image
header('Content-Type: image/png');
imagepng($gd2Image);

// Clean up
imagedestroy($gd2Image);
?>

Instructions for use

  • Please make sure that the GD library is enabled during PHP installation (usually enabled by default).

  • The file save path captcha.gd2 should have write permissions.

  • You can save the verification code content in $_SESSION for subsequent verification.

Sample application scenarios

You can embed this dynamically generated verification code image in the form:

 <form method="post" action="https://m66.net/verify.php">
    <img src="https://m66.net/captcha.php" alt="Verification code">
    <input type="text" name="captcha" placeholder="请输入Verification code">
    <input type="submit" value="submit">
</form>

summary

Although imagecreatefromgd2() is not directly used to create images, by first using imagegd2() to generate a GD2 file, then reading and outputting, the purpose of dynamically generating verification code images can be achieved. This approach is suitable for scenes where intermediate image caches or image templates are required.

If you need more complex verification code images (distortions, interference lines, font changes, etc.), you can use the imagettftext() function to combine TTF fonts to achieve more advanced functions.

Need me to demonstrate a verification code version with TTF font?