php汉字验证码-中文验证码-支持uft8和gbk
uft8 案例
- <?php
- define(‘WWW.PHPZY.COM’, ‘WWW.PHPZY.COM’);
- function verify($user_str){//从cookie中对比用户的输入
- if(md5($cookie_str.WWW.PHPZY.COM)==$_COOKIE[‘vrify’])
- return 1;
- else
- return 0;
- }
- function c2ch($num){
- $ch_h = chr(substr($num,0,2)+160);
- $ch_l = chr(substr($num,2,2)+160);
- return $ch_h.$ch_l;
- }
- function num_rand(){
- mt_srand((double)microtime() * 1000000);
- $d= mt_rand(16,36);
- $n= mt_rand(1,19);
- return c2ch($d*100+$n);
- }
- $k[0]=num_rand();
- $k[1]=num_rand();
- $k[2]=num_rand();
- $k[3]=num_rand();
- //print_r($k);
- $cookie_str=implode(”, $k);
- setcookie(‘vrify’,md5($cookie_str.WWW.PHPZY.COM),time()+600);
- $str[0]=iconv(‘gb2312′,’UTF-8’,$k[0]);
- $str[1]=iconv(‘gb2312′,’UTF-8’,$k[1]);
- $str[2]=iconv(‘gb2312′,’UTF-8’,$k[2]);
- $str[3]=iconv(‘gb2312′,’UTF-8’,$k[3]);
- //构造图像
- $x_size=80;
- $y_size=25;
- $font=’heiti.ttf’;
- $pic=imagecreate($x_size,$y_size);
- $background_color = imagecolorallocate ($pic, 255, 255, 255);
- $black=imagecolorallocate($pic,0,0,0);
- $red=imagecolorallocate($pic,255,0,0);
- $ddd=imagecolorallocate($pic,255,0,255);
- imagettftext($pic,15,mt_rand(-8,8),6,mt_rand(19,22),$black,$font,$str[0]);
- imagettftext($pic,15,mt_rand(-8,8),37,20,$black,$font,$str[2]);
- imagettftext($pic,mt_rand(15,17),mt_rand(-8,8),22,20,$ddd,$font,$str[1]);
- imagettftext($pic,mt_rand(15,17),mt_rand(-8,8),54,mt_rand(19,22),$red,$font,$str[3]);
- imagerectangle($pic, 0, 0, $x_size – 1, $y_size – 1,$black);
- Imagepng($pic);
- ImageDestroy($pic);
- ?>
gbk案例
- <?php
- error_reporting(0);
- session_start();
- Header("Content-type: image/PNG");
- $str = "的一是在了不和有大这主中人上为们地个用工时要动国产以我到他会作来分生对于学下级就年";
- $imgWidth = 130;
- $imgHeight = 40;
- $authimg = imagecreate($imgWidth,$imgHeight);
- $bgColor = ImageColorAllocate($authimg,255,255,255);
- $fontfile = "simhei.ttf";
- $white=imagecolorallocate($authimg,234,185,95);
- imagearc($authimg, 150, 8, 20, 20, 75, 170, $white);
- imagearc($authimg, 180, 7,50, 30, 75, 175, $white);
- imageline($authimg,20,20,180,30,$white);
- imageline($authimg,20,18,170,50,$white);
- imageline($authimg,25,50,80,50,$white);
- $noise_num = 800;
- $line_num = 20;
- imagecolorallocate($authimg,0xff,0xff,0xff);
- $rectangle_color=imagecolorallocate($authimg,0xAA,0xAA,0xAA);
- $noise_color=imagecolorallocate($authimg,0x00,0x00,0x00);
- $font_color=imagecolorallocate($authimg,0x00,0x00,0x00);
- $line_color=imagecolorallocate($authimg,0x00,0x00,0x00);
- for($i=0;$i<$noise_num;$i++){
- imagesetpixel($authimg,mt_rand(0,$imgWidth),mt_rand(0,$imgHeight),$noise_color);
- }
- for($i=0;$i<$line_num;$i++){
- imageline($authimg,mt_rand(0,$imgWidth),mt_rand(0,$imgHeight),mt_rand(0,$imgWidth),mt_rand(0,$imgHeight),$line_color);
- }
- $randnum=rand(0,strlen($str)-4);
- if($randnum%2)$randnum+=1;
- $str = substr($str,$randnum,8);
- $_SESSION[chart]=$str;
- //$str = iconv("GB2312","UTF-8",$str);
- ImageTTFText($authimg, 20, 0, 16, 30, $font_color, $fontfile, $str);
- //ImagePNG($authimg);
- //ImageDestroy($authimg);
- @header("Expires: -1");
- @header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
- @header("Pragma: no-cache");
- header(‘Content-Type: image/jpeg’);
- imagepng($authimg);
- ?>
记得放字体!没有字体随便去控制面板-字体-复制一个过来就可以使用了