[php验证码实现局部刷新]php验证码实现局部刷新

更新时间:2017-05-08    来源:Access    手机版     字体:

【www.bbyears.com--Access】

/*
 * Auth_code()是验证码函数
 * @access public
 * @param int $width 表示验证码的长度,默认为80
 * @param int $height 表示验证码的高度,默认为20
 * @param int $num  表示验证码中数字位数,默认为4
 * @param int $line  表示验证码中线的条数,默认为4
 * @param int $line  表示验证码中点的个数,默认为150
 */

 代码如下

function Auth_code($width = 80,$height = 20,$num = 4,$line = 4,$dot = 150) {

 $length = floor($width/$num);  //floor:取整数部分 length:每段平均长度
 for($i=0;$i<$num;$i++)
  @$rand.=dechex(mt_rand(1,15));
 $_SESSION["code"]=$rand;
 
 //新建一个黑色底的画板
 $im=imagecreatetruecolor($width,$height);
 
 //取色
 $green=imagecolorallocate($im,0,255,0);
 $red = imagecolorallocate($im,255,0,0);
 $white=imagecolorallocate($im,255,255,255);
 $black = imagecolorallocate($im,0,0,0);
 
 //填充画板
 //imagefill($im,0,0,$green);
   
 //画线
 for($i=0;$i<$line;$i++){
  $color=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  imageline($im,rand(10,$width-10),0,rand(0,$width),$height,$color);
 }
 //画点
 for($i=0;$i<$dot;$i++){
  $color=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
  imagesetpixel($im,rand(0,$width),rand(0,$height),$color);
 }
 //画数字
 for($i=0;$i<$num;$i++) { 
  imagestring($im,5,mt_rand($i*$length+1,($i+1)*$length-8),mt_rand(0,$height-14),$_SESSION["code"][$i],$white);
 }
 
 //输出图片
 header("content-type:image/jpeg");
 imagejpeg($im);
}

 -主要函数:
       imagecreatetruecolor  //新建一个黑色底的画板
       imagecolorallocate     //取色
       imagefill                     //填充画板
       imageline                  //画线
       imagesetpixel           //画点
       imagestring             //画字符串(本验证码为数字)
       注意:在将数字验证码画上画板时要注意字体越界,所以要相应减去一些范围,详见附件。
    -局部刷新验证码:

 代码如下       

本文来源:http://www.bbyears.com/shujuku/32481.html

猜你感兴趣