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

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年的前端工作经验,熟练掌握前端各项技能,熟练多种前端框架,希望遇到志同道合的前端朋友们,一起学习交流,共同进步!