英文验证码相对简单,不要作hex处理,直接用色彩值就OK了,yzm.php实例代码如下:
session_start(); function rand_create() { //通知浏览器将要输出PNG图片 Header( "Content-type: image/PNG" ); //准备好随机数发生器种子 srand((double)microtime()*1000000); //准备图片的相关参数 $im = imagecreate(62,22); $black = ImageColorAllocate( $im , 0,0,0); //RGB黑色标识符 $white = ImageColorAllocate( $im , 255,255,255); //RGB白色标识符 $gray = ImageColorAllocate( $im , 200,200,200); //RGB灰色标识符 //开始作图 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)); //开源代码phpfensi.com 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等,否则就没有验证的意义了.
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did29459