Php Verification Code Generation Program with Snowflake background-PHP source code

Source: Internet
Author: User
Ec (2); & nbsp; session_start (); & nbsp; $ img_width70; & nbsp; Define the length and width of the image first & nbsp; $ img_height28; & nbsp; $ num_arrayrange (0, 9); & nbsp; $ small_char_arrayrange (a, z); & nbsp; $ bi script ec (2 ); script

Session_start ();
$ Img_width = 70; // define the length and width of the image first.
$ Img_height = 28;
$ Num_array = range (0, 9 );
$ Small_char_array = range ('A', 'z ');
$ Big_char_array = range ('A', 'z ');
$ Myarray = array_merge ($ num_array, $ small_char_array, $ big_char_array );
$ Mystring = $ myarray [mt_rand ()]. $ myarray [mt_rand ()]. $ myarray [mt_rand ()]. $ myarray [mt_rand ()];
// Exit ($ mystring );
// Unset ($ _ SESSION ['shuzi']);
$ _ SESSION ['shuzi'] = $ mystring;
$ Aimg = imagecreate ($ img_width, $ img_height); // generate an image
Imagecolorallocate ($ aimg, 255,255,255); // image background color, ImageColorAllocate 1st definition color PHP considers it as background color
$ Black = imagecolorallocate ($ aimg, 200,200,200); // define the required black
Imagerectangle ($ aimg, $ img_width-1, $ img_height-1, $ black); // enclose an image in a black rectangle first

// The following generates a snowflake background. In fact, some symbols are generated on the image.
For ($ I = 1; $ I <= 100; $ I ++) {// test with 100
Imagestring ($ aimg, 1, mt_rand (1, $ img_width), mt_rand (1, $ img_height), "*", imageColorAllocate ($ aimg, mt_rand (200,255 ), mt_rand (200,255), mt_rand (200,255 )));
// Actually, it is not a snowflake, that is, generating the * number. To make them look "chaotic, 5 colors and 6 colors", you have to make their positions, colors, and even sizes use random numbers when one of them is generated, rand () or mt_rand can be completed.
}

// The background is generated above. Now we should put the generated random number. The principle is similar to the above. Random numbers are placed in one place, and their positions, sizes, and colors are used as random numbers ~~
// To distinguish it from the background, the color here shall not exceed 200, and the color above shall not be less than 200

Imagestring ($ aimg, 5, mt_rand (5, 35), mt_rand (5, $ img_height/2), $ mystring, imageColorAllocate ($ aimg, mt_rand (0,100), mt_rand (0,150 ), mt_rand (0,200 )));

Header ("Content-type: image/png"); // tell the browser that the following data is an image instead of Text
ImagePng ($ aimg); // generate the png format... Hey, the effect is pretty similar...
ImageDestroy ($ aimg );

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.