PHP 验证码生成类(可定制长度和内容)

时间:2023-10-07 15:26:32

===================VerifyTool======================

<?php

class VerifyTool
{
private $fontPath; //字体路径
private $verifyStr; //字符库
private $verifyLen; //字符数
private $verifyCode; //验证码
private $verifyImg; //验证图像 public function __construct($fontPath)
{
$this->fontPath = $fontPath;
$this->verifyLen = 4;
$this->verifyStr = '0123456789'
. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
. 'abcdefghijklmnopqrstuvwxyz';
} /**
* 设置验证码所包含的字符
* @param $str
*/
public function setChars($str)
{
$this->verifyStr = $str;
} /**
* 设置验证码字符串长度
* @param $num
*/
public function setLength($num)
{
$this->verifyLen = $num;
} /**
* 得到验证码的内容(默认转换为小写)
* @param bool $tolower
* @return string
*/
public function getCode($tolower = true)
{
if ($tolower) {
return strtolower($this->verifyCode);
} else {
return $this->verifyCode;
}
} /**
* 绘制验证码图像
* @return $this
*/
public function drawImage()
{
$width = $this->verifyLen * 50;//图片宽度
$height = 60;//图片高度
$graylevel = 240;//背景灰度
$fontsize = 24;//字体大小
$content = '';//验证码内容
$image = imagecreatetruecolor($width, $height);
$bgcolor = imagecolorallocate($image, $graylevel, $graylevel, $graylevel);
imagefill($image, 0, 0, $bgcolor);
//绘制随机字符
for ($i = 0; $i < $this->verifyLen; $i++) {
$fontcolor = imagecolorallocate($image, rand(120, 220), rand(60, 150), rand(100, 200));
$fontchar = mb_substr($this->verifyStr, rand(0, mb_strlen($this->verifyStr, 'utf8') - 1), 1, 'utf8');
$x = ($i * $width / $this->verifyLen) + rand(10, 20);
$y = $height / 2 + rand(-5, 5) + $fontsize / 2;
imagettftext($image, $fontsize, rand(-60, 60), $x, $y, $fontcolor, $this->fontPath, $fontchar);
$content .= $fontchar;
}
//绘制随机点
for ($i = 0; $i < $this->verifyLen * 10; $i++) {
$pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));
imagefilledellipse($image, rand(0, $width), rand(0, $height), 5, 5, $pointcolor);
}
//绘制随机直线
for ($i = 0; $i < $this->verifyLen; $i++) {
$linecolor = imagecolorallocatealpha($image, rand(60, 120), rand(80, 160), rand(60, 120), rand(80, 100));
imageline($image, 0, rand(0, $height - 0), $width, rand(0, $height - 0), $linecolor);
}
$this->verifyCode = $content;
$this->verifyImg = $image;
return $this;
} /**
* 显示验证码
*/
public function show()
{
header('content-type: image/png');
imagepng($this->verifyImg);
imagedestroy($this->verifyImg);
}
}

VerifyTool.class.php

==================使用方式====================

创建一个verify_image.php文件:

<?php
//开启SESSION
session_start();
//引入验证码工具
require_once 'VerifyTool.class.php';
//初始化工具(必须传入有效的字体路径)
$verifyTool = new VerifyTool('../res/simhei.ttf');
//绘制图像并显示
$verifyTool->drawImage()->show();
//将验证码信息保存至SESSION
$_SESSION['verify_code'] = $verifyTool->getCode();

如果直接打开效果如下:

PHP 验证码生成类(可定制长度和内容)

还可以为验证工具设置参数:

$verifyTool = new VerifyTool('../res/simhei.ttf');
//设置验证码字符内容
$verifyTool->setChars('巧学巧用');
//设置验证码长度
$verifyTool->setLength(10);
$verifyTool->drawImage()->show();

设置参数后效果如下:

PHP 验证码生成类(可定制长度和内容)

==================验证方式====================

创建一个 verify_test.php 文件:

<?php
//如果需要使用SESSION,必须在脚本开始处开启SESSION
session_start();
?>
<!--显示验证码和输入框-->
<img src="verify_image.php">
<form action="verify_test.php" method="get">
<input type="text" name="mycode">
<input type="submit" value="submit">
</form> <?php
if (!empty($_GET['mycode'])) {
//去除用户输入的多余空格,并将输入转换为小写字母
$mycode = strtolower(trim($_GET['mycode']));
//将SESSION保存的验证码信息取出,并进行比较
if ($mycode == $_SESSION['verify_code']) {
echo '验证成功,请继续';
} else {
echo '验证失败,请重试';
}
}

这仅仅是测试验证码的小Demo,如果要在其他场景下使用的话,就需要理解其中的原理,做到巧学巧用才行。