With snowflake background PHP verification code generation program

Source: Internet
Author: User

Session_Start ();
$img _width=70; First define the length and width of the picture
$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 (0,61)]. $myarray [Mt_rand (0,61)]. $myarray [Mt_rand (0,61)]. $myarray [Mt_rand (0,61)];
Exit ($mystring);
unset ($_session[' Shuzi '));
$_session[' shuzi ' = $mystring;
$aimg = Imagecreate ($img _width, $img _height); Generate pictures
Imagecolorallocate ($aimg, 255,255,255); Picture background, imagecolorallocate the 1th time to define color PHP is considered to be the background
$black = Imagecolorallocate ($aimg, 200,200,200); Define the required Black
Imagerectangle ($aimg, 0,0, $img _width-1, $img _height-1, $black);//A black rectangle to surround the picture

The following is the creation of a snowflake background, in fact, is to generate some symbols on the picture
For ($i =1 $i <=100; $i + +) {//test with 100 first
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)));
In fact, is not a snowflake, is to generate the * number just. In order for them to look "cluttered, 5-color, 6", they have to have their position, color, or even size randomly counted at 1 1, and rand () or mt_rand can do it.
}

With the background generated, it's time to put the generated random numbers up. The reason is similar to above, random number 1 1 places, at the same time let their position, size, color are used into random number ~ ~
In order to distinguish it from the background, the color here is not more than 200, the above is not 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 a picture, not a text display
Imagepng ($AIMG); Generate PNG format ... The effect is quite like a matter of ...
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.