帝国cms验证码美化 简单美化

帝国cms验证码美化 简单美化
帝国cms验证码美化 简单美化

可控制字体(推荐用帝国默认的cour.ttf字体或者其他占位好的字体),文字大小等。加入线和圆弧干扰。

1、复制\e\ShowKey\index.php代码替换原有
2、复制 \e\data\mark\ 下的 cour.ttf 字体到 \e\ShowKey\ 下,即可。

其他细节根据自身需求再调试即可。(验证码图片大小等都可以自定义,背景色字体色,干扰像素颜色都是在帝国后台自身控制)
效果截图:


\e\ShowKey\index.php代码如下(样式1):

<?php
require('../class/connect.php');

//取得随机数
function domake_password($pw_length){
        global $public_r;
        if($public_r['keytog']==1)//字母
        {
                $low_ascii_bound=65;
                $upper_ascii_bound=90;
                $notuse=array(91);
        }
        elseif($public_r['keytog']==2)//数字+字母
        {
                $low_ascii_bound=50;
                $upper_ascii_bound=90;
                $notuse=array(58,59,60,61,62,63,64,73,79);
        }
        else//数字
        {
                $low_ascii_bound=48;
                $upper_ascii_bound=57;
                $notuse=array(58);
        }
        while($i<$pw_length)
        {
                if(PHP_VERSION<'4.2.0')
                {
                        mt_srand((double)microtime()*1000000);
                }
                $randnum=mt_rand($low_ascii_bound,$upper_ascii_bound);
                if(!in_array($randnum,$notuse))
                {
                        $password1=$password1.chr($randnum);
                        $i++;
                }
        }
        return $password1;
}

//返回颜色
function ReturnShowKeyColor($img){
        global $public_r;
        //背景色
        if($public_r['keybgcolor'])
        {
                $bgcr=ToReturnRGB($public_r['keybgcolor']);
                $r['bgcolor']=imagecolorallocate($img,$bgcr[0],$bgcr[1],$bgcr[2]);
        }
        else
        {
                $r['bgcolor']=imagecolorallocate($img,102,102,102);
        }
        //文字色
        if($public_r['keyfontcolor'])
        {
                $fcr=ToReturnRGB($public_r['keyfontcolor']);
                $r['fontcolor']=ImageColorAllocate($img,$fcr[0],$fcr[1],$fcr[2]);
        }
        else
        {
                $r['fontcolor']=ImageColorAllocate($img,255,255,255);
        }
        //干扰色
        if($public_r['keydistcolor'])
        {
                $dcr=ToReturnRGB($public_r['keydistcolor']);
                $r['distcolor']=ImageColorAllocate($img,$dcr[0],$dcr[1],$dcr[2]);
        }
        else
        {
                $r['distcolor']=ImageColorAllocate($img,71,71,71);
        }
        return $r;
}

