- $ Authnum = '';
- $ Ychar = ", A, B, C, D, E, F, G, H, I, J, K, L, M, n, O, P, Q, R, S, T, U, V, W, X, Y, Z ";
- $ List = explode (",", $ ychar); // delimiter function
- For ($ I = 0; $ I <4; $ I ++ ){
- $ Randnum = rand (0, 35 );
- $ Authnum. = $ list [$ randnum]; // output as an array
Method 2: define a private function.
- Private function createCheckCode ()
- {
- For (I = 0; I CodeNum; I ++)
- {
- Number = rand (0, 2 );
- Switch (number)
- {
- Case 0: rand_number = rand (48, 57); break; // number
- Case 1: rand_number = rand (); break; // uppercase letter
- Case 2: rand_number = rand (97,122); break; // lowercase letter
- }
- $ Asc = sprintf ("% c", rand_number );
- $ Asc_number = asc_number.asc;
- }
- Return asc_number;
- }
Method 3: use a random seed to generate a php verification code.
- Srand (microtime () * 100000); // equivalent to a timer
- $ String = "abcdefghigklmnopqrstuvwxyz123456789 ";
- For ($ I = 0; $ I <4; $ I ++)
- {
- $ New_number. = $ string [rand (0, strlen ($ string)-1)]; // An array is generated immediately.
- }
Method 4,
- For ($ I = 0; $ I <4; $ I ++)
- {
- $ Rand. = dechex (rand (); // Convert decimal to hexadecimal
- }
Next I will go to the focus of this article: PHP GD Library: (provides IPI for a series of image processing functions to generate image processing images) enable the GD Library in php: php. in the ini configuration file, remove ";" from "; extension = php_gd2.dll". some GD library functions: 1. imagecreatetruecolor (int x_size, int Y_size) create a true color image 2. imagecolorallocate (resource image, int red, int green, int blue) allocates color to an image with three primary colors. imagestring (resource, font, int x, int y, content, color) plotting function 4. header ("Content-type: image/jpeg") the output function php header defines the header action. in php5, the following types are supported: 1, Content-type: xxxx/yyyy 2, Location: xxxx: yyyy/zzzz 3, Status: nnn xxxxxx xxxx/yyyy indicates the type of the content file, for example, image/gif image/jpeg image/png imagejpeg (), imagegif (), imagepang () 5. iamgeline (resource image, int x1, int y1, int x2, int y2, int color); draw line function, (int x, int y) start coordinate 6. imagesetpixel (resource image, int x, int y, int color) Function 7. imagettftext (resource image, float size, float angle, int x, int y, int color, string fontfile, string text) function 8. iconv ("gb 2312 "," UTF-8 "," string "); // you must first convert the text into a UTF-8 format php verification code to insert a Chinese character. here, you can refer to the php Chinese verification code generation method. Example 1: code for randomly generating numbers and letters:
- // Che. php
- Session_start ();
- For ($ I = 0; $ I <4; $ I ++)
- {
- $ Rand. = dechex (rand (1, 15 ));
- }
- $ _ SESSION ['Check _ num'] = $ rand;
- $ Image = imagecreatetruecolor (50, 30 );
- $ Bg = imagecolorallocate ($ im, 0); // specifies the background color when the palette is used for the first time.
- $ Te = imagecolorallocate ($ im, 255,255,255 );
- Imagestring ($ image, 6, rand (0, 20), rand (0, 2), $ rand, $ te );
- Ob_clean (); // The Image "http: // localhost/**. PHP" is displayed on the php webpage because the verification code is generated.
- Header ("Content-type: image/jpeg"); imagejpeg ($ image );
- ?>
Example 2: draw the interference line code for the image:
- For ($ I = 0; $ I <8; $ I ++) // draw multiple lines
- {
- $ Cg = imagecolorallocate ($ im, rand (0,255), rand (0,255), rand (0,255); // Generate random colors
- Imageline ($ im, rand (10, 40), 0, rand (10, 40), 20, $ cg );
- }
Example 3: draw the interference point code for the image:
- For ($ I = 0; $ I <80; $ I ++) // draw multiple points
- {
- Imagesetpixel ($ im, rand (0, 40), rand (0, 20), $ cg );
- }
Example 4: write the text into the image code:
$ Str = array ('my', 'my', 'login', 'login'); // store the displayed Chinese characters
- For ($ I = 0; $ I <4; $ I ++)
- {
- $ Sss. = $ str [rand ()]; // randomly display Chinese characters
- }
// $ Str = iconv ("gb2312", "UTF-8", $ str); // Chinese character encoding conversion, which does not seem to require me
- Imagettftext ($ im, 10, 0, rand (5, 60), rand (5, 60), $ te, "simhei. ttf", $ sss );//
0: font skew. "simhei. ttf": font style, which is generally placed under the root directory; This completes the entire php image verification code production process. the script editor hopes that this article will help you to generate a php verification code using the gd Library. |