PHP image verification code production implementation sharing (full)

Source: Internet
Author: User
Tags imagejpeg
Recently I am learning about php, but now I am just getting started, so I don't know much about it. I just learned from the basics and won't go online, then remember it in this magic weapon, just as today we met the function rand (). I thought about how to use it, and finally remembered the verification code, digital verification code, and letter verification code, chinese verification code, but I can't do it myself. what should I do? search online, read other people's code, don't understand, watch videos, listen to the teacher, and explain the functions encountered in it, take notes. Generally, the random verification codes on webpages are surrounded by certain boxes, which seem to be in the background of images. After reading it, I think that although I have encountered many problems, I believe that as long as I am down to earth, I will definitely learn. Now I want to make a summary. I may write a mess myself, but I believe it will happen one day. 1. random number generation -- create image -- write random number into image -- add interference value (point, line) to image -- keep in session -- reference in form; random Functions: rand (int min, int max); tens of thousands of changes, I read a lot of code for generating random numbers on the Internet, including random numbers and letters, random numbers (arrays), and so on; can't do without rand (); the code is as follows (some copy on the Internet, I hope you will not be surprised. first:
The code is as follows:
$ 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

Second:
The code is as follows:
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;
}

Third:
The code is as follows:
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.
}

Fourth:
The code is as follows:
For ($ I = 0; $ I <4; $ I ++)
{
$ Rand. = dechex (rand (); // Convert decimal to hexadecimal
}

GD Library: (provides IPI for a series of image processing functions to generate image processing images)
Enable GD Library in php: in the php. ini configuration file, remove ";" from "; extension = php_gd2.dll";
Some GD library functions: 1. imagecreatetruecolor (int x_size, int Y_size) new true color image
2. imagecolorallocate (resource image, int red, int green, int blue) allocates color to an image with three primary colors.
3. 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) point function 7. imagettftext (resource image, float size, float angle, int x, int y, int color, string fontfile, string text) function 8. iconv ("gb2312", "UTF-8", "string"); // you must first convert the text into a UTF-8 php verification code to insert a Chinese character.

Code for randomly generating numbers and letters:

The code is as follows:
// 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 );
?>

Draw the interference line code for the image:
The code is as follows:
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 );
}

Draw the interference point code for the image:
The code is as follows:
For ($ I = 0; $ I <80; $ I ++) // draw multiple points
{
Imagesetpixel ($ im, rand (0, 40), rand (0, 20), $ cg );
}

Write text into the image code:
The code is as follows:
$ 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;

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.