OneThink | 漂亮的验证码 | 移植到ThinkPHP3.1.3

浏览:3093 发布日期:2013/12/23 分类:技术分享 关键字: OneThink 漂亮的验证码 ThinkPHP3.1.3 验证码
OneThink1.0 上的验证码移植到 移植到ThinkPHP3.1.3,只是代码搬家没有技术含量,本人懒惰,有些 OneThink1.0 Verify类封好装的函数我直接拆了贴到Image类里没有封装,有兴趣的10秒钟复制粘帖 写个 static function 装好。需要字体支持,so 拷贝 OneThink1.0 的 Verify 文件夹(\ThinkPHP\Library\Think\Verify) 到 ThinkPHP3.1.3 目录(\ThinkPHP\Extend\Library\ORG\Util\) 下,注意这里下面的代码没有考虑中文验证码,自己修改下就OK了。
  static function buildImageVerify($length=5, $mode=1, $type='png', $imageL=0, $imageH=0, $verifyName='verify',$fontSize=25,$fontttf='') {  // 验证码字体,不设置随机获取  抄袭的 onethink
        $codeSet = '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY';
        $bg      = array(243, 251, 254);  // 背景
        // 图片宽(px)
        $imageL || $imageL = $length*$fontSize*1.5 + $length*$fontSize/2; 
        // 图片高(px)
        $imageH || $imageH = $fontSize * 2.5;
        // 建立一幅 $imageL x $imageH 的图像
        $im = imagecreate($imageL, $imageH); 
        // 设置背景      
        imagecolorallocate($im, $bg[0], $bg[1], $bg[2]); 

        // 验证码字体随机颜色
        $color = imagecolorallocate($im, mt_rand(1,150), mt_rand(1,150), mt_rand(1,150));
        // 验证码使用随机字体
        $ttfPath = dirname(__FILE__) . '/Verify/ttfs/';

        if(empty($fontttf)){
            $dir = dir($ttfPath);
            $ttfs = array();        
            while (false !== ($file = $dir->read())) {
                if($file[0] != '.' && substr($file, -4) == '.ttf') {
                    $ttfs[] = $file;
                }
            }
            $dir->close();
            $fontttf = $ttfs[array_rand($ttfs)];
        } 
        $fontttf = $ttfPath . $fontttf;
        /*
        if($this->useImgBg) {
            $this->_background();
        }
        */
        // 绘杂点
        for($i = 0; $i < 10; $i++){
            //杂点颜色
            $noiseColor = imagecolorallocate($im, mt_rand(150,225), mt_rand(150,225), mt_rand(150,225));
            for($j = 0; $j < 5; $j++) {
                // 绘杂点
                imagestring($im, 5, mt_rand(-10, $imageL),  mt_rand(-10, $imageH), $codeSet[mt_rand(0, 27)], $noiseColor);
            }
        }
        // 绘干扰线
        $px = $py = 0;
        
        // 曲线前部分
        $A = mt_rand(1, $imageH/2);                  // 振幅
        $b = mt_rand(-$imageH/4, $imageH/4);   // Y轴方向偏移量
        $f = mt_rand(-$imageH/4, $imageH/4);   // X轴方向偏移量
        $T = mt_rand($imageH, $imageH*2);  // 周期
        $w = (2* M_PI)/$T;
                        
        $px1 = 0;  // 曲线横坐标起始位置
        $px2 = mt_rand($imageH/2, $imageH * 0.8);  // 曲线横坐标结束位置

        for ($px=$px1; $px<=$px2; $px = $px + 1) {
            if ($w!=0) {
                $py = $A * sin($w*$px + $f)+ $b + $imageH/2;  // y = Asin(ωx+φ) + b
                $i = (int) ($fontSize/5);
                while ($i > 0) {    
                    imagesetpixel($im, $px + $i , $py + $i, $color);  // 这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出(不用这while循环)性能要好很多                
                    $i--;
                }
            }
        }
        
        // 曲线后部分
        $A = mt_rand(1, $imageH/2);                  // 振幅        
        $f = mt_rand(-$imageH/4, $imageH/4);   // X轴方向偏移量
        $T = mt_rand($imageH, $imageH*2);  // 周期
        $w = (2* M_PI)/$T;        
        $b = $py - $A * sin($w*$px + $f) - $imageH/2;
        $px1 = $px2;
        $px2 = $imageH;

        for ($px=$px1; $px<=$px2; $px=$px+ 1) {
            if ($w!=0) {
                $py = $A * sin($w*$px + $f)+ $b + $imageH/2;  // y = Asin(ωx+φ) + b
                $i = (int) ($fontSize/5);
                while ($i > 0) {            
                    imagesetpixel($image, $px + $i, $py + $i, $color);    
                    $i--;
                }
            }
        }
        
        // 绘验证码
        $codeNX = 0; // 验证码第N个字符的左边距
        for ($i = 0; $i<$length; $i++) {
            $code[$i] = $codeSet[mt_rand(0, 51)];
            $codeNX += mt_rand($fontSize*1.2, $fontSize*1.6);
            // 写一个验证码字符
            imagettftext($im, $fontSize, mt_rand(-40, 40), $codeNX, $fontSize*1.6, $color, $fontttf, $code[$i]);
        }
        
        //imagettftext($im, $fontSize, 0, ($imageL - $fontSize*$length*1.2)/3, $fontSize * 1.5, $color, $fontttf, iconv("GB2312","UTF-8", join('', $code)));
        
        // 保存验证码
        $code = strtoupper(implode('', $code));
        session($verifyName, md5($code));
        
                
        header('Cache-Control: private, max-age=0, no-store, no-cache, must-revalidate');
        header('Cache-Control: post-check=0, pre-check=0', false);        
        header('Pragma: no-cache');
        header("content-type: image/png");
    
        // 输出图像
        imagepng($im);
        imagedestroy($im);
 
    }
最佳答案
评论( 相关
后面还有条评论,点击查看>>