//显示验证码
function ShowKey($v){
        $vname=ecmsReturnKeyVarname($v);
        $key=strtolower(domake_password(4));
        ecmsSetShowKey($vname,$key);
        //是否支持gd库
        if(function_exists("imagejpeg")) 
        {
                header ("Content-type: image/jpeg");
                $img=imagecreate(72,35);
                $colorr=ReturnShowKeyColor($img);
                $bgcolor=$colorr['bgcolor'];
                $fontcolor=$colorr['fontcolor'];
                $distcolor=$colorr['distcolor'];
                imagefill($img,0,0,$bgcolor);
                //imagestring($img,5,6,3,$key,$fontcolor);
                         $font='cour.ttf';//字体名字
                imagettftext($img,20,rand(-10,10),5,25,$fontcolor,$font,$key);//生成验证码图像
                //干扰像素[线和圆弧]
                $linenums = rand(1, 3);
                for ($x=0; $x <= $linenums; $x++){
                        $linecolor = imagecolorallocatealpha($img, rand(0, 255), rand(0, 255), rand(0, 255), 10);
                        $linecolor2 = imagecolorallocatealpha($img, rand(0, 255), rand(0, 255), rand(0,255), 0);
                        $linex = rand(0, 70);
                        $liney = rand(0, 35);
                        if(rand(0,$x)%2==0){
                                imageline($img, $linex, $liney, $linex + rand(0, 4) - 2, $liney + rand(1, 4) - 2, $linecolor);
                                imagearc($img, rand(1, 70), rand(1, 30), rand(1, 50), rand(1, 5), 0, 150, $linecolor2);
                        }else{
                                imageline($img, $linex, $liney, $linex + rand(0, 4) - 2, $liney + rand(1, 4) - 2, $linecolor2);
                                imagearc($img, rand(1, 70), rand(1, 30), rand(1, 50), rand(1, 5), 0, 200, $linecolor);
                        }
                }
                //加入干扰象素
                for($i=0;$i<90;$i++)
                {
                        imagesetpixel($img,rand()%70,rand()%30,$distcolor);
                }
                imagejpeg($img);
                imagedestroy($img);
        }
        elseif (function_exists("imagepng"))
        {
                header ("Content-type: image/png");
                $img=imagecreate(47,20);
                $colorr=ReturnShowKeyColor($img);
                $bgcolor=$colorr['bgcolor'];
                $fontcolor=$colorr['fontcolor'];
                $distcolor=$colorr['distcolor'];
                imagefill($img,0,0,$bgcolor);
                imagestring($img,5,6,3,$key,$fontcolor);
                for($i=0;$i<90;$i++) //加入干扰象素
                {
                        imagesetpixel($img,rand()%70,rand()%30,$distcolor);
                }
                imagepng($img);
                imagedestroy($img);
        }
        elseif (function_exists("imagegif")) 
        {
                header("Content-type: image/gif");
                $img=imagecreate(47,20);
                $colorr=ReturnShowKeyColor($img);
                $bgcolor=$colorr['bgcolor'];
                $fontcolor=$colorr['fontcolor'];
                $distcolor=$colorr['distcolor'];
                imagefill($img,0,0,$bgcolor);
                imagestring($img,5,6,3,$key,$fontcolor);
                for($i=0;$i<90;$i++) //加入干扰象素
                {
                        imagesetpixel($img,rand()%70,rand()%30,$distcolor);
                }
                imagegif($img);
                imagedestroy($img);
        }
        elseif (function_exists("imagewbmp")) 
        {
                header ("Content-type: image/vnd.wap.wbmp");
                $img=imagecreate(47,20);
                $colorr=ReturnShowKeyColor($img);
                $bgcolor=$colorr['bgcolor'];
                $fontcolor=$colorr['fontcolor'];
                $distcolor=$colorr['distcolor'];
                imagefill($img,0,0,$bgcolor);
                imagestring($img,5,6,3,$key,$fontcolor);
                for($i=0;$i<90;$i++) //加入干扰象素
                {
                        imagesetpixel($img,rand()%70,rand()%30,$distcolor);
                }
                imagewbmp($img);
                imagedestroy($img);
        }
        else
        {
                ecmsSetShowKey($vname,'ecms');
                echo ReadFiletext("../data/images/ecms.jpg");
        }
}

//返回变量名
function ecmsReturnKeyVarname($v){
        if($v=='login')//登陆
        {
                $name='checkloginkey';
        }
        elseif($v=='reg')//注册
        {
                $name='checkregkey';
        }
        elseif($v=='info')//信息
        {
                $name='checkinfokey';
        }
        elseif($v=='spacefb')//空间反馈
        {
                $name='checkspacefbkey';
        }
        elseif($v=='spacegb')//空间留言
        {
                $name='checkspacegbkey';
        }
        elseif($v=='gbook')//留言
        {
                $name='checkgbookkey';
        }
        elseif($v=='feedback')//反馈
        {
                $name='checkfeedbackkey';
        }
        elseif($v=='getpassword')//取回密码
        {
                $name='checkgetpasskey';
        }
        elseif($v=='regsend')//重发激活邮件
        {
                $name='checkregsendkey';
        }
        else//评论pl
        {
                $name='checkplkey';
        }
        return $name;
}

