php 验证码

来源:互联网 发布:php rides 缓存 编辑:程序博客网 时间:2024/06/07 08:40
<?php /***获取验证码字符串*$str 字符串*$len 长度*$type 类型 1,数字 2,字母 3,混合*/function getVStr(&$str,$len=4,$type=1){ $num=range(0,9); $zm=range('a','z'); $str='';for ($i=0; $i < $len; $i++) { switch ($type) {case 1:$str.=$num[mt_rand(1,8)];break;case 2:$str.=$zm[mt_rand(0,25)];break;case 3:$ind=mt_rand(0,1);$str.=$ind?$zm[mt_rand(0,25)]:$num[mt_rand(1,8)];break;}}}/***生成验证码图片*$width 图片宽度*$height 图片高度*$len 长度*$type 类型 1,数字 2,字母 3,混合*$px 干扰点数目*$line 干扰线数目*/function proV($width=300, $height=30,$len=4,$type=1,$px=300,$line=5){$img=imagecreatetruecolor($width, $height);$bg=imagecolorallocate($img, 250, 250, 250);imagefilledrectangle($img, 0, 0, $width-1, $height-1, $bg);getVStr($str,$len,$type);$strArr=str_split($str,1);$fontfile='./arial.ttf';//字体文件//验证码内容foreach ($strArr as $i => $v) {$x=$i*($width/$len)+5;$y=mt_rand(30,35);$fg=imagecolorallocate($img, mt_rand(0,120), mt_rand(0,120), mt_rand(0,120));$angle=mt_rand(-30,30);imagettftext($img, 20, $angle, $x, $y, $fg, $fontfile, $v);// imagestring($img, 10, $x, $y, $v, $fg);}//干扰点for ($i=0; $i < $px; $i++) { $color=imagecolorallocate($img, mt_rand(60,200), mt_rand(60,200), mt_rand(60,200));imagesetpixel($img, mt_rand(0,$width-1), mt_rand(0,$height-1), $color);}//干扰线for ($i=0; $i < $line; $i++) { $color=imagecolorallocate($img, mt_rand(100,200), mt_rand(100,200), mt_rand(100,200));imageline($img, mt_rand(0,$width-1), mt_rand(0,$height-1), mt_rand(0,$width-1), mt_rand(0,$height-1), $color);}//把验证码信息保存到session中$_SESSION['l2js_yb_vf']=$str;//输出图片header("content-type:image/jpeg");imagejpeg($img);imagedestroy($img);}proV(170,42,5,3); ?>

0 0
原创粉丝点击