/** Default Home **/
- 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 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 background color
- Imagefill ($im, 0, 0, $BGC);
Get numbers randomly
- $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 Main text information
- Imagestring ($im, 6, ($i *10) +3, rand (0,6), $random, $colors [rand (0, COUNT ($colors)-1)]);
- }
Add Random noise
- for ($i =0; $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 verification Code in $_session
- Sess ("verify", $verify);
Output a picture and release the cache
- Header (' content-type:image/png ');
- Imagepng ($im);
- Imagedestroy ($im);
- }
- };
- ?>
Copy CodeExample 2, a class that generates a random string and a validation code for a class of random strings and Authenticode. The following code implementation, the main do can be very good to distinguish between a get_code (), another create_check_image (), the output image directly after the call, session () to take the verification code directly Get_code (). Session_star () must be placed at the front when using the session. Full code:
Class Randcheckcode
- {
- /* Function Name: Get_code ()
- * Function: Get random string
- Parameters
- 1, (int) $length = #随机字符长度
- 2, (int) $mode = 0 #随机字符类型,
- 0 for case English and numbers, 1 for numbers, 2 for lowercase letters, 3 for uppercase letters,
- 4 is uppercase and lowercase, 5 is capital letter and number, 6 is lowercase and number
- * Return: The obtained string
- */
- function Get_code ($length =32, $mode =0)//Get random verification code functions
- {
- Switch ($mode)
- {
- Case ' 1 ':
- $str = ' 123456789 ';
- Break
- Case ' 2 ':
- $str = ' abcdefghijklmnopqrstuvwxyz ';
- Break
- Case ' 3 ':
- $str = ' abcdefghijklmnopqrstuvwxyz ';
- Break
- Case ' 4 ':
- $str = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz ';
- 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);//Generate a random number between 0 and $len
- $num =mt_rand (0, $len);//Generate a random number between 0 and $len
- $checkstr. = $str [$num];
- }
- return $checkstr;
- }
/** function Name: Create_check_image ()
- Function: Produces a picture of a checksum code
- Parameter: $checkcode: Check code string
- Return value: Returns the picture
- */
- function Create_check_image ($checkcode)//produce a
- {
- $im =imagecreate (65,22);//produce a picture
- $black =imagecolorallocate ($im, 0,0,0);//Background color
- $white =imagecolorallocate ($im, 255,255,255);//Foreground color
- $gray =imagecolorallocate ($im, 200,200,200);
- Imagefill ($im, 30,30, $gray);//In the $im image coordinates 30,30 (the upper left corner of the image is 0,0) with the $gray color to perform the area fill (that is, with 30, 30 points of the same color and adjacent points will be filled)
Imagestring ($im, 5,8,3, $checkcode, $white);//Use $white color to draw the string $checkcode to 8, 3 coordinates of the image represented by $im (this is the upper-left corner of the string, the upper-left corner of the image is 0,0), 5 is the font size, the font can only be 1,2,3,4 or 5, using the built-in font
- for ($i =0; $i <120; $i + +)
- {
- $randcolor =imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));
- Imagesetpixel ($im, Rand ()%70,rand ()%30, $randcolor),///on $im image with $randcolor color in (rand ()%70,rand ()%30) coordinates (the upper left corner of the image is 0,0) Draw a dot on the
- }
- Header ("Content-type:image/png");
- Imagepng ($im);//Export the image to a browser or file in PNG format
- Imagedestroy ($im);//Destroy Image $im
- }
- }
- /*
- $randcode =new Randcheckcode ();
- $checkstring = $randcode->get_code (5,7);
- $image = $randcode->create_check_image ($checkstring);
- Echo $image;
- */
- ?>
Copy Code |