/** Default homepage **/
- Class DefaultController extends AppController
- {
- Public function index (){
- $ Len = 5;
- $ Str = "ABCDEFGHIJKLNMPQRSTUVWXYZ123456789 ";
$ Im = imagecreatetruecolor (70, 20 );
- $ Bgc = imagecolorallocate ($ im, 255,255,255 );
- $ Bgtxt = imagecolorallocate ($ im, 220,220,220 );
// Random color palette
- $ Colors = array (
- Imagecolorallocate ($ im, 255, 0, 0 ),
- Imagecolorallocate ($ im, 0,200, 0 ),
- Imagecolorallocate ($ im, 0, 0,255 ),
- Imagecolorallocate ($ im, 0, 0, 0 ),
- Imagecolorallocate ($ im, 255,128, 0 ),
- Imagecolorallocate ($ im, 255,208, 0 ),
- Imagecolorallocate ($ im, 98,186,245 ),
- );
// Fill in the background color
- Imagefill ($ im, 0, 0, $ bgc );
// Obtain random numbers
- $ Verify = "";
- While (strlen ($ verify) <$ len ){
- $ I = strlen ($ verify );
- $ Random = $ str [rand (0, strlen ($ str)];
- $ Verify. = $ random;
// Draw background text
- Imagestring ($ im, 6, ($ I * 10) + 3, rand (0, 6), $ random, $ bgtxt );
- // Draw the main text information
- Imagestring ($ im, 6, ($ I * 10) + 3, rand (0, 6), $ random, $ colors [rand (0, count ($ colors) -1)]);
- }
// Add random colors
- For ($ I = 0; I I <100; $ I ++ ){
- $ Color = imagecolorallocate ($ im, rand (50,220), rand (50,220), rand (50,220 ));
- Imagesetpixel ($ im, rand (0, 70), rand (0, 20), $ color );
- }
// Save the verification code to $ _ SESSION
- Sess ("verify", $ verify );
// Output the image and release the cache
- Header ('content-type: image/png ');
- Imagepng ($ im );
- Imagedestroy ($ im );
- }
- };
- ?>
Example 2: a php instance that generates a random string and verification code class. The implementation of the following code can distinguish one get_code () and the other create_check_image (). the output image is called directly after the session () command, and get_code () is used to obtain the verification code. When using session, you must put session_star () at the beginning. Complete code:
Class RandCheckCode
- {
- /* Function name: get_code ()
- * Function: obtain a random string.
- * Parameters:
- 1. (int) $ length = 32 # random character length
- 2. (int) $ mode = 0 # random character type,
- 0 is a combination of uppercase and lowercase English letters and numbers, 1 is a number, 2 is a lowercase letter, 3 is a capital letter,
- 4: uppercase letters, 5: uppercase letters and numbers, 6: lowercase letters and numbers
- * Return: the obtained string.
- */
- Function get_code ($ length = 32, $ mode = 0) // Obtain the random verification code function
- {
- Switch ($ mode)
- {
- Case '1 ':
- $ Str = '000000 ';
- Break;
- Case '2 ':
- $ Str = 'abcdefghijklmnopqrstuvwxy ';
- Break;
- Case '3 ':
- $ Str = 'abcdefghijklmnopqrstuvwxy ';
- Break;
- Case '4 ':
- $ Str = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy ';
- Break;
- Case '5 ':
- $ Str = 'abcdefghijklmnopqrstuvwxyz1234567890 ';
- Break;
- Case '6 ':
- $ Str = 'abcdefghijklmnopqrstuvwxyz1234567890 ';
- Break;
- Default:
- $ Str = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz1234567890 ';
- Break;
- }
- $ Checkstr = '';
- $ Len = strlen ($ str)-1;
- For ($ I = 0; $ I <$ length; $ I ++)
- {
- // $ Num = rand (0, $ len); // generates a random number between 0 and $ len.
- $ Num = mt_rand (0, $ len); // generates a random number between 0 and $ len.
- $ Checkstr. = $ str [$ num];
- }
- Return $ checkstr;
- }
/** Function name: create_check_image ()
- Function: generates an image of the verification code.
- Parameter: $ checkcode: verification code string
- Return value: returns the image.
- */
- Function create_check_image ($ checkcode) // Generate
- {
- $ Im = imagecreate (); // generate an image
- $ Black = imagecolorallocate ($ im, 0, 0); // background color
- $ White = imagecolorallocate ($ im, 255,255,255); // foreground color
- $ Gray = imagecolorallocate ($ im, 200,200,200 );
- Imagefill ($ im, 30, 30, $ gray); // The coordinates of the $ im image are 30, 30 (the upper left corner of the image is 0, 0) fill the area with the $ gray color (that is, the adjacent points will be filled with the same color as the 30, 30 points)
Imagestring ($ im, 5, 8, 3, $ checkcode, $ white ); // use the $ white color to draw the string $ checkcode to the 8, 3 coordinates of the image represented by $ im (this is the coordinate of the upper left corner of the string, the upper left corner of the entire image is 0, 0), and 5 is the font size, the font can only be 1, 2, 3, 4, or 5. the built-in font is used.
- For ($ I = 0; I I <120; $ I ++)
- {
- $ Randcolor = imagecolorallocate ($ im, rand (0,255), rand (0,255), rand (0,255 ));
- Imagesetpixel ($ im, rand () % 70, rand () % 30, $ randcolor); // use $ randcolor on $ im images at (rand () % 70, rand () % 30) coordinate (the upper left corner of the image is 0, 0 ).
- }
- Header ("Content-type: image/png ");
- Imagepng ($ im); // output the image to a browser or file in PNG format
- Imagedestroy ($ im); // destroy the image $ im
- }
- }
- /*
- $ Randcode = new RandCheckCode ();
- $ Checkstring = $ randcode-> get_code (5, 7 );
- $ Image = $ randcode-> create_check_image ($ checkstring );
- Echo $ image;
- */
- ?>
|