php验证码的封装,PHP实现生成验证码的封装

/**

* 验证码生成类

* User: xiaoyu

* Date: 2019/4/12

* Time: 11:50

*/

class CaptchaC

{

private $image;

public function __construct()

{

//创建一张底图

$this->image = imagecreatetruecolor(200, 50);

//为一幅图像分配颜色

$bgcolor = imagecolorallocate($this->image, 255, 255, 255);

//区域填充 imagefill()  在 image 图像的坐标 x,y(图像左上角为 0, 0)处用 color

//颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)。

imagefill($this->image, 0, 0, $bgcolor);

//        $this->alpNum();

$this->cha();

$this->interfere();

}

public function __destruct()

{

imagedestroy($this->image);

}

public function outPut()

{

header('Content-Type: image/png');//加这一句

imagepng($this->image);

}

//生成汉字验证码

public function cha()

{

$fontfile = "MSYH.TTF";//字体样式

$fonts = $this->font();//验证码字体库--

$strdb = str_split($fonts, 3);//中文一个字符占3个字节

$captch_code = '';

for ($i = 0; $i 

//为一幅图像分配颜色--随机生成验证码的颜色

$fontcolor = imagecolorallocate($this->image, rand(0, 120), rand(0, 120), rand(0, 120));

$text = $strdb[mt_rand(0, count($strdb) - 1)];

$captch_code .= $text;

//为图像插入字符

imagettftext($this->image, mt_rand(20, 24), mt_rand(-60, 60), (40 * $i + 20), mt_rand(30, 35), $fontcolor, $fontfile, $text);

}

}

//生成字母数字验证码

public function alpNum()

{

session_start();

$content = "ABCDEFGHIJKLMNPQRSTUVWXYabcdefghigkmnpqrstuvwxy3456789";

$captcha = '';

for ($i = 0; $i 

$fontsize = 6;

//为一幅图像分配颜色--随机生成验证码的颜色

$fontcolor = imagecolorallocate($this->image, rand(0, 120), rand(0, 120), rand(0, 120));

//substr根据下标截取字符串,strlen获取字符串长度

$fontcontent = substr($content, mt_rand(0, strlen($content) - 1), 1);

$captcha .= $fontcontent;

$x = ($i * 200 / 4) + rand(30, 40);

$y = rand(20, 30);

// 水平地画一行字符串 参数:要画的图像,字体大小,图像的坐标x,y,字体颜色

imagestring($this->image, $fontsize, $x, $y, $fontcontent, $fontcolor);

}

$_SESSION['captcha'] = $captcha;

}

//生成干扰元素

public function interfere()

{

for ($i = 0; $i 

//为一幅图像分配颜色--随机生成点的颜色

$pointcolor = imagecolorallocate($this->image, rand(50, 200), rand(50, 200), rand(50, 200));

//— 画一个单一像素

imagesetpixel($this->image, rand(1, 199), rand(1, 59), $pointcolor);

}

for ($i = 0; $i 

//为一幅图像分配颜色--随机生成线的颜色

$linecolor = imagecolorallocate($this->image, rand(80, 220), rand(80, 220), rand(80, 220));

//— 画一条线段 需要俩个点确定一条线

imageline($this->image, rand(1, 199), rand(1, 59), rand(1, 199), rand(1, 59), $linecolor);

}

}

//验证码字体库--

private function font()

{

return "关雎鸠在河洲窈窕淑君子好逑参差荇菜左流窈窕淑女寤寐求求不得寤寐思服悠哉悠哉辗转反侧参差荇菜右采之窈窕淑琴瑟友参差荇菜左右芼窈窕钟鼓乐蒹葭苍苍白露为霜所谓伊人在水一方溯洄从之道阻且长溯游从之宛在水中央蒹葭凄凄白露未晞所谓伊人在水之湄溯洄从之道阻且跻溯游从之宛在水中坻蒹葭采采白露未已所谓伊人在水之涘溯洄从之道阻且右溯游从之宛在水中沚";

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的原生PHP验证码类的示例: ```php class Captcha { // 验证码字符长度 protected $length = 4; // 验证码宽度 protected $width = 100; // 验证码高度 protected $height = 40; // 验证码字符集 protected $charset = '0123456789'; // 验证码图片 protected $image; // 构造函数 public function __construct() { $this->create(); } // 生成验证码 public function create() { $this->image = imagecreatetruecolor($this->width, $this->height); $bgColor = imagecolorallocate($this->image, 255, 255, 255); imagefill($this->image, 0, 0, $bgColor); $code = $this->generateCode(); $textColor = imagecolorallocate($this->image, 0, 0, 0); $font = __DIR__ . '/arial.ttf'; for ($i = 0; $i < $this->length; $i++) { $x = ($this->width - 20) / $this->length * $i + 10; $y = $this->height / 2 + 10; imagettftext($this->image, 20, rand(-10, 10), $x, $y, $textColor, $font, $code[$i]); } header('Content-Type: image/png'); imagepng($this->image); } // 验证码校验 public function check($code) { if (strtolower($code) == strtolower($_SESSION['captcha'])) { return true; } else { return false; } } // 生成随机字符 protected function generateCode() { $code = ''; $charsetLength = strlen($this->charset); for ($i = 0; $i < $this->length; $i++) { $code .= $this->charset[rand(0, $charsetLength - 1)]; } $_SESSION['captcha'] = $code; return $code; } // 析构函数 public function __destruct() { imagedestroy($this->image); } } ``` 使用示例: ```php $captcha = new Captcha(); ``` 这将生成一个验证码图像并将其输出到浏览器。要检查用户输入的验证码是否正确,可以调用`check()`方法: ```php if ($captcha->check($_POST['captcha'])) { // 验证码正确 } else { // 验证码错误 } ``` 请注意,此示例仅用于演示目的。在实际应用程序中,您可能需要添加其他功能(例如:检查用户是否已经提交了表单,以防止滥用)并进行更多的安全检查。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值