$img = imagecreate(120,30);
$white = imagecolorallocate($img,255,255,255);
$black = imagecolorallocate($img,0,0,0);
https://点干扰素
for ($i = 0;$i< 200;$i++) {
https://这里的颜色我就用随机产生的颜色了,之前是给定好了的
imagesetpixel($img,rand(0,120),rand(0,30),imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255)));
}
https://线干扰素
for($i = 0;$i<20;$i++){
https://这里的颜色我就用随机产生的颜色了,之前是给定好了的
imageline($img,rand(0,120),rand(0,30),rand(0,120),rand(0,30),imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255)));
}
https://像素字体大小
$size = 16;
https://角度大小
$fontfile = "simhei.ttf";
https://获取文本范围,见下方图
$info=imagettfbbox($size,rand(0,5),$fontfile,$code);
https://echo '
';
https://print_r($info);
$code_w=$info[4]-$info[6];https://字符串的宽度
$code_h=$info[1]-$info[7];https://字符串的高度
https://imagesx($img) image 所代表的图像的宽度
$x=(imagesx($img)-$code_w)/2;https://起始点的$x
$y=(imagesy($img)+$code_h)/2;https://起始点的$y
https://向图像写入文本
imagettftext($img,$size,rand(0,5),$x,$y,imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255)),$fontfile,$code);
https://生成图像
imagejpeg($img);
imagedestroy($img);
这里$info这个是一个数组(可以print_r()看一看),数组中的每一项对应的其实是坐标,imagesx( $img ) 就是 image 所代表的图像的宽度,这样我想尽量让汉字保持在图片的居中位置,不然都跑偏了也看不见了
生成验证码
小例子
session_start();
header("content-type:text/html;chartset:utf-8");
echo '
';
var_dump($_SESSION);
var_dump($_POST);
echo '
';if($_SESSION['code'] == $_POST['code']){
echo '验证成功';
https://进行后续操作balabala
}
?>
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
Document
总结
到此这篇关于php结合GD库实现中文验证码的简单方法的文章就介绍到这了,更多相关php结合GD库实现中文验证码内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!