搜索
首页php教程PHP源码支持背景为调色板和图片文件的验证码类

支持背景为调色板和图片文件的验证码类

<?
 /**
  *   验证码类
  *   @author  firerat
  *   @email    lukai_rat@163.com
  *   @time     Feb 16  2011 15:28
  *   @lastmodify Feb 16  2011 15:28
  */
 
class verifycode{
        //image source
        private  $im;
        //图片宽度
        private  $w;
        //图片高度
        private  $h;
        //验证字符集合
        private  $text;
        //字符数
        private $length;
        //字体
        private  $font = array();
        //字体大小
        private  $fontsize = array();
        //字体颜色
        private  $fontcolor = array();
        //字体的偏转角度
        private  $angel = array();
        //背景色
        private  $backgroundcolor = array();
        //背景图片
        private $backgroundimage = array();
        //坐标-x
        private $x  ;
        //坐标-y
        private $y  ;
        //验证码存放的字段
        private $checkcodefield ;
 
        /**
         * 构造函数
         *
         */
        public function __construct(){
          //图片宽度
          $this->w = 500 ;
          //图片高度
          $this->h = 600;
          //验证字符
          $this->text = &#39;0123456789qwertyuiopasdfghjklzxcvbnm&#39;;
          //字体
          $this->font = array(
                 &#39;C:\\WINDOWS\\Fonts\\Ming Imperial.TTF&#39;,
                 &#39;C:\\WINDOWS\\Fonts\\latha.TTF&#39;,
                 &#39;C:\\WINDOWS\\Fonts\\ARIALNBI.TTF&#39;,
                 &#39;C:\\WINDOWS\\Fonts\\GOTHICBI.TTF&#39;
                );
          //字符数
          $this->length = &#39;4&#39;;
          //字体大小
          $this->fontsize = array(50,60,70);
          //字体颜色
          $this->fontcolor = array(
                 array(&#39;red&#39;=>0x14, &#39;green&#39;=>0x37,&#39;blue&#39;=>0xad),
                 array(&#39;red&#39;=>0x6e, &#39;green&#39;=>0x86,&#39;blue&#39;=>0xd6),
                 array(&#39;red&#39;=>0x2c, &#39;green&#39;=>0x40 ,&#39;blue&#39;=>0x81),
                 array(&#39;red&#39;=>0x06, &#39;green&#39;=>0x1f ,&#39;blue&#39;=>0x70),
                 array(&#39;red&#39;=>0x14, &#39;green&#39;=>0x37 ,&#39;blue&#39;=>0xad),
                 array(&#39;red&#39;=>0x57, &#39;green&#39;=>0x79 ,&#39;blue&#39;=>0xc0)
          );
          //字体的偏转角度
          $this->angel = array(2,4,8,-2,-4,-8);
          //背景色
          $this->backgroundcolor = array(
                  array(&#39;red&#39;=>0x14, &#39;green&#39;=>0x37,&#39;blue&#39;=>0xad),
                  array(&#39;red&#39;=>0x6e, &#39;green&#39;=>0x86,&#39;blue&#39;=>0xd6),
                  array(&#39;red&#39;=>0x2c, &#39;green&#39;=>0x40 ,&#39;blue&#39;=>0x81),
                  array(&#39;red&#39;=>0x06, &#39;green&#39;=>0x1f ,&#39;blue&#39;=>0x70),
                  array(&#39;red&#39;=>0x14, &#39;green&#39;=>0x37 ,&#39;blue&#39;=>0xad),
                  array(&#39;red&#39;=>0x57, &#39;green&#39;=>0x79 ,&#39;blue&#39;=>0xc0)
          );
 
          //背景图片
          $this->backgroundimage = array(
                 &#39;F:\\city_photo\\city_photo\\1\\1807141.jpg&#39;,
                 &#39;F:\\city_photo\\city_photo\\1\\1807141.jpg&#39;,
                 &#39;F:\\city_photo\\city_photo\\1\\1807141.jpg&#39;,
                 &#39;F:\\city_photo\\city_photo\\1\\1807141.jpg&#39;,
                 &#39;F:\\city_photo\\city_photo\\1\\1807141.jpg&#39;
          );
          //坐标轴X
          $this->x = 100 ;
          //坐标轴Y
          $this->y = 300 ;
          //验证码存放的字段
          $this->checkcodefield = &#39;checkcode&#39;;
        }
 
        /* 创建背景
         *
         * @return $im
         */
        public function createImgae(){
           //$this->im = imagecreatetruecolor($this->w,$this->h);
           $key = array_rand($this->backgroundimage);
           $this->im = imagecreatefromjpeg( $this->backgroundimage[$key]);
 
           return $this->im;
        }
 
        /* 创建背景的索引色
         *
         *  @return
         */
        public function createBackgroundColor(){
           //获取背景随机颜色组key
           $rgbGroupKey = array_rand($this->backgroundcolor);
           $red = $this->backgroundcolor[$rgbGroupKey][&#39;red&#39;];
           $green = $this->backgroundcolor[$rgbGroupKey][&#39;green&#39;];
           $blue = $this->backgroundcolor[$rgbGroupKey][&#39;blue&#39;];
 
           //返回颜色索引
           return imagecolorallocate($this->im, $red, $green, $blue);
        }
 
 
        /* 获取随机字符,并将字符存放发到
         *
         *  @return
         */
        public  function getRandStr(){
           $randChars  = &#39;&#39;;
           for($i = 0 ; $i < $this->length ; $i++){
               $randChars .= $this->text[rand(0,strlen($this->text))] ;
           }
 
           //字体编码统一转换为utf-8
           //$randChars = iconv("GB2312","UTF-8",$randChars);
 
           //写进session
           $_SESSION[$this->checkcodefield] =  $randChars;
 
           return  $randChars;
        }
 
        /* 创建字体颜色索引
         * @return
         */
        public function createFontColor(){
           //获取背景随机颜色组key
           $rgbGroupKey = array_rand($this->fontcolor);
           $red = $this->fontcolor[$rgbGroupKey][&#39;red&#39;];
           $green = $this->fontcolor[$rgbGroupKey][&#39;green&#39;];
           $blue = $this->fontcolor[$rgbGroupKey][&#39;blue&#39;];
 
           //颜色索引
           return  imagecolorallocate($this->im, $red, $green, $blue);;
        }
 
        //添加文字到图片
        public function addTextToImage(){
          //字体颜色
          $fontcolor =$this->createFontColor();
          //字体
          $key = array_rand($this->font);
          $font = $this->font[$key];
 
          //验证码
          $text = $this->getRandStr();
 
          //偏转角度
          $key = array_rand($this->angel);
          $_angle = $this->angel[$key];
 
          //起始坐标
          $x = $this->x;
          $y = $this->y;
 
          //字体大小
          $key = array_rand($this->fontsize);
          $_fontsize = $this->fontsize[$key];
 
          //添加文字到图片
          imagettftext($this->im, $_fontsize , $_angle, $x, $y, $fontcolor, $font, $text);
        }
 
        /**
         * 输出图片
         *
         */
        public function outputImage(){
          //创建布景
          $this->createImgae();
          //颜色添加到布景
          $this->createBackgroundColor();
          //添加文字到图片
          $this->addTextToImage();
 
          //增加过期,30分钟后过期
          $expireTime = date("M, d Y H:i:s",time()+1800);
          header("Expires: {$expireTime} GMT ");
          header("Last-Modified: " . gmdate("D, d M Y H:i:s")." GMT");
          header("Cache-Control: no-store, no-cache, must-revalidate");
          header("Cache-Control: post-check=0, pre-check=0", false);
          header("Pragma: no-cache");
 
          //声明输出文件的类型
          header(&#39;Content-type: image/png&#39;);
          //输出png图片
          imagepng($this->im);
          //释放与此图片关联的内存
          imagedestroy($this->im);
        }
 
    }
 
    $instance = new verifycode;
    $instance->outputImage();


3. [图片] examplle2.jpg 

1184.jpg


以上就是支持背景为调色板和图片文件的验证码类的内容,更多相关内容请关注PHP中文网(www.php.cn)!


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。