在今天作後臺的時候遇到的一個問題,鬱悶了一下午。如今搞好了就簡單總結一個。php
首先驗證碼是基於GD庫,也就是說咱們必須現開啓php中的GD庫session
要使用驗證碼,須要導入擴展類庫中的ORG.Util.Image類庫和ORG.Util.String類庫。咱們經過在在模塊類中增長一個verify方法來用於顯示驗證碼:ide
Public function verify(){字體
import('ORG.Util.Image');ui
Image::buildImageVerify();spa
}rest
ok,咱們直接在模版中調用這個方法blog
定義完成後,驗證碼的顯示只須要在模板文件中添加:圖片
<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);