PHPIN.NET

 找回密码
 立即注册
查看: 9514|回复: 0

[7.X版] 帝国cms验证码美化 简单美化

[复制链接]

469

主题

31

回帖

5509

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
5509
发表于 2015-2-4 09:04:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

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

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

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

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

\e\ShowKey\index.php代码如下(样式1):
  1. <?php
  2. require('../class/connect.php');

  3. //取得随机数
  4. function domake_password($pw_length){
  5.         global $public_r;
  6.         if($public_r['keytog']==1)//字母
  7.         {
  8.                 $low_ascii_bound=65;
  9.                 $upper_ascii_bound=90;
  10.                 $notuse=array(91);
  11.         }
  12.         elseif($public_r['keytog']==2)//数字+字母
  13.         {
  14.                 $low_ascii_bound=50;
  15.                 $upper_ascii_bound=90;
  16.                 $notuse=array(58,59,60,61,62,63,64,73,79);
  17.         }
  18.         else//数字
  19.         {
  20.                 $low_ascii_bound=48;
  21.                 $upper_ascii_bound=57;
  22.                 $notuse=array(58);
  23.         }
  24.         while($i<$pw_length)
  25.         {
  26.                 if(PHP_VERSION<'4.2.0')
  27.                 {
  28.                         mt_srand((double)microtime()*1000000);
  29.                 }
  30.                 $randnum=mt_rand($low_ascii_bound,$upper_ascii_bound);
  31.                 if(!in_array($randnum,$notuse))
  32.                 {
  33.                         $password1=$password1.chr($randnum);
  34.                         $i++;
  35.                 }
  36.         }
  37.         return $password1;
  38. }

  39. //返回颜色
  40. function ReturnShowKeyColor($img){
  41.         global $public_r;
  42.         //背景色
  43.         if($public_r['keybgcolor'])
  44.         {
  45.                 $bgcr=ToReturnRGB($public_r['keybgcolor']);
  46.                 $r['bgcolor']=imagecolorallocate($img,$bgcr[0],$bgcr[1],$bgcr[2]);
  47.         }
  48.         else
  49.         {
  50.                 $r['bgcolor']=imagecolorallocate($img,102,102,102);
  51.         }
  52.         //文字色
  53.         if($public_r['keyfontcolor'])
  54.         {
  55.                 $fcr=ToReturnRGB($public_r['keyfontcolor']);
  56.                 $r['fontcolor']=ImageColorAllocate($img,$fcr[0],$fcr[1],$fcr[2]);
  57.         }
  58.         else
  59.         {
  60.                 $r['fontcolor']=ImageColorAllocate($img,255,255,255);
  61.         }
  62.         //干扰色
  63.         if($public_r['keydistcolor'])
  64.         {
  65.                 $dcr=ToReturnRGB($public_r['keydistcolor']);
  66.                 $r['distcolor']=ImageColorAllocate($img,$dcr[0],$dcr[1],$dcr[2]);
  67.         }
  68.         else
  69.         {
  70.                 $r['distcolor']=ImageColorAllocate($img,71,71,71);
  71.         }
  72.         return $r;
  73. }

  74. //显示验证码
  75. function ShowKey($v){
  76.         $vname=ecmsReturnKeyVarname($v);
  77.         $key=strtolower(domake_password(4));
  78.         ecmsSetShowKey($vname,$key);
  79.         //是否支持gd库
  80.         if(function_exists("imagejpeg"))
  81.         {
  82.                 header ("Content-type: image/jpeg");
  83.                 $img=imagecreate(72,35);
  84.                 $colorr=ReturnShowKeyColor($img);
  85.                 $bgcolor=$colorr['bgcolor'];
  86.                 $fontcolor=$colorr['fontcolor'];
  87.                 $distcolor=$colorr['distcolor'];
  88.                 imagefill($img,0,0,$bgcolor);
  89.                 //imagestring($img,5,6,3,$key,$fontcolor);
  90.                          $font='cour.ttf';//字体名字
  91.                 imagettftext($img,20,rand(-10,10),5,25,$fontcolor,$font,$key);//生成验证码图像
  92.                 //干扰像素[线和圆弧]
  93.                 $linenums = rand(1, 3);
  94.                 for ($x=0; $x <= $linenums; $x++){
  95.                         $linecolor = imagecolorallocatealpha($img, rand(0, 255), rand(0, 255), rand(0, 255), 10);
  96.                         $linecolor2 = imagecolorallocatealpha($img, rand(0, 255), rand(0, 255), rand(0,255), 0);
  97.                         $linex = rand(0, 70);
  98.                         $liney = rand(0, 35);
  99.                         if(rand(0,$x)%2==0){
  100.                                 imageline($img, $linex, $liney, $linex + rand(0, 4) - 2, $liney + rand(1, 4) - 2, $linecolor);
  101.                                 imagearc($img, rand(1, 70), rand(1, 30), rand(1, 50), rand(1, 5), 0, 150, $linecolor2);
  102.                         }else{
  103.                                 imageline($img, $linex, $liney, $linex + rand(0, 4) - 2, $liney + rand(1, 4) - 2, $linecolor2);
  104.                                 imagearc($img, rand(1, 70), rand(1, 30), rand(1, 50), rand(1, 5), 0, 200, $linecolor);
  105.                         }
  106.                 }
  107.                 //加入干扰象素
  108.                 for($i=0;$i<90;$i++)
  109.                 {
  110.                         imagesetpixel($img,rand()%70,rand()%30,$distcolor);
  111.                 }
  112.                 imagejpeg($img);
  113.                 imagedestroy($img);
  114.         }
  115.         elseif (function_exists("imagepng"))
  116.         {
  117.                 header ("Content-type: image/png");
  118.                 $img=imagecreate(47,20);
  119.                 $colorr=ReturnShowKeyColor($img);
  120.                 $bgcolor=$colorr['bgcolor'];
  121.                 $fontcolor=$colorr['fontcolor'];
  122.                 $distcolor=$colorr['distcolor'];
  123.                 imagefill($img,0,0,$bgcolor);
  124.                 imagestring($img,5,6,3,$key,$fontcolor);
  125.                 for($i=0;$i<90;$i++) //加入干扰象素
  126.                 {
  127.                         imagesetpixel($img,rand()%70,rand()%30,$distcolor);
  128.                 }
  129.                 imagepng($img);
  130.                 imagedestroy($img);
  131.         }
  132.         elseif (function_exists("imagegif"))
  133.         {
  134.                 header("Content-type: image/gif");
  135.                 $img=imagecreate(47,20);
  136.                 $colorr=ReturnShowKeyColor($img);
  137.                 $bgcolor=$colorr['bgcolor'];
  138.                 $fontcolor=$colorr['fontcolor'];
  139.                 $distcolor=$colorr['distcolor'];
  140.                 imagefill($img,0,0,$bgcolor);
  141.                 imagestring($img,5,6,3,$key,$fontcolor);
  142.                 for($i=0;$i<90;$i++) //加入干扰象素
  143.                 {
  144.                         imagesetpixel($img,rand()%70,rand()%30,$distcolor);
  145.                 }
  146.                 imagegif($img);
  147.                 imagedestroy($img);
  148.         }
  149.         elseif (function_exists("imagewbmp"))
  150.         {
  151.                 header ("Content-type: image/vnd.wap.wbmp");
  152.                 $img=imagecreate(47,20);
  153.                 $colorr=ReturnShowKeyColor($img);
  154.                 $bgcolor=$colorr['bgcolor'];
  155.                 $fontcolor=$colorr['fontcolor'];
  156.                 $distcolor=$colorr['distcolor'];
  157.                 imagefill($img,0,0,$bgcolor);
  158.                 imagestring($img,5,6,3,$key,$fontcolor);
  159.                 for($i=0;$i<90;$i++) //加入干扰象素
  160.                 {
  161.                         imagesetpixel($img,rand()%70,rand()%30,$distcolor);
  162.                 }
  163.                 imagewbmp($img);
  164.                 imagedestroy($img);
  165.         }
  166.         else
  167.         {
  168.                 ecmsSetShowKey($vname,'ecms');
  169.                 echo ReadFiletext("../data/images/ecms.jpg");
  170.         }
  171. }

  172. //返回变量名
  173. function ecmsReturnKeyVarname($v){
  174.         if($v=='login')//登陆
  175.         {
  176.                 $name='checkloginkey';
  177.         }
  178.         elseif($v=='reg')//注册
  179.         {
  180.                 $name='checkregkey';
  181.         }
  182.         elseif($v=='info')//信息
  183.         {
  184.                 $name='checkinfokey';
  185.         }
  186.         elseif($v=='spacefb')//空间反馈
  187.         {
  188.                 $name='checkspacefbkey';
  189.         }
  190.         elseif($v=='spacegb')//空间留言
  191.         {
  192.                 $name='checkspacegbkey';
  193.         }
  194.         elseif($v=='gbook')//留言
  195.         {
  196.                 $name='checkgbookkey';
  197.         }
  198.         elseif($v=='feedback')//反馈
  199.         {
  200.                 $name='checkfeedbackkey';
  201.         }
  202.         elseif($v=='getpassword')//取回密码
  203.         {
  204.                 $name='checkgetpasskey';
  205.         }
  206.         elseif($v=='regsend')//重发激活邮件
  207.         {
  208.                 $name='checkregsendkey';
  209.         }
  210.         else//评论pl
  211.         {
  212.                 $name='checkplkey';
  213.         }
  214.         return $name;
  215. }

  216. $v=$_GET['v'];
  217. ShowKey($v);
  218. ?>
