英文验证码相对简单,不要作hex处理,直接用色彩值就OK了,yzm.php实例代码如下:
- session_start();
- function rand_create()
- {
-
- Header("Content-type: image/PNG");
-
- srand((double)microtime()*1000000);
-
- $im = imagecreate(62,22);
- $black = ImageColorAllocate($im, 0,0,0);
- $white = ImageColorAllocate($im, 255,255,255);
- $gray = ImageColorAllocate($im, 200,200,200);
-
- imagefill($im,0,0,$gray);
- while(($randval=rand()%100000)<10000);{
- $_SESSION["Auth_code"] = $randval;
-
- imagestring($im, 5, 10, 3, $randval, $black);
- }
-
- for($i=0;$i<200;$i++){
- $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
- imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
- }
-
- ImagePNG($im);
-
- ImageDestroy($im);
- }
- rand_create();
调用方法<img src=yzm.php />
中文验证码程序如下:
- Header("Content-type: image/PNG");
- $str = "这里设置一中文如果中国WEB第一站www.phpfensi.com";
- $imagesW = 140;
- $imagesH = 40;
-
- $Auimg = imagecreate($imagesW,$imagesH);
- $bgc = ImageColorAllocate($Auimg,255,255,255);
- $font = "heiti.ttf";
- $white=imagecolorallocate($Auimg,234,185,95);
- imagearc($Auimg, 150, 8, 20, 20, 75, 170, $white);
- imagearc($Auimg, 180, 7,50, 30, 75, 175, $white);
- imageline($Auimg,20,20,180,30,$white);
- imageline($Auimg,20,18,170,50,$white);
- imageline($Auimg,25,50,80,50,$white);
- $noise_num = 800;
- $line_num = 20;
- imagecolorallocate($Auimg,0xff,0xff,0xff);
- $rectangle_color=imagecolorallocate($Auimg,0xAA,0xAA,0xAA);
- $noise_color=imagecolorallocate($Auimg,0x00,0x00,0x00);
- $font_color=imagecolorallocate($Auimg,0x00,0x00,0x00);
- $line_color=imagecolorallocate($Auimg,0x00,0x00,0x00);
- for($i=0;$i<$noise_num;$i++){
- imagesetpixel($Auimg,mt_rand(0,$imagesW),mt_rand(0,$imagesH),$noise_color);
- }
- for($i=0;$i<$line_num;$i++){
- imageline($Auimg,mt_rand(0,$imagesW),mt_rand(0,$imagesH),mt_rand(0,$imagesW),mt_rand(0,$imagesH),$line_color);
- }
- $mtRnd=rand(0,strlen($str)-4);
- if($mtRnd%2)$mtRnd+=1;
- $str = substr($str,$mtRnd,8);
- $str = iconv("GB2312","UTF-8",$str);
- ImageTTFText($Auimg, 20, 0, 16, 30, $font_color, $font, $str);
- ImagePNG($Auimg);
- ImageDestroy($Auimg);
共同点就是验证码都借助于其它容器来保存如session,cookie等,否则就没有验证的意义了. |