-
-
- /**
- * Verification code class
- */
- class Base_Tool_Verify{
- /**
- * How to generate verification code
- */
- public static function createCode(){
- //Generate verification code image
- // Full Number
- $str = "D,B,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,W,J,K,M, M,N,K,P,Q,R,S,T,U,V,W,X,Y,Z"; //The characters to be displayed can be added or deleted by yourself
- $list = explode(",", $str);
- $cmax = count($list) - 1;
- $verifyCode = '';
- for ( $i=0; $i < 4; $i++ ){
- $randnum = mt_rand(0, $ cmax);
- $verifyCode .= $list[$randnum]; //Take out the characters and combine them into the verification code characters we want
- }
- $_SESSION['code'] = $verifyCode; //Put the characters into SESSION
-
- $im = imagecreate(80,28); //Generate an image
- $black = imagecolorallocate($im, 0,0,0); //This and the following three items are the set colors
- $white = imagecolorallocate($ im, 244,249,244);
- $gray = imagecolorallocate($im, rand(200,255),rand(200,255),rand(200,255));
- $red = imagecolorallocate($im, rand(200,255), rand(200,255), rand (200,255));
- $rand_color = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
- $rand_color2 = imagecolorallocate($im, rand(0,255), rand(0,255), rand (0,255));
- imagefill($im,0,0,$white); //Fill the image with color
- $pix=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
- mt_srand();
- for($i=0;$i<100;$i++)
- {
- imagesetpixel($im,mt_rand(0,180),mt_rand(0,35),$pix);
- }
- imageline( $im, 0,rand(0,28), 80, rand(0,28), $rand_color);
- imageline($im, 0,rand(0,28), 80, rand(0,28), $ rand_color2);
-
- //Draw the verification code into the image
- $mycolor = imagecolorallocate($im, 0, 78, 152);
- $path = API_PATH.DS.'Base'.DS.'Tool';
- putenv( 'GDFONTPATH=' . $path);
- $font = 'simhei.ttf';
- $arrstr = str_split($verifyCode);
- imagettftext($im, 20, rand(0,50), 10, rand(20, 30), $mycolor, $font, $arrstr[0]);
- imagettftext($im, 15, rand(0,50), 20, rand(20,30), $mycolor, $font, $arrstr[1 ]);
- imagettftext($im, 15, rand(0,50), 30, rand(20,30), $mycolor, $font, $arrstr[2]);
- imagettftext($im, 15, rand( 0,50), 40, rand(20,30), $mycolor, $font, $arrstr[3]);
- $font2=imagecolorallocate($im,41,163,238);
- imagerectangle($im,0,0,1 ,1,$font2);
- imagepng($im);
- imagedestroy($im);
- }
- }
-
Copy code
|