复制代码


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

样式2:
  1. <?php
  2. require('../class/connect.php');

  3. //取得随机数
  4. function domake_password($pw_length){
  5.     global $public_r;
  6.     if ($public_r['keytog'] == 1){//字母
  7.         $low_ascii_bound = 65;
  8.         $upper_ascii_bound = 90;
  9.         $notuse = array(91);
  10.     } elseif ($public_r['keytog'] == 2){//数字+字母
  11.         $low_ascii_bound = 50;
  12.         $upper_ascii_bound = 90;
  13.         $notuse = array(58, 59, 60, 61, 62, 63, 64, 73, 79);
  14.     } else{//数字
  15.         $low_ascii_bound = 48;
  16.         $upper_ascii_bound = 57;
  17.         $notuse = array(58);
  18.     }
  19.     while ($i < $pw_length) {
  20.         if (PHP_VERSION < '4.2.0') {
  21.             mt_srand((double)microtime() * 1000000);
  22.         }
  23.         $randnum = mt_rand($low_ascii_bound, $upper_ascii_bound);
  24.         if (!in_array($randnum, $notuse)) {
  25.             $password1 = $password1 . chr($randnum);
  26.             $i++;
  27.         }
  28.     }
  29.     return $password1;
  30. }

  31. //返回颜色
  32. function ReturnShowKeyColor($img){
  33.     global $public_r;
  34.     //背景色
  35.     if ($public_r['keybgcolor']) {
  36.         $bgcr = ToReturnRGB($public_r['keybgcolor']);
  37.         $r['bgcolor'] = imagecolorallocate($img, $bgcr[0], $bgcr[1], $bgcr[2]);
  38.     } else {
  39.         $r['bgcolor'] = imagecolorallocate($img, 102, 102, 102);
  40.     }
  41.     //文字色
  42.     if ($public_r['keyfontcolor']) {
  43.         $fcr = ToReturnRGB($public_r['keyfontcolor']);
  44.         $r['fontcolor'] = ImageColorAllocate($img, $fcr[0], $fcr[1], $fcr[2]);
  45.     } else {
  46.         $r['fontcolor'] = ImageColorAllocate($img, 255, 255, 255);
  47.     }
  48.     //干扰色
  49.     if ($public_r['keydistcolor']) {
  50.         $dcr = ToReturnRGB($public_r['keydistcolor']);
  51.         $r['distcolor'] = ImageColorAllocate($img, $dcr[0], $dcr[1], $dcr[2]);
  52.     } else {
  53.         $r['distcolor'] = ImageColorAllocate($img, 71, 71, 71);
  54.     }
  55.     return $r;
  56. }

  57. //显示验证码
  58. function ShowKey($v){
  59.     $vname = ecmsReturnKeyVarname($v);
  60.     $codelen = 4;//验证码长度
  61.     $width = 120;//宽度
  62.     $height = 50;//高度
  63.     $fontsize = 28;//指定字体大小
  64.     $img = imagecreate($width, $height);//图形资源句柄
  65.     $colorr = ReturnShowKeyColor($img);
  66.     $fontcolor = $colorr['fontcolor'];//指定字体颜色
  67.     $font = ECMS_PATH . 'e/data/mark/elephant.ttf';//字体名字
  68.     $key = strtoupper(domake_password($codelen));//随机因子中的x位字符
  69.     ecmsSetShowKey($vname, $key);
  70.     //是否支持gd库
  71.     if (function_exists("imagejpeg")) {
  72.         header("Content-type: image/jpeg");
  73.         $color = imagecolorallocate($img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
  74.         imagefilledrectangle($img, 0, $height, $width, 0, $color);
  75.         for ($i = 0; $i < 6; $i++) {
  76.             $color = imagecolorallocate($img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
  77.             imageline($img, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $color);
  78.         }
  79.         //雪花
  80.         for ($i = 0; $i < 100; $i++) {
  81.             $color = imagecolorallocate($img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
  82.             imagestring($img, mt_rand(1, 5), mt_rand(0, $width), mt_rand(0, $height), '*', $color);
  83.         }
  84.         for ($i = 0; $i < $codelen; $i++) {
  85.             $fontcolor = imagecolorallocate($img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
  86.             imagettftext($img, $fontsize, 0, $fontsize * $i + $i, $height / 1.3, $fontcolor, $font, $key[$i]);
  87.         }
  88.         //imagettftext($img,25,0,5,35,$fontcolor,$font,$key);//生成验证码图像
  89.         imagejpeg($img);
  90.         imagedestroy($img);
  91.     } elseif (function_exists("imagepng")) {
  92.         header("Content-type: image/png");
  93.         $img = imagecreate(47, 20);
  94.         $colorr = ReturnShowKeyColor($img);
  95.         $bgcolor = $colorr['bgcolor'];
  96.         $fontcolor = $colorr['fontcolor'];
  97.         $distcolor = $colorr['distcolor'];
  98.         imagefill($img, 0, 0, $bgcolor);
  99.         imagestring($img, 5, 6, 3, $key, $fontcolor);
  100.         for ($i = 0; $i < 90; $i++){ //加入干扰象素
  101.             imagesetpixel($img, rand() % 70, rand() % 30, $distcolor);
  102.         }
  103.         imagepng($img);
  104.         imagedestroy($img);
  105.     } elseif (function_exists("imagegif")) {
  106.         header("Content-type: image/gif");
  107.         $img = imagecreate(47, 20);
  108.         $colorr = ReturnShowKeyColor($img);
  109.         $bgcolor = $colorr['bgcolor'];
  110.         $fontcolor = $colorr['fontcolor'];
  111.         $distcolor = $colorr['distcolor'];
  112.         imagefill($img, 0, 0, $bgcolor);
  113.         imagestring($img, 5, 6, 3, $key, $fontcolor);
  114.         for ($i = 0; $i < 90; $i++){ //加入干扰象素
  115.             imagesetpixel($img, rand() % 70, rand() % 30, $distcolor);
  116.         }
  117.         imagegif($img);
  118.         imagedestroy($img);
  119.     } elseif (function_exists("imagewbmp")) {
  120.         header("Content-type: image/vnd.wap.wbmp");
  121.         $img = imagecreate(47, 20);
  122.         $colorr = ReturnShowKeyColor($img);
  123.         $bgcolor = $colorr['bgcolor'];
  124.         $fontcolor = $colorr['fontcolor'];
  125.         $distcolor = $colorr['distcolor'];
  126.         imagefill($img, 0, 0, $bgcolor);
  127.         imagestring($img, 5, 6, 3, $key, $fontcolor);
  128.         for ($i = 0; $i < 90; $i++){ //加入干扰象素
  129.             imagesetpixel($img, rand() % 70, rand() % 30, $distcolor);
  130.         }
  131.         imagewbmp($img);
  132.         imagedestroy($img);
  133.     } else {
  134.         ecmsSetShowKey($vname, 'ecms');
  135.         echo ReadFiletext("../data/images/ecms.jpg");
  136.     }
  137. }

  138. //返回变量名
  139. function ecmsReturnKeyVarname($v){
  140.     if ($v == 'login'){//登陆
  141.         $name = 'checkloginkey';
  142.     } elseif ($v == 'reg'){//注册
  143.         $name = 'checkregkey';
  144.     } elseif ($v == 'info'){//信息
  145.         $name = 'checkinfokey';
  146.     } elseif ($v == 'spacefb'){//空间反馈
  147.         $name = 'checkspacefbkey';
  148.     } elseif ($v == 'spacegb'){//空间留言
  149.         $name = 'checkspacegbkey';
  150.     } elseif ($v == 'gbook'){//留言
  151.         $name = 'checkgbookkey';
  152.     } elseif ($v == 'feedback'){//反馈
  153.         $name = 'checkfeedbackkey';
  154.     } elseif ($v == 'getpassword'){//取回密码
  155.         $name = 'checkgetpasskey';
  156.     } elseif ($v == 'regsend'){//重发激活邮件
  157.         $name = 'checkregsendkey';
  158.     } else{//评论pl
  159.         $name = 'checkplkey';
  160.     }
  161.     return $name;
  162. }

  163. $v = $_GET['v'];
  164. ShowKey($v);
  165. ?>
复制代码


字体打包下载(/e/data/mark/):
mark.rar (123.3 KB, 下载次数: 528)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|PHPIN.NET ( 冀ICP备12000898号-14 )|网站地图

GMT+8, 2024-4-20 19:59

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表