自定义ThinkPHP中文验证码的字体大小
匿名 · 更新于 2013/11/24
首先找到ThinkPHP\Extend\Library\ORG\Util下的Image.class.php,打开此类文件,找到GBVerify函数,代码如下所示:
// 中文验证码
static function GBVerify($length=4, $type='png', $width=180, $height=50, $fontface='simhei.ttf', $verifyName='verify') {
import('ORG.Util.String');
$code = String::randString($length, 4);
$width = ($length * 45) > $width ? $length * 45 : $width;
session($verifyName, md5($code));
$im = imagecreatetruecolor($width, $height);
$borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
$bkcolor = imagecolorallocate($im, 250, 250, 250);
imagefill($im, 0, 0, $bkcolor);
@imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
// 干扰
for ($i = 0; $i < 15; $i++) {
$fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
}
for ($i = 0; $i < 255; $i++) {
$fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $fontcolor);
}
if (!is_file($fontface)) {
$fontface = dirname(__FILE__) . "/" . $fontface;
}
for ($i = 0; $i < $length; $i++) {
$fontcolor = imagecolorallocate($im, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120)); //这样保证随机出来的颜色较深。
$codex = String::msubstr($code, $i, 1);
imagettftext($im, mt_rand(16,20), mt_rand(-60, 60), 40 * $i + 20, mt_rand(30, 35), $fontcolor, $fontface, $codex);
}
Image::output($im, $type);
}
以上第27行代码,
imagettftext($im, mt_rand(16,20), mt_rand(-60, 60), 40 * $i + 20, mt_rand(30, 35), $fontcolor, $fontface, $codex);imagettftext中第二个参数mt_rand(16,20)就是设置字体大小的,这里ThinkPHP用到了一个随机字体大小的范围,即16到24个像素,我们可以直接修改这里,比如改成mt_rand(10,12),这样验证码字体的大小就被限制在10到12像素大小,当然在做项目中,我们不可能每次做验证码需要的字体大小都固定在一个范围,所以我们可以自己改变下ThinkPHP中文验证码方法,也就是为GBVerify函数再加入一个设置字体大小的参数,具体代码实现如下
// 中文验证码
static function GBVerify($length=4, $type='png', $width=180, $height=50, $fontface='simhei.ttf', $verifyName='verify',$fontsize='16,20') {
import('ORG.Util.String');
$fontsize_arr = explode(',',$fontsize); //分割字体大小参数为数组
$code = String::randString($length, 4);
$width = ($length * 45) > $width ? $length * 45 : $width;
session($verifyName, md5($code));
$im = imagecreatetruecolor($width, $height);
$borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
$bkcolor = imagecolorallocate($im, 250, 250, 250);
imagefill($im, 0, 0, $bkcolor);
@imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
// 干扰
for ($i = 0; $i < 15; $i++) {
$fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $fontcolor);
}
for ($i = 0; $i < 255; $i++) {
$fontcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $fontcolor);
}
if (!is_file($fontface)) {
$fontface = dirname(__FILE__) . "/" . $fontface;
}
for ($i = 0; $i < $length; $i++) {
$fontcolor = imagecolorallocate($im, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120)); //这样保证随机出来的颜色较深。
$codex = String::msubstr($code, $i, 1);
imagettftext($im, mt_rand($fontsize_arr[0], $fontsize_arr[1]), mt_rand(-60, 60), 40 * $i + 20, mt_rand(30, 35), $fontcolor, $fontface, $codex);
}
Image::output($im, $type);
}
这样我们可以这样使用ThinkPHP的GBVerify函数
public function verify(){
import('ORG.Util.Image');
Image::GBVerify(2,'png',50,30,'simhei.ttf','verify','10,15');
}
其中末尾参数'10,15'就是我们自定义的中文验证码的字体大小。
