Use php to generate a verification code, and use php to generate a verification code

Source: Internet
Author: User

Use php to generate a verification code, and use php to generate a verification code

<? Php/*** php verification code generation * @ param $ width canvas width * @ param $ height canvas height * @ param $ vcodelen verification code length * @ param $ pointnum interference pixel count * @ param number of lines interfering with $ linenum ** train of thought: create a verification code canvas, generate and fill in the background color, generate the verification code content/interference pixel/line, fill in the canvas, and output. */$ Width = 100; $ height = 30; $ vcodelen = 4; $ pointnum = 200; $ linenum = 3; // create a canvas $ image = imagecreatetruecolor ($ width, $ height); // create a color block $ bgcolor = imagecolorallocate ($ image, 255,255,255); // fill the canvas background color imagefill ($ image, 0, 0, $ bgcolor ); // Verification Code content for ($ I = 0; $ I <$ vcodelen; $ I ++) {// font size $ fontsize = 5; // font color, the color is random within the specified range $ fontcolor = imagecolorallocate ($ image, rand (0,120), rand (0,120), rand (0, 120); $ data = 'abcdefghijklmnopqrstuvwxyz0123456789 '; // the content of the verification code is randomly intercepted in the above string $ fontcontent = substr ($ data, rand (0, strlen ($ data )), 1); // string display position $ x = ($ I * $ width/4) + rand (5, 15); $ y = rand (5, 10 ); // The font size of the string filling image // imagestring can be 1-5. The imagettftext function (font file required) imagestring ($ image, $ fontsize, $ x, $ y, $ fontcontent, $ fontcolor); // imagettftext ($ image, $ fontsize, 0, $ x, $ y, $ fontcolor, '/font/Geneva. dfont ', $ Fontcontent);} // interference pixel for ($ I = 0; $ I <$ pointnum; $ I ++) {$ pointcolor = imagecolorallocate ($ image, rand (0,120), rand (0,120), rand (0,120); // canvas fill pixel point function imagesetpixel ($ image, rand (0, $ width), rand (0, $ height), $ pointcolor);} // interfere with the line for ($ I = 0; $ I <$ linenum; $ I ++) {$ linecolor = imagecolorallocate ($ image, rand (0,120), rand (0,120), rand (0,120); // The canvas fill line function imageline ($ image, rand (0, $ width), rand (0, $ height), rand (0, $ width), rand (0, $ height), $ linecolor) ;}// the image output format header ('content-type: image/png '); // output the verification code image imagepng ($ image); // destroy the canvas imagedestroy ($ image);?>

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.