2015-06-17 15:35:26
修改 ThinkPHP 默认验证码类的字体及大小
是否感觉ThinkPHP中的默认生成的验证码太小了?修改验证码的大小后,发现字体大小跟位置 没变化,看上去感觉很不好,下面就来改改吧…
找到ThinkPHP框架目录下的Extend/Library/ORG/Util/Image.class.php类文件打开,找到 buildImageVerify方法,大概在371行开始,用以下代码替换掉以前的(改的东西并不多,只要 加上两行代码就可以了,但为了清楚那两行代码是加在哪贴出buildImageVerify的整个方法)
1 | 
static function buildImageVerify($length=4, $mode=1, $type='png', $width=48, $height=22, $verifyName='verify') { | 
 
2 | 
 import('ORG.Util.String'); | 
 
3 | 
 $randval = String::randString($length, $mode); | 
 
4 | 
 session($verifyName, md5($randval)); | 
 
5 | 
 $width = ($length * 10 + 10) > $width ? $length * 10 + 10 : $width; | 
 
6 | 
 if ($type != 'gif' && function_exists('imagecreatetruecolor')) { | 
 
7 | 
 $im = imagecreatetruecolor($width, $height); | 
 
9 | 
 $im = imagecreate($width, $height); | 
 
11 | 
 $r = Array(225, 255, 255, 223); | 
 
12 | 
 $g = Array(225, 236, 237, 255); | 
 
13 | 
 $b = Array(225, 236, 166, 125); | 
 
16 | 
$backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]); //背景色(随机) | 
 
17 | 
 $borderColor = imagecolorallocate($im, 100, 100, 100); //边框色 | 
 
18 | 
 imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor); | 
 
19 | 
 imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor); | 
 
20 | 
 $stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120)); | 
 
22 | 
 for ($i = 0; $i < 10; $i++) { | 
 
23 | 
 imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $stringColor); | 
 
25 | 
 for ($i = 0; $i < 25; $i++) { | 
 
26 | 
 imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $stringColor); | 
 
28 | 
 $fontPath=THINK_PATH."/Common/ERASDEMI.TTF"; //字体文件路径,可自行更改,我放在ThinkPHP目录下的Common目录里面 | 
 
29 | 
 for ($i = 0; $i < $length; $i++) { | 
 
30 | 
 //imagestring($im, 5, $i * 18 + 5, mt_rand(1, 8), $randval{$i}, $stringColor); //这是默认的 | 
 
31 | 
 imagettftext($im, 20, mt_rand(-30,30) , $i * 16 + 5, $height/1.4, $stringColor, $fontPath, $randval{$i}); //这个是新的,用imagettftext函数 | 
 
33 | 
 Image::output($im, $type); | 
 
 
 
字体下载
前后对效果
    
	更多精彩内容:各种AI课程、技能课程、黑科技软件、网站小程序源码、副业小项目、PPT模板等精品素材、电商课程、推广引流课程等,尽在 天边资源网 。