PHP image operation-generating image verification code and php image Verification Code

Source: Internet
Author: User

PHP image operation-generating image verification code and php image Verification Code

A simple verification code is actually a few characters output in the image, which can be achieved through the imagestring function.

However, in terms of processing, in order to make the verification code more secure and Prevent Automatic Identification by other programs, it is often necessary to interfere with the verification code. Generally, noise and line segments are drawn, skew and distortion of output characters.

You can use imagesetpixel to draw a point to Implement noise interference, but it is not helpful to draw only one point. Therefore, here we often use loops to draw randomly.


Example:

<? Php
$ Img = imagecreatetruecolor (100, 40 );
$ Black = imagecolorallocate ($ img, 0x00, 0x00, 0x00 );
$ Green = imagecolorallocate ($ img, 0x00, 0xFF, 0x00 );
$ White = imagecolorallocate ($ img, 0xFF, 0xFF, 0xFF );
Imagefill ($ img, 0, 0, $ white );
// Generate a random Verification Code
$ Code = '';
For ($ I = 0; $ I <4; $ I ++) {// 4-digit Verification Code
$ Code. = rand (0, 9 );
}
Imagestring ($ img, 5, 10, 10, $ code, $ black );
// Add noise interference
For ($ I = 0; $ I <50; $ I ++ ){
Imagesetpixel ($ img, rand (0,100), rand (0,100), $ black); // imagesetpixel-draw a single pixel. Syntax: bool imagesetpixel (resource $ image, int $ x, int $ y, int $ color)
Imagesetpixel ($ img, rand (0,100), rand (0,100), $ green );
}
// Output the verification code
Header ("content-type: image/png ");
Imagepng ($ img); // output the image to a browser or file in PNG format
Imagedestroy ($ img); // after image processing is complete, use the imagedestroy () command to destroy image resources to release memory. Although this function is not necessary, it is a good habit to use it.

?>




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.