關於thinkphp下驗證碼的問題

在今天做後臺的時候遇到的一個問題,鬱悶了一下午。現在搞好了就簡單總結一個。

首先驗證碼是基於GD庫,也就是說我們必須現開啓php中的GD庫

要使用驗證碼,需要導入擴展類庫中的ORG.Util.Image類庫和ORG.Util.String類庫。我們通過在在模塊類中增加一個verify方法來用於顯示驗證碼:

  1. Public function verify(){

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

  3.    Image::buildImageVerify();

ok,我們直接在模版中調用這個方法

定義完成後,驗證碼的顯示只需要在模板文件中添加:

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

    就到這裏,不乏顯示,爲什麼,官方的說法是: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類

在image你可以看到處理圖片的方法都在這裏面了。找到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('imagecreatetruecolor')) {
          $im = imagecreatetruecolor($width, $height);
      } else {
          $im = imagecreate($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 = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]);    //背景色(隨機)

      $backColor = imagecolorallocate($im, 17, 168, 171);                         //自定義背景顏色
      $borderColor = imagecolorallocate($im, 100, 100, 100);                    //驗證碼邊框色

      imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);  //畫一個矩形
      imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
      $stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));//設定隨機顏色
      $whites=imagecolorallocate($im,255,255,255);//設定隨機顏色
      // 干擾
      for ($i = 0; $i < 2; $i++) {
          imagearc($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++) {
          imagesetpixel($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;
                imagettftext($im, $size, mt_rand(10, 10), $x, $y, $whites, $fontpath, $randval{$i});
              }
        }else{
            for ($i = 0; $i < $length; $i++) {
             imagestring($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);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章