当前位置:IT橙子的前端技术博客 > 前端笔记 > 正文

php生成验证码函数

时间:2018-04-24 来源:原创 分类:前端笔记 阅读:

参数说明:函数调用:$code=vcode(100,40,30,4);
返回值是$string,是生成的验证码图片;
参数说明:
$width:图片的宽,
$height:图片的高,
$fontSize:字体大小,
$countElement:验证码个数,
$countPixel:小点的数目,
$countLine:线段数目。

效果图:


 
function vcode($width=120,$height=40,$fontSize=30,$countElement=5,$countPixel=100,$countLine=4){
header('Content-type:image/jpeg');
//$width=120;
//$height=40;
$element=array(
    'a','b','c','d','e','f','g','h','i','j','k','l',
    'm','n','o','p','q','r','s','t', 'u','v','w','x');
//随机生成个验证码
$string='';
for($i=0;$i<$countElement;$i++){
    $string.=$element[rand(0,count($element)-1)];
}
//$string=$element[rand(0,count($element)-1)];
$img= imagecreatetruecolor($width,$height);
$colorBg=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));//分配颜色
$colorBorder=imagecolorallocate($img,rand(200,255),rand(200,255),rand(180,255));//分配颜色
$colorSpolit=imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,255));//分配颜色
$colorText=imagecolorallocate($img,rand(10,100),rand(10,50),rand(10,100));//分配颜色
//填充背景
imagefill($img,0,0,$colorBg);
//绘制一个矩形
imagerectangle($img,0,0,$width-1,$height-1,$colorBorder);
//画小点
for($i=0;$i<$countPixel;$i++){
    imagesetpixel($img,rand(0,$width-1),rand(0,$height-1),$colorSpolit);
};
///画线段
for($j=0;$j<$countLine;$j++){
    imageline($img,rand(0,$width/2),rand(0,$height/2),rand($width/2,$width),
rand($height/2,$height),$colorSpolit);
}
//字体向图像写入文本
imagettftext($img,$fontSize,rand(-5,5),rand(5,25),rand(25,35),$colorText,'
fonts/ManyGifts.ttf',$string);
imagejpeg($img);
imagedestroy($img);
return $string;
}

-------------------------------------------正文完~-------------------------------------------

关于橙子

    橙子,一个奋斗在前端路上的女程序员~~

    橙子,热爱前端,关注前端,4年的前端工作经验,熟练掌握前端各项技能,熟练多种前端框架,希望遇到志同道合的前端朋友们,一起学习交流,共同进步!

学习交流

  • 微信公众号:IT橙子6 微信扫一扫添加关注 获取更多前端学习资料!
  • QQ交流群:592969963 IT橙子前端技术交流群

相关推荐