Create a verification code for the GD Library-PHP source code

Source: Internet
Author: User
The php code used to create the verification code for the GD Library:

 4, // verification code length 'code _ size' => 20, // The verification code font size 'code _ width' => 100, // the width of the verification code box 'code _ height' => 30, // The height of the verification code box 'code _ num' => true, // enable the digit 'code _ upper '=> true, // enable the uppercase letter 'code _ lower' => true, // enable the lower-case letter 'code _ delete' => '1li0oo2zz '// exclude obfuscation characters ); // Convert the configuration item to the corresponding variable foreach ($ code_ini as $ key => $ value) {$ key = $ value ;} // Create a verification code canvas $ code_img = imagecreatetruecolor ($ code_width, $ code_height); $ black = imagecolorallocate ($ code_img, 0, 0, 0 ); // fill in random background color $ bg = imagecolorallocate ($ code_img, rand (200,255), rand (200,255), rand (200,255); imagefill ($ code_img, 0, 0, $ bg); // randomly draw 100 interference points for ($ I = 0; $ I <100; $ I ++) {$ color = imagecolorallocate ($ code_img, rand (0,200), rand (0,200), rand (0,200); imagesetpixel ($ code_img, rand (2, $ code_width-2), rand (2, $ code_height-2 ), $ color) ;}// randomly draw 10 interference lines for ($ q = 0; $ q <10; $ q ++) {$ color = imagecolorallocate ($ code_img, rand (0,200), rand (0,200), rand (0,200); imageline ($ code_img, rand (2, $ code_width-2), rand (2, $ code_height-2 ), rand (2, $ code_width-2), rand (2, $ code_height-2), $ color);} // Get the verification code $ code = get_code ($ code_count, $ code_num, $ code_upper, $ code_lower, $ code_remove); for ($ I = 0; $ I <$ code_count; $ I ++) {$ color = imagecolorallocate ($ code_img, rand (0,200 ), rand (0,200), rand (0,200); imagettftext ($ code_img, $ code_size, rand (-30, 30), 5 + 20 * $ I, 24, $ color, '.. /fonts/msyh. ttf', $ code [$ I]);} imagerectangle ($ code_img, 0, 0, $ code_width-1, $ code_height-1, $ black ); // output the verification code header ('content-type: image/GIF'); imagegif ($ code_img); // destroy the imagedestroy ($ code_img) resource ); // Obtain the verification code function get_code ($ code_count, $ code_num, $ code_upper, $ code_lower, $ code_remove) {$ num = array (); $ upper = array (); $ lower = array (); if ($ code_num) {$ num = range (0, 9);} if ($ code_upper) {$ upper = range ('A ', 'Z');} if ($ code_lower) {$ lower = range ('A', 'z ');} // merge the matching array $ arr = array_merge ($ num, $ lower, $ upper); // exclude obfuscation characters $ remove = str_split ($ code_remove ); $ arr1 = array_diff ($ arr, $ remove); $ mess = ''; for ($ I = 0; $ I <$ code_count; $ I ++) {$ mess. = $ arr1 [array_rand ($ arr1)];} return $ mess ;}

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.