php验证码类
2015-12-13 09:54
661 查看
<?php /** * Created by JetBrains PhpStorm. * User: 张华 * Date: 14-3-8 * Time: 下午12:21 * QQ: 746502560@qq.com * To change this template use File | Settings | File Templates. */ final class Captcha { private $width; private $height; private $codeNum; private $code; private $im; public function __construct($width=80, $height=35, $codeNum=4) { $this->width = $width; $this->height = $height; $this->codeNum = $codeNum; } function showImg() { //创建图片 $this->createImg(); //设置干扰元素 $this->setDisturb(); //设置验证码 $this->setCaptcha(); //输出图片 $this->outputImg(); } public function checkCaptcha($captchaVal){ $codeNum=httpRequest::getSession('codeNum'); if($codeNum == $captchaVal){ return true; } return false; } public 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); unset($bgColor); } private function setDisturb() { $area = ($this->width * $this->height) / 20; $disturbNum = ($area > 250) ? 250 : $area; //加入弧线 for ($i = 0; $i <= 5; $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); } unset($area,$disturbNum,$color); } private function createCode() { $str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ"; for ($i = 0; $i < $this->codeNum; $i++) { $this->code .= $str{rand(0, strlen($str) - 1)}; } httpRequest::setSession('codeNum',$this->code); unset($str,$i); } private function setCaptcha() { $this->createCode(); for ($i = 0; $i < $this->codeNum; $i++) { $color = imagecolorallocate($this->im, rand(0, 250), rand(0, 250), rand(0, 250)); $size = rand(14, 20); $x = floor($this->width / $this->codeNum) * $i + 5; $y = rand(0, $this->height - 20); imagechar($this->im, $size, $x, $y, $this->code{$i}, $color); } unset($color,$size,$x,$y,$i); } private function outputImg() { if (imagetypes() & IMG_JPG) { header('Content-type:image/jpeg'); imagejpeg($this->im); imagedestroy($this->im); } elseif (imagetypes() & IMG_GIF) { header('Content-type: image/gif'); imagegif($this->im); imagedestroy($this->im); } elseif (imagetype() & IMG_PNG) { header('Content-type: image/png'); imagepng($this->im); imagedestroy($this->im); } else { die("没有你需要的图像资源类型!"); } } public function __destruct(){ $this->width=null; $this->height=null; $this->codeNum=null; $this->code=null; $this->im=null; unset($this->width,$this->height,$this->codeNum,$this->code); } }
相关文章推荐
- php日期类
- phpunit测试学习 2 分类总结断言涉及哪些方面
- phpunit测试学习 1:一点简单的扼要有用的东西的总结 一点入门认识
- phpunit测试成功 phpunit测试实践代码
- 3分钟wamp中php安装 pear 然而并没有用 并没能借此安装phpunit 不得不借用了其他的方式安装phpunit
- 5分钟windows wamp php安装phpunit 2015最新安装实践
- php--关于函数(2)变量范围
- RESTFUL及Yii框架下开发RESTFUL的细节
- 2015-oschina开源盛会,关于鸟哥分享的php7的会议纪要
- [hdoj试题]A+B for Input-Output Practice (VIII)
- php链接数据库
- FragmentPagerAdapter与FragmentStatePagerAdapter使用详解与区别
- cakephp中使用ajax获得数据,动态折线显示
- [hdoj试题]A+B for Input-Output Practice (VII)
- cakephp 中对于数据库的增删查改操作
- ProFTP如何热加载配置文件
- ORA-06577:output parameter not a bind variable
- [hdoj试题]A+B for Input-Output Practice (VI)
- 内容提供者ContentProvider的使用详解
- [hdoj试题]A+B for Input-Output Practice (V)