-
-
/**Default homepage **/ - class DefaultController extends AppController
- {
- public function index() {
- $len = 5;
- $str = "ABCDEFGHIJKLNMPQRSTUVWXYZ12 3456789" ;
$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 the background color
- imagefill($im, 0, 0, $bgc);< /p>
//Get 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 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 the verification code in $_SESSION
- sess("verify", $verify);
//Output picture And release the cache
- header('Content-type: image/png');
- imagepng($im);
- imagedestroy($im);
- }
- };
- ?>
-
Copy Code
Example 2, PHP instance of a class that generates random strings and verification codes
Class for generating random strings and verification codes.
The implementation of the following code is mainly to distinguish one get_code() and the other create_check_image(). The output image directly calls the latter one. When session() takes the verification code, just get_code() directly.
When using session, session_star() must be placed first.
Full code:
-
-
- class RandCheckCode
- {
- /*Function name: get_code()
- *Function: Get a random string
- *Parameters:
- 1. (int)$length = 32 #Random character length
- 2. (int)$mode = 0 #Random character type,
- 0 is uppercase and lowercase English and numbers, 1 is numbers, 2 is lowercase letters, 3 is uppercase letters,
- 4 is uppercase and lowercase letters, 5 is uppercase letters and numbers, 6 is lowercase letters and numbers
- *Return: obtained string
- */
- function get_code($length=32,$mode=0)//Get random verification code function
- {
- 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='ABCDEFGHIJKLMNOPQRSTUVWXYZ12345678 90';
- 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: Generate a check code image
- Parameters: $checkcode: Verification code string
- Return value: Return the image
- */
- function create_check_image($checkcode)//Generate a
- {
- $im=imagecreate(65,22);//Generate an image
- $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); //Perform area filling with $gray color at coordinates 30,30 of the $im image (the upper left corner of the image is 0,0) (that is, points with the same color and adjacent points at 30,30 will be filled)< ;/p>
imagestring($im,5,8,3,$checkcode,$white);//Use $white color to draw the string $checkcode to 8,3 of the image represented by $im coordinates (this is the coordinate of the upper left corner of the string, the upper left corner of the entire image is 0,0), 5 is the font size, the font can only be 1, 2, 3, 4 or 5, use 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);//Use $randcolor color to draw a point on the $im image at (rand()%70, rand()%30) coordinates (the upper left corner of the image is 0,0)
- }
- header("Content-type:image/png");
- imagepng($im);//Output the image to the 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 ;
- */
- ?>
-
Copy code
|