继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

php之验证码

拉风的咖菲猫
关注TA
已关注
手记 354
粉丝 44
获赞 212

<?php

header("Content-type: image/png");

gd_info();

$num=5;//生成验证码的字母个数

$type=2;//验证码的类型,0表示只有数字,1表示数字和小写字母,2表示数字和大写字母

$str=getCode($num,$type);

$width=$num*20;

$hight=30;

$im=imagecreatetruecolor($width,$hight);

 

$color=imagecolorallocate($im,100,100,100);

$bg = imagecolorallocate($im,255,2,255);

imagefill($im,0,0,$bg);

 

###添加干扰点

for($i=0;$i<200;$i++){

        imagesetpixel($im,rand(0,$width),rand(0,$hight),$color);

}

  

###添加干扰线

for($i=0;$i<5;$i++){

        imageline($im,rand(0,$width),rand(0,$hight),rand(0,$width),rand(0,$hight),$color);

}

 

####在画布上画字符

for($i=0;$i<$num;$i++){

        imagechar($im,1000,5+(18*$i),8,$str[$i],$color);//图像源,字体大小,倾斜角度(0为不倾斜),起始x坐标,起始y坐标,颜色,字体文件,内容

}

 

//ob_clean();//清除缓冲区内

 

imagepng($im);

imagedestroy($im);

 

function getCode($num,$type)

{

        $content="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

        $t=array(9,35,strlen($content)-1);

        $c="";

        for($i=0;$i<$num;$i++)

        {

                $c.=$content[rand(0,$t[$type])];

        }

        return $c;

}

?>

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP