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 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元