1.需要用到的知识
1.随机验证码文本的生成。
2.通过 gd库生成图片
2.随机验证码文本的生成
这部分比较简单,直接上代码。
//随机生成二维码
private function rand_code()
{
$code = "";
$txt = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for ($i = 0; $i < $this->code_num; $i++) {
$code.= $txt[mt_rand(0, strlen($txt) - 1)];
}
return $code;
}
这里生成的是大小写字母和数字的二维码,如果要有中文则稍微复杂一点。
3.验证码图片的生成
验证码的生成主要分为几个部分:
- 创建画布对象;
- 为画布设置干扰线条;
- 为画布设置干扰像素点;
- 将生成的文本验证码绘制到画布中。
(1)创建画布
//创建二维码背景
private function create_backgroud_image()
{
//绘制画布
$this->res = imagecreatetruecolor($this->width, $this->height);
//设置背景颜色
$background_color = imagecolorallocate($this->res, 200, 200, 200);
//为画布填充背景色
imagefill($this->res, 0, 0, $background_color);
}
(2)绘制干扰线条
//为二维码背景生成随机线条
private function create_line()
{
imagesetthickness($this->res,$this->create_line_width());
for ($i = 0; $i < 10; $i++) {
imageline($this->res,
mt_rand(0, $this->width),
mt_rand(0, $this->height),
mt_rand(0, $this->width),
mt_rand(0, $this->height),
$this->create_color(150,255,150,255,150,255));
}
}
(3)绘制干扰像素点
//为二维码背景生成随机点
private function create_spot(){
$max = $this->width * 5;
for($i=0;$i<$max;$i++){
imagesetpixel($this->res,mt_rand(0,$this->width),mt_rand(0,$this->height),$this->create_color(100,255));
}
}
(4)将生成的文本验证码绘制到画布中
//生成验证码图片
private function create_txt_image($txt){
//$txt = $this->rand_code();//获取生成的code
$font = realpath("times.TTF");
$size = $this->width/$this->code_num;
$w = $size<$this->width?$size:$this->width;
for($i=0;$i<$this->code_num;$i++){
$txt_color = imagecolorallocate($this->res,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
imagettftext($this->res,
$w,
mt_rand(-10,10),
($w)*$i,
$this->height/1.4,
$this->create_color(0,130,0,130,0,130),
$font,
$txt[$i]);
}
}
将上述方法整合到Captcha类中,代码如下
<?php
/**
* 验证码生成类
*/
class Captcha
{
// protected $code = "";
//对外
protected $width;
protected $height;
protected $res;
protected $code_num;
public function __construct($width = 100,$height = 50,$code_num = 5)
{
$this->width = $width;
$this->height = $height;
$this->code_num = $code_num;
}
public function render()
{
header("Content-type:image/jpg");
$this->create_backgroud_image();
$this->create_line();
$this->create_spot();
$code = $this->rand_code();
$this->create_txt_image($code);
imagepng($this->res);
imagedestroy($this->res);
return $code;
}
//随机生成验证码
private function rand_code()
{
$code = "";
$txt = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for ($i = 0; $i < $this->code_num; $i++) {
$code.= $txt[mt_rand(0, strlen($txt) - 1)];
}
return $code;
}
//创建二维码背景
private function create_backgroud_image()
{
//绘制画布
$this->res = imagecreatetruecolor($this->width, $this->height);
//设置背景颜色
$background_color = imagecolorallocate($this->res, 200, 200, 200);
//为画布填充背景色
imagefill($this->res, 0, 0, $background_color);
}
//生成随机颜色
private function create_color($min_r = 0,$max_r = 255,$min_g = 0,$max_g = 255,$min_b = 0,$max_b = 255)
{
return imagecolorallocate($this->res, mt_rand($min_r,$max_r), mt_rand($min_g,$max_g), mt_rand($min_b,$max_b));
}
//自动生成线条的宽度
private function create_line_width(){
$w = $this->width/100;
return $w;
}
//为验证码背景生成随机线条
private function create_line()
{
imagesetthickness($this->res,$this->create_line_width());
for ($i = 0; $i < 10; $i++) {
imageline($this->res,
mt_rand(0, $this->width),
mt_rand(0, $this->height),
mt_rand(0, $this->width),
mt_rand(0, $this->height),
$this->create_color(150,255,150,255,150,255));
}
}
//为验证码背景生成随机点
private function create_spot(){
$max = $this->width * 5;
for($i=0;$i<$max;$i++){
imagesetpixel($this->res,mt_rand(0,$this->width),mt_rand(0,$this->height),$this->create_color(100,255));
}
}
//生成验证码图片
private function create_txt_image($txt){
//$txt = $this->rand_code();//获取生成的code
$font = realpath("times.TTF");
$size = $this->width/$this->code_num;
$w = $size<$this->width?$size:$this->width;
for($i=0;$i<$this->code_num;$i++){
$txt_color = imagecolorallocate($this->res,mt_rand(0,150),mt_rand(0,150),mt_rand(0,150));
imagettftext($this->res,
$w,
mt_rand(-10,10),
($w)*$i,
$this->height/1.4,
$this->create_color(0,130,0,130,0,130),
$font,
$txt[$i]);
}
}
}
下面是项目结构
1.php代码
<?php
session_start();
include_once "Captcha.php";
header("Content-type:image/jpg");
$captcha = new Captcha(100,50);
$code = $captcha->render();
$_SESSION["captcha"] = $code;
1.html代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="2.php" method="POST">
<input id="captcha" name="captcha" type="text" placeholder="请输入验证码">
<img src="1.php" alt="验证码" onclick="this.src='1.php?'+Math.random()" />
<input type="submit" value="提交">
</form>
</body>
<script>
</script>
</html>
2.php代码
<?php
session_start();
$captcha = strtolower($_POST["captcha"]);
if($captcha ==strtolower($_SESSION["captcha"])){
echo "ok";//验证码成功
}else{
echo "fail";//验证码失败
}
*.ttf为字体文件
项目代码在这里:https://download.csdn.net/download/weixin_38017811/19577265?spm=1001.2014.3001.5501