tp5验证码类文件位于extend/other/Captcha.php<br>
<br>
<br>
<br>
//控制器中 获取验证码<br>
public function get_captcha(){ <br>
//使用memcheck 设置session <br>
Session::init(['prefix'=> 'wll_','type'=> '','auto_start' => true]);<br>
$captcha = new \other\Captcha(86,48,4);<br>
echo $captcha->showImg(); <br>
Session::set('code',$captcha->getCaptcha());<br>
exit; <br>
}<br>
<br>
<br>
<br>
<br>
模块中: <img class="code-img lazy" src="/static/imghwm/default1.png" data-src="__WEBSITE__/public/index.php/seller/login/get_captcha" alt="tp5验证码" ><br>
<br>
<br>
<br>
<br>
echo Session::get('code','wll_'); 输出验证码<br>
<br>
<br>
以下是Captcha.php 类<br>
<?php <br />
namespace other;<br>
class Captcha{<br>
private $width;<br>
private $height;<br>
private $codeNum;<br>
private $code;<br>
private $im;<br>
//初始化<br>
function __construct($width=80, $height=20, $codeNum=4){<br>
$this->width = $width;<br>
$this->height = $height;<br>
$this->codeNum = $codeNum;<br>
}<br>
//显示验证码<br>
function showImg(){<br>
//创建图片<br>
$this->createImg();<br>
//设置干扰元素<br>
$this->setDisturb();<br>
//设置验证码<br>
$this->setCaptcha();<br>
//输出图片<br>
$this->outputImg();<br>
}<br>
<br>
//获取显示的验证码,用来验证验证码是否数据正确<br>
function getCaptcha(){<br>
return $this->code;<br>
}<br>
<br>
//创建图片<br>
private function createImg(){<br>
$this->im = imagecreatetruecolor($this->width, $this->height);<br>
$bgColor = imagecolorallocate($this->im, 255, 255, 255);//创建的前景为白色<br>
imagefill($this->im, 0, 0, $bgColor);<br>
}<br>
<br>
//设置干扰元素<br>
private function setDisturb(){<br>
$area = ($this->width * $this->height) / 20;<br>
$disturbNum = ($area > 250) ? 250 : $area;<br>
//加入点干扰<br>
for ($i = 0; $i
$color = imagecolorallocate($this->im, rand(0, 255), rand(0, 255), rand(0, 255));<br>
imagesetpixel($this->im, rand(1, $this->width - 2), rand(1, $this->height - 2), $color);<br>
}<br>
//加入弧线<br>
for ($i = 0; $i
$color = imagecolorallocate($this->im, rand(128, 255), rand(125, 255), rand(100, 255));<br>
imagearc($this->im, rand(0, $this->width), rand(0, $this->height), rand(30, 300), rand(20, 200), 50, 30, $color);<br>
}<br>
}<br>
//设置验证码随机数<br>
private function createCode(){<br>
$str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";<br>
for ($i = 0; $i codeNum; $i++) {<br>
$this->code .= $str{rand(0, strlen($str) - 1)};<br>
}<br>
}<br>
//设置验证码<br>
private function setCaptcha(){<br>
<br>
//设置验证码随机数 <br>
$this->createCode();<br>
<br>
//文字前景<br>
$color = imagecolorallocate($this->im, rand(50, 250), rand(100, 250), rand(128, 250));<br>
<br>
//因为imagechar最大的文字字体为5,字体太小而不用这个方式了<br>
//imagechar($this->im, $size, $x, $y, $this->code{$i}, $color); <br>
<br>
//因为imagechar最大的文字字体为5,而这里要显示更大的文字,所以用 imagefttext <br>
imagefttext($this->im,30,0,10,35,$color,'static/pc/fonts/monofont.ttf',$this->code);//图象资源,尺寸,角度,x轴,y轴,颜色,字体路径,文本插入图像<br>
}<br>
//输出图片<br>
private function outputImg(){<br>
if (imagetypes() & IMG_JPG) {<br>
header('Content-type:image/jpeg');<br>
imagejpeg($this->im);<br>
} elseif (imagetypes() & IMG_GIF) {<br>
header('Content-type: image/gif');<br>
imagegif($this->im);<br>
} elseif (imagetype() & IMG_PNG) {<br>
header('Content-type: image/png');<br>
imagepng($this->im);<br>
} else {<br>
die("Don't support image type!");<br>
}<br>
}//end<br>
}
AD:真正免费,域名+虚机+企业邮箱=0元

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version
SublimeText3 Linux latest version