$v=$_GET['v'];
ShowKey($v);
?>

样式2效果图(背景色和文字颜色是随机的,不受帝国后台颜色设定限制):
 

样式2:

<?php
require('../class/connect.php');

//取得随机数
function domake_password($pw_length){
    global $public_r;
    if ($public_r['keytog'] == 1){//字母
        $low_ascii_bound = 65;
        $upper_ascii_bound = 90;
        $notuse = array(91);
    } elseif ($public_r['keytog'] == 2){//数字+字母
        $low_ascii_bound = 50;
        $upper_ascii_bound = 90;
        $notuse = array(58, 59, 60, 61, 62, 63, 64, 73, 79);
    } else{//数字
        $low_ascii_bound = 48;
        $upper_ascii_bound = 57;
        $notuse = array(58);
    }
    while ($i < $pw_length) {
        if (PHP_VERSION < '4.2.0') {
            mt_srand((double)microtime() * 1000000);
        }
        $randnum = mt_rand($low_ascii_bound, $upper_ascii_bound);
        if (!in_array($randnum, $notuse)) {
            $password1 = $password1 . chr($randnum);
            $i++;
        }
    }
    return $password1;
}

//返回颜色
function ReturnShowKeyColor($img){
    global $public_r;
    //背景色
    if ($public_r['keybgcolor']) {
        $bgcr = ToReturnRGB($public_r['keybgcolor']);
        $r['bgcolor'] = imagecolorallocate($img, $bgcr[0], $bgcr[1], $bgcr[2]);
    } else {
        $r['bgcolor'] = imagecolorallocate($img, 102, 102, 102);
    }
    //文字色
    if ($public_r['keyfontcolor']) {
        $fcr = ToReturnRGB($public_r['keyfontcolor']);
        $r['fontcolor'] = ImageColorAllocate($img, $fcr[0], $fcr[1], $fcr[2]);
    } else {
        $r['fontcolor'] = ImageColorAllocate($img, 255, 255, 255);
    }
    //干扰色
    if ($public_r['keydistcolor']) {
        $dcr = ToReturnRGB($public_r['keydistcolor']);
        $r['distcolor'] = ImageColorAllocate($img, $dcr[0], $dcr[1], $dcr[2]);
    } else {
        $r['distcolor'] = ImageColorAllocate($img, 71, 71, 71);
    }
    return $r;
}

