PHP之实现验证码功能
开启GD扩展
知识点:
GD库是用来处理图片的,使用GD库,首先在php.ini中开启gd扩展
extension=gd2(默认已经开启)
开启以后就可以使用image开头的函数了
创建最简单的图片
1. 创建画布
2. 给画布填充颜色(给画布填充的第一个颜色自动填充成背景颜色)
3. 显示图片
$image = imagecreate(200,100);
imagecolorallocate($image,255,25,236);
header('content-type:image/jpeg');
imagejpeg($image);
imagejpeg($image,'./tu.jpeg');
小结:
1. 第一个分配的颜色是背景色
2. 要输出画布,需要设置header()头
3. 保存画布,不需要设置header()头
填充颜色例题
$image = imagecreate(200,100);
$color = imagecolorallocate($image,200,200,200);
switch (rand(1,100)%3){
case 0:
$color=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));
break;
case 1:
$color=imagecolorallocate($image,0,rand(0,255),rand(0,255));
break;
default:
$color=imagecolorallocate($image,0,rand(0,255),0);
}
imagefill($image,0,0,$color);
header('content-type:image/jpeg');
imagejpeg($image);
验证码
验证码的作用:防止暴力破解
原理:创建一个图片,在图片上写上一个随机的字符串
1. 生成随机字符串
2. 创建画布
3. 将字符写到画布上,imagestring(图片资源,内置字体,起始点x,起始点y,字符串,颜色编号)
难点:字符串居中
$x = (画布宽度-字符宽度*字符个数)/2
$y = (画布高度-字符高度)/2
$all_array = array_merge(range('a','z'),range('A','Z'),range(2,9));
$div_array = ['l','o','O','I'];
$array = array_diff($all_array,$div_array);
unset($all_array,$div_array);
$index=array_rand($array,6);
shuffle($index);
$code='';
foreach ($index as $i):
$code.=$array[$i];
endforeach;
$image = imagecreate(90,30);
imagecolorallocate($image,0,rand(0,255),255);
$color = imagecolorallocate($image,213,255,rand(0,255));
$font = 5;
$x=(imagesx($image)-imagefontwidth($font)*strlen($code))/2;
$y=(imagesy($image)-imagefontheight($font))/2;
imagestring($image,$font,$x,$y,$code,$color);
header('content-type:image/jpeg');
imagejpeg($image);
小结:
range():生成指定的范围数组
array_merge():合并数组
array_diff():计算数组的差集
array_rand():随机获取数组元素
在某一张图片上生成验证码
$codeset = '23456789zxcvbnmkjhgfdsaqwertyuipZXCVBNMLKJHGFDSAQWERTYUP';
$code = '';
$max = strlen($codeset);
for ($i=1;$i<=6;$i++){
$index = rand(0,$max-1);
$code.=$codeset[$index];
}
$path = './captcha/captcha_bg'.rand(1,5).'jpg';
$image = imagecreatefromjpeg($path);
$font = 5;
$color = imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));
$x = (imagesx($image)-imagefontwidth($font)*strlen($code))/2;
$y = (imagesy($image)-imagefontheight($font))/2;
imagestring($image,$font,$x,$y,$code,$color);
header('content-type:image/gif');
imagegif($image);
中文验证码
步骤:
1. 将字体拷贝到当前目录下
2. 中文处理需要多字节处理
3. 使用多字节处理函数需要开启相应的扩展extension=mbstring
4. 使用imagettfbox测定中文字符串的范围
5. 使用imagettftext将中文写到画布上
$codeset = '我爱你亲爱的祖国万事万物丹';
$max = mb_strlen($codeset)-1;
$code = '';
for ($i=1;$i<=5;$i++){
$start = rand(0,$max);
$code.=mb_substr($codeset,$start,1);
}
$image = imagecreate(150,50);
$color = imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));
imagefill($image,0,0,$color);
$size = 15;
$angle =0;
$fontfile = 'D:\MVVC\simhei.ttf';
$info = imagettfbbox($size,$angle,$fontfile,$code);
$code_w = $info[4]-$info[6];
$code_h = $info[1]-$info[7];
$x = (imagesx($image)-$code_w)/2;
$x = (imagesx($image)+$code_h)/2;
$textcolor = imagecolorallocate($image,255,0,0);
imagettftext($image,$size,$angle,15,20,$textcolor,$fontfile,$code);
imagejpeg($image,'./tu.jpg');
验证码常用函数
imagecreate():创建画布
imagecreatetruecolor():创建支持真彩色的画布
imagecolorallocate():给画布分配颜色
imagejpeg():将图片以jpeg格式输出
imagegif():将图片以gif格式输出
imagepng():将图片以png格式输出
imagefill():填充颜色
imagesx():获取图片宽度
imagesy():获取图片高度
imagefontwidth():获取内置字体宽度
imagefontheight():获取内置字体高度
imagestring():将字符串写到图片上
imagecreatefromjpeg():打开jpg创建图片资源
imagecreatefrompng():打开png创建图片资源
imagecreatefromgif():打开gif创建图片资源
imagettfbox():测定中文字体的范围
imagettftext():将中文字体写到图片上
imagecopy():图片拷贝
imagedestory():销毁图片资源
imagecopyresampled():拷贝图片并缩放大小
排错思路
1. 注释header,注释以后,错误信息就出来了
2. 如果没有报错,就留心一下图片代码前有无字符输出,图片前面是不允许有任何字符输出的
3. 查看源码,图片代码是否有空白字符,包括代码前后
4. 如果上面都无效,在header前添加ob_clean()