<?php
namespace captcha;
class Captcha {
private $image;
private $width = 80;
private $height = 35;
public function create_captcha($code) {
//创建彩色画布: param1: 图片宽度, param2: 图片高度
$this->image = imagecreatetruecolor($this->width, $this->height);
//为一幅图像分配颜色, param1: 画布资源, param2: red; param3: green, param4: blue
$bgcolor = imagecolorallocate($this->image, mt_rand(200,255), mt_rand(200,255), mt_rand(200,255));
$textColor = imagecolorallocate($this->image, 0, 0, 0);
//填充矩形颜色, param1: 画布资源, param2: 填充x起点, param3: 填充y起点,param4: 填充x终点, param5: 填充y终点, param6: 要填充的颜色,由imagecolorallocate创建的颜色资源
imagefilledrectangle($this->image, 0, 0, $this->width, $this->height, $bgcolor);
$this->create_interference();
$font = dirname(__FILE__) . "/atnadiannemedium.ttf";
//水平绘制字符串, param1: 画布资源, param2: 字符串尺寸,param3: 绘制角度, param4:左上角的x坐标, param5:左上角的y坐标, param6: 颜色资源,param7: 字体文件, param8:字符串内容
imagettftext($this->image, 20, mt_rand(-10, 10), 8, 25, $textColor, $font, $code);
//设置头部,image/png
header('Content-type: image/png');
//向浏览器或文件输出PNG图像, param1: 画布资源
imagepng($this->image);
// 销毁画布
imagedestroy($this->image);
}
public function rand_code() {
$str = "abcdefghijklmnopqrstuvwxyz0123456789";
$code = '';
for($i = 0; $i < 4; $i++) {
$code .= $str[mt_rand(0, strlen($str) - 1)];
}
return $code;
}
private function create_interference() {
//线条
for ($i = 0; $i < 6; $i++) {
$color = imagecolorallocate($this->image, mt_rand(10, 100), mt_rand(10, 100), mt_rand(10, 100));
// 画一条线条,param1: 画布资源,param2: 填充x起点, param3: 填充y起点,param4: 填充x终点, param5: 填充y终点,param6: 颜色资源
imageline($this->image, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
}
//干扰
for ($i = 0; $i < 100; $i++) {
$color = imagecolorallocate($this->image, mt_rand(100, 200), mt_rand(100, 200), mt_rand(100, 200));
// 水平地画一行字符串, param1: 画布资源,param2: 颜色尺寸(1-5), param3: 填充x起点, param4: 填充y起点, param5: 字符串内容, param6: 颜色资源
imagestring($this->image, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), 'X', $color);
}
}
}
?>
目录: extend/captcha/Captcha.php
使用
<?php
namespace app\backend\controller;
use think\Controller;
use think\Request;
use think\Session;
use captcha\Captcha as CaptchaClass;
class Captcha extends Controller
{
public function get_captcha() {
$captcha = new CaptchaClass();
$code = $captcha->rand_code();
Session::set('captcha', $code);
$captcha->create_captcha($code);
}
}
?>
<input class="login-code" type="text" name="code" placeholder="验证码" value="" /><img src="/backend/get_captcha" width="60" height="30" onclick="this.src='/backend/get_captcha?'+Math.random();" />
用到的主要函数
resource imagecreatetruecolor ( int $width
, int $height
)
int imagecolorallocate ( resource $image
, int $red
, int $green
, int $blue
)
bool imagefilledrectangle ( resource $image
, int $x1
, int $y1
, int $x2
, int $y2
, int $color
)
array imagettftext ( resource $image
, float $size
, float $angle
, int $x
, int $y
, int $color
, string $fontfile
, string $text
)
bool imageline ( resource $image
, int $x1
, int $y1
, int $x2
, int $y2
, int $color
)
bool imagestring ( resource $image
, int $font
, int $x
, int $y
, string $s
, int $col
)