封装PHP验证码类

浏览量:431 | 分类:PHP | 发布日期:2010-03-05

PHP验证码是很常见的应用
下面是一段封装好的类库
方便以后使用
 

总结一下核心函数:
imagecreate 创建图像;
imagecolorallocate  为图像上色
imagerectangle 创建矩形
imagestring 画字符串
imageline 生成干扰线
imagejpeg 生成JPEG格式
imagedestroy 催婚图像

PHP验证码类
  1. <?php
  2. session_start();
  3. class checkImage {
  4.  
  5.     private $config;
  6.     private $im;
  7.     private $str;
  8.  
  9.     function __construct() {
  10.         $this->config['width']      = 50;
  11.         $this->config['height']     = 20;
  12.         $this->config['vcode']      = "vcode";
  13.         $this->config['type']       = "default";
  14.         $this->config['length']     = 4;
  15.         $this->config['interfere']  = 10;
  16.         $this->str['default']       = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  17.         $this->str['string']        = "abcdefghijklmnopqrstuvwxyz";
  18.         $this->str['int']           = "0123456789";
  19.     }
  20.  
  21.     public function init($config=array()){
  22.         if (!empty($config) && is_array($config)){
  23.             foreach($config as $key=>$value){
  24.                 $this->config[$key] =   $value;
  25.             }
  26.         }
  27.     }
  28.  
  29.     public function create(){
  30.         if (!function_exists("imagecreate")){
  31.             return false;
  32.         }
  33.         $this->createImage();
  34.     }
  35.  
  36.     private function createImage(){
  37.         $this->im   =   imagecreate($this->config['width'],$this->config['height']);
  38.         imagecolorallocate($this->im, 255, 255, 255);
  39.  
  40.         $bordercolor=   imagecolorallocate($this->im,0,0,0);
  41.         imagerectangle($this->im,0,0,$this->config['width']-1,$this->config['height']-1,$bordercolor);
  42.  
  43.         $this->createStr();
  44.         $vcode  =   $_SESSION[$this->config['vcode']];
  45.         $fontcolor  =   imagecolorallocate($this->im,46,46,46);
  46.         for($i=0;$i<$this->config['length'];$i++){
  47.             imagestring($this->im,5,$i*10+6,rand(2,5),$vcode[$i],$fontcolor);
  48.         }
  49.  
  50.         $interfere  =   $this->config['interfere'];
  51.         $interfere  =   $interfere>30?"30":$interfere;
  52.         if (!empty($interfere) && $interfere>1){
  53.             for($i=1;$i<$interfere;$i++){
  54.                 $linecolor  =   imagecolorallocate($this->im,rand(0,255),rand(0,255),rand(0,255));
  55.                 $x  =   rand(1,$this->config['width']);
  56.                 $y  =   rand(1,$this->config['height']);
  57.                 $x2 =   rand($x-10,$x+10);
  58.                 $y2 =   rand($y-10,$y+10);
  59.                 imageline($this->im,$x,$y,$x2,$y2,$linecolor);
  60.             }
  61.         }
  62.         header("Pragma:no-cachern");
  63.         header("Cache-Control:no-cachern");
  64.         header("Expires:0rn");
  65.         header("content-type:image/jpegrn");
  66.         imagejpeg($this->im);
  67.         imagedestroy($this->im);
  68.         exit;
  69.     }
  70.  
  71.     private function createStr(){
  72.         if ($this->config['type']=="int"){
  73.             for($i=1;$i<=$this->config['length'];$i++){
  74.                 $vcode  .=  rand(0,9);
  75.             }
  76.             $_SESSION[$this->config['vcode']] = $vcode;
  77.             return true;
  78.         }
  79.         $len    =   strlen($this->str[$this->config['type']]);
  80.         if (!$len){
  81.             $this->config['type'] = "default";
  82.             $this->create_str();
  83.         }
  84.         for($i=1;$i<=$this->config['length'];$i++){
  85.             $offset  =  rand(0,$len-1);
  86.             $vcode  .=  substr($this->str[$this->config['type']],$offset,1);
  87.         }
  88.         $_SESSION[$this->config['vcode']] = $vcode;
  89.         return true;
  90.     }
  91.  
  92. }
  93.  
  94. $v = new checkImage();
  95. //$config['width']  =   50;         //验证码宽
  96. //$config['height'] =   20;         //验证码高
  97. //$config['vcode']  =   "vcode";    //检查验证码时用的SESSION
  98. //$config['type']   =   "int";  //验证码展示的类型default:大写字母,string:小写字母,int:数字
  99. //$config['length'] =   2;          //验证码长度
  100. //$config['interfere']= 10;         //干扰线强度,范围为1-30,0或空为不起用干扰线
  101. //$v->init($config);    //配置
  102. $v->create();
  103.  
  104. ?>
  105.  
  106. <SCRIPT LANGUAGE="JavaScript">
  107.     <!--
  108.         function reloadcode(){
  109.          var d = new Date();
  110.          document.getElementById('safecode').src="./vcode.php?t="+d.toTimeString()
  111.         }
  112.     //-->
  113. </SCRIPT>
  114.  
  115. <img src="vcode.php" id="safecode" onclick="reloadcode()">

 

上一篇: 2010年元宵节

下一篇: 心酸

评论

过客   2010-08-13 20:22:09