Current Location: Home> Latest Articles> Use header() to return the image (such as CAPTCHA verification code)

Use header() to return the image (such as CAPTCHA verification code)

M66 2025-05-28

In PHP, if you want to display images through the browser or generate dynamic images (such as verification code), you need to use the header() function to set the response header to tell the browser that it is returning an image type, rather than a normal text or HTML page.

In this article, we will introduce how to use PHP's header() function to generate and return verification code images. Verification codes are widely used in website registration, login and other scenarios, and are used to prevent robots from submitting forms automatically.

1. Basic steps to generate verification code pictures

Generating verification code pictures usually requires the following steps:

  1. Generate random verification code characters : The content of the verification code can be a number, letter or a combination of numbers and letters.

  2. Create an image resource : Create a blank image using PHP's imagecreate() or imagecreatetruecolor() function.

  3. Draw text : Draw verification code characters on images, usually using imagettftext() or imagestring() functions.

  4. Set the response header : Use the header() function to inform the browser of the image type.

  5. Output image : output the image to the browser through imagepng() , imagejpeg() and other functions.

  6. Destroy image resources : Use the imagedestroy() function to release image resources.

2. Code example

Here is a code example that creates and returns a verification code image using PHP:

 <?php
// Set the response header,Tell the browser that it is returning an image
header("Content-Type: image/png");

// Create a blank image,Width is 120px,Height is 40px
$width = 120;
$height = 40;
$image = imagecreate($width, $height);

// Set background color and text color
$background_color = imagecolorallocate($image, 255, 255, 255); // White background
$text_color = imagecolorallocate($image, 0, 0, 0); // Black text

// Generate random verification code characters
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$captcha_text = substr(str_shuffle($characters), 0, 5);

// Draw verification code text on the picture
imagestring($image, 5, 30, 10, $captcha_text, $text_color);

// Output picture
imagepng($image);

// Destroy image resources,Free memory
imagedestroy($image);
?>

3. Code parsing

  1. Set the response header : header("Content-Type: image/png"); This line sets the response header to tell the browser that the content returned is an image in PNG format. You can change to other image formats according to your needs, such as JPEG ( image/jpeg ).

  2. Create an image resource : imagecreate($width, $height); Creates a blank image with a specified width and height, returning an image resource.

  3. Set colors : imagecolorallocate($image, 255, 255, 255); and imagecolorallocate($image, 0, 0); used to define the background color (white) and text color (black) of the image.

  4. Generate a random verification code : substr(str_shuffle($characters), 0, 5); This line of code will randomly select 5 characters from a character set as verification code.

  5. Draw text : imagestring($image, 5, 30, 10, $captcha_text, $text_color); Responsible for drawing the generated verification code text onto the image.

  6. Output image : imagepng($image); output the image to the browser, and the browser will automatically display it as an image.

  7. Destroy image resources : imagedestroy($image); is used to free image resources to avoid memory leakage.

4. Complete code and URL replacement

If you need to reference external resources such as fonts, pictures, etc. when generating verification codes, the URL address may be involved. In this case, we can replace the domain name of the URL with m66.net , for example: