搜索
系统检测到您的用户名不符合规范:

tp5验证码

浏览:14368 发布日期:2016年06月16日 分类:功能实现 关键字: tp5
tp5验证码
类文件位于extend/other/Captcha.php



//控制器中 获取验证码
public function get_captcha(){
//使用memcheck 设置session
Session::init(['prefix'=> 'wll_','type'=> '','auto_start' => true]);
$captcha = new \other\Captcha(86,48,4);
echo $captcha->showImg();
Session::set('code',$captcha->getCaptcha());
exit;
}




模块中: <img title="点击刷新验证码" src="__WEBSITE__/public/index.php/seller/login/get_captcha" class="code-img" onClick="this.src='__WEBSITE__/public/index.php/seller/login/get_captcha?d='+Math.random();" />




echo Session::get('code','wll_'); 输出验证码


以下是Captcha.php 类
<?php
namespace other;
class Captcha{
private $width;
private $height;
private $codeNum;
private $code;
private $im;
//初始化
function __construct($width=80, $height=20, $codeNum=4){
$this->width = $width;
$this->height = $height;
$this->codeNum = $codeNum;
}
//显示验证码
function showImg(){
//创建图片
$this->createImg();
//设置干扰元素
$this->setDisturb();
//设置验证码
$this->setCaptcha();
//输出图片
$this->outputImg();
}

//获取显示的验证码,用来验证验证码是否数据正确
function getCaptcha(){
return $this->code;
}

//创建图片
private function createImg(){
$this->im = imagecreatetruecolor($this->width, $this->height);
$bgColor = imagecolorallocate($this->im, 255, 255, 255);//创建的前景为白色
imagefill($this->im, 0, 0, $bgColor);
}

//设置干扰元素
private function setDisturb(){
$area = ($this->width * $this->height) / 20;
$disturbNum = ($area > 250) ? 250 : $area;
//加入点干扰
for ($i = 0; $i < $disturbNum; $i++) {
$color = imagecolorallocate($this->im, rand(0, 255), rand(0, 255), rand(0, 255));
imagesetpixel($this->im, rand(1, $this->width - 2), rand(1, $this->height - 2), $color);
}
//加入弧线
for ($i = 0; $i <= 1; $i++) {//最多两条线
$color = imagecolorallocate($this->im, rand(128, 255), rand(125, 255), rand(100, 255));
imagearc($this->im, rand(0, $this->width), rand(0, $this->height), rand(30, 300), rand(20, 200), 50, 30, $color);
}
}
//设置验证码随机数
private function createCode(){
$str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";
for ($i = 0; $i < $this->codeNum; $i++) {
$this->code .= $str{rand(0, strlen($str) - 1)};
}
}
//设置验证码
private function setCaptcha(){

//设置验证码随机数
$this->createCode();

//文字前景
$color = imagecolorallocate($this->im, rand(50, 250), rand(100, 250), rand(128, 250));

//因为imagechar最大的文字字体为5,字体太小而不用这个方式了
//imagechar($this->im, $size, $x, $y, $this->code{$i}, $color);

//因为imagechar最大的文字字体为5,而这里要显示更大的文字,所以用 imagefttext
imagefttext($this->im,30,0,10,35,$color,'static/pc/fonts/monofont.ttf',$this->code);//图象资源,尺寸,角度,x轴,y轴,颜色,字体路径,文本插入图像
}
//输出图片
private function outputImg(){
if (imagetypes() & IMG_JPG) {
header('Content-type:image/jpeg');
imagejpeg($this->im);
} elseif (imagetypes() & IMG_GIF) {
header('Content-type: image/gif');
imagegif($this->im);
} elseif (imagetype() & IMG_PNG) {
header('Content-type: image/png');
imagepng($this->im);
} else {
die("Don't support image type!");
}
}//end
}
评论() 相关
后面还有条评论,
评论支持使用[code][/code]标签添加代码
您需要登录后才可以评论 登录 | 立即注册
收藏
gg22mm
积分:709 等级:LV2
热点推荐
(追記) (追記ここまで)
最新更新

我们

合作

网站

信息

ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。

AltStyle によって変換されたページ (->オリジナル) /