//显示验证码
function ShowKey($v){
    $vname = ecmsReturnKeyVarname($v);
    $codelen = 4;//验证码长度
    $width = 120;//宽度
    $height = 50;//高度
    $fontsize = 28;//指定字体大小
    $img = imagecreate($width, $height);//图形资源句柄
    $colorr = ReturnShowKeyColor($img);
    $fontcolor = $colorr['fontcolor'];//指定字体颜色
    $font = ECMS_PATH . 'e/data/mark/elephant.ttf';//字体名字
    $key = strtoupper(domake_password($codelen));//随机因子中的x位字符
    ecmsSetShowKey($vname, $key);
    //是否支持gd库
    if (function_exists("imagejpeg")) {
        header("Content-type: image/jpeg");
        $color = imagecolorallocate($img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
        imagefilledrectangle($img, 0, $height, $width, 0, $color);
        for ($i = 0; $i < 6; $i++) {
            $color = imagecolorallocate($img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
            imageline($img, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $color);
        }
        //雪花
        for ($i = 0; $i < 100; $i++) {
            $color = imagecolorallocate($img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
            imagestring($img, mt_rand(1, 5), mt_rand(0, $width), mt_rand(0, $height), '*', $color);
        }
        for ($i = 0; $i < $codelen; $i++) {
            $fontcolor = imagecolorallocate($img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
            imagettftext($img, $fontsize, 0, $fontsize * $i + $i, $height / 1.3, $fontcolor, $font, $key[$i]);
        }
        //imagettftext($img,25,0,5,35,$fontcolor,$font,$key);//生成验证码图像
        imagejpeg($img);
        imagedestroy($img);
    } elseif (function_exists("imagepng")) {
        header("Content-type: image/png");
        $img = imagecreate(47, 20);
        $colorr = ReturnShowKeyColor($img);
        $bgcolor = $colorr['bgcolor'];
        $fontcolor = $colorr['fontcolor'];
        $distcolor = $colorr['distcolor'];
        imagefill($img, 0, 0, $bgcolor);
        imagestring($img, 5, 6, 3, $key, $fontcolor);
        for ($i = 0; $i < 90; $i++){ //加入干扰象素
            imagesetpixel($img, rand() % 70, rand() % 30, $distcolor);
        }
        imagepng($img);
        imagedestroy($img);
    } elseif (function_exists("imagegif")) {
        header("Content-type: image/gif");
        $img = imagecreate(47, 20);
        $colorr = ReturnShowKeyColor($img);
        $bgcolor = $colorr['bgcolor'];
        $fontcolor = $colorr['fontcolor'];
        $distcolor = $colorr['distcolor'];
        imagefill($img, 0, 0, $bgcolor);
        imagestring($img, 5, 6, 3, $key, $fontcolor);
        for ($i = 0; $i < 90; $i++){ //加入干扰象素
            imagesetpixel($img, rand() % 70, rand() % 30, $distcolor);
        }
        imagegif($img);
        imagedestroy($img);
    } elseif (function_exists("imagewbmp")) {
        header("Content-type: image/vnd.wap.wbmp");
        $img = imagecreate(47, 20);
        $colorr = ReturnShowKeyColor($img);
        $bgcolor = $colorr['bgcolor'];
        $fontcolor = $colorr['fontcolor'];
        $distcolor = $colorr['distcolor'];
        imagefill($img, 0, 0, $bgcolor);
        imagestring($img, 5, 6, 3, $key, $fontcolor);
        for ($i = 0; $i < 90; $i++){ //加入干扰象素
            imagesetpixel($img, rand() % 70, rand() % 30, $distcolor);
        }
        imagewbmp($img);
        imagedestroy($img);
    } else {
        ecmsSetShowKey($vname, 'ecms');
        echo ReadFiletext("../data/images/ecms.jpg");
    }
}

//返回变量名
function ecmsReturnKeyVarname($v){
    if ($v == 'login'){//登陆
        $name = 'checkloginkey';
    } elseif ($v == 'reg'){//注册
        $name = 'checkregkey';
    } elseif ($v == 'info'){//信息
        $name = 'checkinfokey';
    } elseif ($v == 'spacefb'){//空间反馈
        $name = 'checkspacefbkey';
    } elseif ($v == 'spacegb'){//空间留言
        $name = 'checkspacegbkey';
    } elseif ($v == 'gbook'){//留言
        $name = 'checkgbookkey';
    } elseif ($v == 'feedback'){//反馈
        $name = 'checkfeedbackkey';
    } elseif ($v == 'getpassword'){//取回密码
        $name = 'checkgetpasskey';
    } elseif ($v == 'regsend'){//重发激活邮件
        $name = 'checkregsendkey';
    } else{//评论pl
        $name = 'checkplkey';
    }
    return $name;
}

$v = $_GET['v'];
ShowKey($v);
?>

字体打包下载(/e/data/mark/):

mark.rar


延伸阅读:

    无相关信息
标签:

上一篇:帝国CMS后台登录成功又显示“您还未登录”的解决办法

下一篇:帝国cms中关于[!--newsnav--]的问题

留言与评论(共有 0 条评论)
   
验证码: