PHP generates picture verification code

Source: Internet
Author: User

At first, there was a php.ini to see if the GD library was turned on,
Next, we will consider each detail of the image generation step.
<?php
Send a generated image to the browser
Create_image ();
Exit ();

function Create_image ()
{
Let ' s generate a totally random string using MD5
$MD 5 = MD5 (rand (0,999));
We don ' t need a character long string so we trim it down to 5
$pass = substr ($md 5, 10, 5);

Set the image width and height
$width = 100;
$height = 20;

Create the image Resource
$image = Imagecreate ($width, $height);

We are making three colors, white, black and gray
$white = Imagecolorallocate ($image, 255, 255, 255);
$black = imagecolorallocate ($image, 0, 0, 0);
$grey = Imagecolorallocate ($image, 204, 204, 204);

Make the background black
Imagefill ($image, 0, 0, $black);

ADD randomly generated string in white to the image
Imagestring ($image, 3, 3, $pass, $white);

Throw in some lines to make it a little bit harder for all bots to

Break
Imagerectangle ($image, 0,0, $width-1, $height-1, $grey);
Imageline ($image, 0, $height/2, $width, $height/2, $grey);
Imageline ($image, $width/2, 0, $width/2, $height, $grey);

Tell the browser what kind of the ' file is ' come in
Header ("Content-type:image/jpeg");

Output the newly created image in JPEG format
Imagejpeg ($image);

Free up Resources
Imagedestroy ($image);
}
?>

Authentication Code Invocation method

Src= "generate_image.php" width= ">"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.