Php Chinese and English verification code

Source: Internet
Author: User
Tags image identifier prepare rand

// The English verification code is relatively simple. Do not perform hex processing. Simply use the color value and you will be OK. If
Session_start ();
Function rand_create ()
{
// Notify the browser that a PNG image will be output
Header ("Content-type: image/PNG ");
// Prepare the random number generator seeds
Srand (double) microtime () * 1000000 );
// Prepare image parameters
$ Im = imagecreate (62,22 );
$ Black = ImageColorAllocate ($ im, 0); // RGB black identifier
$ White = ImageColorAllocate ($ im, 255,255,255); // RGB white identifier
$ Gray = ImageColorAllocate ($ im, 200,200,200); // RGB gray identifier
// Start plotting
Imagefill ($ im, 0, 0, $ gray );
While ($ randval = rand () % 100000) <10000 );{
$ _ SESSION ["Auth_code"] = $ randval;
// Print the four-digit integer verification code into the image
Imagestring ($ im, 5, 10, 3, $ randval, $ black );
    }
// Add interference pixels
For ($ I = 0; I I <200; $ I ++ ){
$ Randcolor = ImageColorallocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
Imagesetpixel ($ im, rand () % 70, rand () % 30, $ randcolor );
    }
// Output verification image
ImagePNG ($ im );
// Destroy the image identifier
ImageDestroy ($ im );
}
Rand_create ();

// Call method

// The Chinese verification code program is as follows:

Header ("Content-type: image/PNG ");
$ Str = "here sets a Chinese text if the first Chinese WEB station is www.111cn.net ";
$ ImagesW = 140;
$ ImagesH = 40;
//
$ Auimg = imagecreate ($ imagesW, $ imagesH );
$ Bgc = ImageColorAllocate ($ Auimg, 255,255,255 );
$ Font = "heiti. ttf"; // set the font here. You can download any font.
$ White = imagecolorallocate ($ Auimg, 234,185, 95 );
Imagearc ($ Auimg, 150, 8, 20, 20, 75,170, $ white );
Imagearc ($ Auimg, 180, 75,175, 30, $ white );
Imageline ($ Auimg, 20, 20, 180,30, $ white );
Imageline ($ Auimg, 20, 18, 170,50, $ white );
Imageline ($ Auimg, 25, 50, 80, 50, $ white );
$ Noise_num = 800;
$ Line_num = 20;
Imagecolorallocate ($ Auimg, 0xff, 0xff, 0xff );
$ Rectangle_color = imagecolorallocate ($ Auimg, 0xAA, 0xAA, 0xAA );
$ Noise_color = imagecolorallocate ($ Auimg, 0x00,0x00,0x00 );
$ Font_color = imagecolorallocate ($ Auimg, 0x00,0x00,0x00 );
$ Line_color = imagecolorallocate ($ Auimg, 0x00,0x00,0x00 );
For ($ I = 0; $ I <$ noise_num; $ I ++ ){
Imagesetpixel ($ Auimg, mt_rand (0, $ imagesW), mt_rand (0, $ imagesH), $ noise_color );
}
For ($ I = 0; $ I <$ line_num; $ I ++ ){
Imageline ($ Auimg, mt_rand (0, $ imagesW), mt_rand (0, $ imagesH), mt_rand (0, $ imagesW), mt_rand (0, $ imagesH ), $ line_color );
}
$ MtRnd = rand (0, strlen ($ str)-4 );
If ($ mtRnd % 2) $ mtRnd + = 1;
$ Str = substr ($ str, $ mtRnd, 8 );
$ Str = iconv ("GB2312", "UTF-8", $ str );
ImageTTFText ($ Auimg, 20, 0, 16, 30, $ font_color, $ font, $ str );
ImagePNG ($ Auimg );
ImageDestroy ($ Auimg );
/*
In common, verification codes are stored by other containers, such as Sessions and cookies. Otherwise, verification is meaningless.
This article is composed of www.111cn.net. For more information, see the source. Thank you for your cooperation.

 

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.