The same number in English
<? Php
Header ("Content-type: image/png ");
// Define the header and declare the image file, preferably png, without copyright concerns;
// Generate a new four-digit integer Verification Code
Session_start (); // enable session;
$ Authnum_session = '';
$ Str = 'abcdefghijkmnpqrstuvwxyz1234567890 ';
// Define the numbers and letters used to display on the image;
$ L = strlen ($ str); // obtain the string length;
// Cyclically and randomly extract four letters and numbers defined above;
For ($ I = 1; $ I <= 4; $ I ++)
{
$ Num = rand (0 L-1 );
// A number is randomly selected at a time. From the first word to the maximum length of the string,
// Minus 1 is because the truncation character starts from 0, so that any 34 characters may be listed in it;
$ Authnum_session. = $ str [$ num];
// Connects the characters obtained from numbers to a total of four digits;
}
Session_register ("authnum_session ");
// It is good to use the session for verification. register the session and name it authnum_session,
// If other pages contain the image
// You can use $ _ SESSION ["authnum_session"] to call
// Generate the verification code image,
Srand (double) microtime () * 1000000 );
$ Im = imagecreate (50, 20); // image width and height;
// Black, white, and gray colors are used;
$ Black = ImageColorAllocate ($ im, 0, 0 );
$ White = ImageColorAllocate ($ im, 255,255,255 );
$ Gray = ImageColorAllocate ($ im, 200,200,200 );
// Print the four-digit integer verification code into the image
Imagefill ($ im, 68, 30, $ gray );
// Comment out the line without interfering with the line;
$ Li = ImageColorAllocate ($ im, 220,220,220 );
For ($ I = 0; $ I <3; $ I ++)
{// Add three interference lines; or do not; depending on the situation, because it may affect user input;
Imageline ($ im, rand (), rand (), rand (20, 40), rand (), $ li );
}
// The Position of the character in the image;
Imagestring ($ im, 5, 8, 2, $ authnum_session, $ white );
For ($ I = 0; $ I <90; $ I ++)
{// Add interference pixels
Imagesetpixel ($ im, rand () % 70, rand () % 30, $ gray );
}
ImagePNG ($ im );
ImageDestroy ($ im );
?>
Chinese
<? Php
/*
* File: code. php
* Function: generate a verification code
*/
Session_start ();
// Set content-type
Header ("Content-type: image/png ");
// Create an image
$ Im = imagecreatetruecolor (120, 30 );
$ ChineseChar = array ("person", "out", "come", "friend", "", "filial piety", "benevolence", "righteousness ", "Li", "Lian", "Zhong", "Guo", "Zhong", "Yi", "White", "man", "fire", "Earth ", "gold", "Wood", "lei", "wind", "dragon", "Tiger", "day", "Earth ",
"Sheng", "Dizzy", "Dish", "bird", "Tian", "3", "bai", "Qian", "Fu", "love ", "Emotion", "Beast", "worm", "fish", "Nine", "Net", "new", "degree", "Ah", "Alas ", "ah", "Oh", "Yi", "old", "less", "day ",
"Month", "Star ");
// Create a color
$ Fontcolor = imagecolorallocate ($ im, 255,255,255 );
$ Bg = imagecolorallocate ($ im, 0, 0, 0 );
// Set text
For ($ I = 0; $ I <4; $ I ++) $ text. = $ ChineseChar [(array_rand ($ ChineseChar)];
$ _ SESSION ['code'] = $ text;
// Set the font [url] http://www.font.com.cn/downlist/s_12_3.html#/url] to download the _ GBK family of fonts, which are generally supported by the GD library!
$ Font = 'gbk. ttf ';
// Add text
Imagettftext ($ im, 18, 0, 11, 21, $ fontcolor, $ font, iconv ("GB2312", "UTF-8", $ text ));
// Output image
Imagepng ($ im );
Imagedestroy ($ im );
?>