关于thinkphp下验证码的问题

在今天作后台的时候遇到的一个问题,郁闷了一下午。如今搞好了就简单总结一个。php

首先验证码是基于GD库,也就是说咱们必须现开启php中的GD库session

要使用验证码,须要导入扩展类库中的ORG.Util.Image类库和ORG.Util.String类库。咱们经过在在模块类中增长一个verify方法来用于显示验证码:ide

  1. Public function verify(){字体

  2.    import('ORG.Util.Image');ui

  3.    Image::buildImageVerify();spa

  4. rest

ok,咱们直接在模版中调用这个方法blog

定义完成后,验证码的显示只须要在模板文件中添加:图片

  1. <img    src='/Public/verify/' />md5

    就到这里,不乏显示,为何,官方的说法是:UTF8文件中的BOM头在做怪。ok

网上有人作了一个清除BOM的php文件,咱们只须要访问一下就行代码以下

<?php
/*清除rom*/
if(isset($_GET['dir'])){
$basedir=$_GET['dir'];
}else{
$basedir = '.';
}
$auto = 1;
checkdir($basedir);
function checkdir($basedir){
if($dh = opendir($basedir)){
while(($file = readdir($dh)) !== false){
if($file != '.' && $file != '..'){
if(!is_dir($basedir."/".$file)){
echo "filename: $basedir/$file ".checkBOM("$basedir/$file")." <br>";
}else{
$dirname = $basedir."/".$file;
checkdir($dirname);
}
}
}//end while
closedir($dh);
}//end if($dh
}//end function
function checkBOM($filename){
global $auto;
$contents = file_get_contents($filename);
$charset[1] = substr($contents, 0, 1);
$charset[2] = substr($contents, 1, 1);
$charset[3] = substr($contents, 2, 1);
if(ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191){
if($auto == 1){
$rest = substr($contents, 3);
rewrite ($filename, $rest);
return "<font color=red>BOM found, automatically removed.</font>";
}else{
return ("<font color=red>BOM found.</font>");
}
}
else return ("BOM Not Found.");
}//end function
function rewrite($filename, $data){
$filenum = fopen($filename, "w");
flock($filenum, LOCK_EX);
fwrite($filenum, $data);
fclose($filenum);
}
?>


一下是我运行的结果

还有记录一个好看一些的验证码类

hinkphp集成了验证码类,但是样式须要修改 默认的太丑了。这时候咱们须要找到 ORG/Util/Image类

在p_w_picpath你能够看处处理图片的方法都在这里面了。找到280多行的buildImageVerify方法  把本来的给注视掉 加上以下代码便可在这以前须要一个字体 位置放在根下的 Public/font/font.ttf没有字体使用默认的验证码字符

以下代码就是 验证码类修改版。

static function buildImageVerify($length=4, $mode=1, $type='png', $width=48, $height=22, $size='20',$verifyName='verify') {
      $fontpath=  dirname(__PATH__).'/Public/font/font.ttf';
      import('ORG.Util.String');//引入字符串类
      $randval = String::rand_string($length, $mode);//随机得到 $length个数字
     $_SESSION[$verifyName] = md5($randval);//把验证码存入session
       $width = ($length * 10 + 10) > $width ? $length * 10 + 10 : $width;//判断若是验证码的宽度小于宽度字符串*10+10
      if ($type != 'gif' && function_exists('p_w_picpathcreatetruecolor')) {
          $im = p_w_picpathcreatetruecolor($width, $height);
      } else {
          $im = p_w_picpathcreate($width, $height);
      }
      $r = Array(225, 255, 255, 223);
      $g = Array(225, 236, 237, 255);
      $b = Array(225, 236, 166, 125);
      $key = mt_rand(0, 3);

      //$backColor = p_w_picpathcolorallocate($im, $r[$key], $g[$key], $b[$key]);    //背景色(随机)

      $backColor = p_w_picpathcolorallocate($im, 17, 168, 171);                         //自定义背景颜色
      $borderColor = p_w_picpathcolorallocate($im, 100, 100, 100);                    //验证码边框色

      p_w_picpathfilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);  //画一个矩形
      p_w_picpathrectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
      $stringColor = p_w_picpathcolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));//设定随机颜色
      $whites=p_w_picpathcolorallocate($im,255,255,255);//设定随机颜色
      // 干扰
      for ($i = 0; $i < 2; $i++) {
          p_w_picpatharc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $stringColor);//增长线条干扰
      }
      for ($i = 0; $i < 1; $i++) {
          p_w_picpathsetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $stringColor);//增长像素干扰

      }

      $y = $height - ($height - $size) / 2;
       if(file_exists($fontpath)){
            for ($i = 0; $i < $length; $i++) {
                $x = $size * $i + $left+10;
                p_w_picpathttftext($im, $size, mt_rand(10, 10), $x, $y, $whites, $fontpath, $randval{$i});
              }
        }else{
            for ($i = 0; $i < $length; $i++) {
             p_w_picpathstring($im, 5, $i * 10 + 5, mt_rand(1, 8), $randval{$i}, $whites);

           }
     }
      Image::output($im, $type);
  }
后台调用的时候设置字体参数便可修改字体大小
 import('ORG.Util.Image');

Image::buildImageVerify($length=4, $mode=1, $type='png', $width=100, $height=30,$size=30);

相关文章
相关标签/搜索