thinkphp很好用,这里就不喷了,使用他来做网站框架进行开发,可以非常迅速,而且代码模块化和易于修改,跑了,还是说正事,在使用验证码类的时候发现原版的验证码,他谬的背景大小可以改,字体还是那么大,怎么都改不了啊,跟着教程看到源码(Extend/Library/ORG/Util/Image.class.php),buildImageVerify这个方法里面在将文字写到图像上时使用的方法为imagestring,搜了一下发现他是没法改大小的,唉,又搜了一下,发现imagettftext这个方法是可以改变字体,同时可以改变文字大小,为了兼容,没有加任何参数,你原来的代码都不用改,当然你也可以自己加入参数,以方便调用。。啥都不说了啊,先看效果,然后真接上源码。。
Image::buildImageVerify(4, 1, 'png', 100, 31); // 100宽,31高时大小为
Image::buildImageVerify(4, 1, 'png', 200, 100); // 200宽,100高时大小为
/**
* 生成图像验证码
* @static
* @access public
* @param string $length 位数
* @param string $mode 类型
* @param string $type 图像格式
* @param string $width 宽度
* @param string $height 高度
* @return string
*/
static function buildImageVerify($length=4, $mode=1, $type='png', $width=48, $height=22, $verifyName='verify') {
import('ORG.Util.String');
$font_file = THINK_PATH . '/../Public/font/elephant.ttf'; // 要修改字体的位置
$font_size = $height * 0.5; // 字体大小,为整个验证码高度的一半左右
$randval = String::randString($length, $mode);
session($verifyName, md5($randval));
$width = ($length * 10 + 10) > $width ? $length * 10 + 10 : $width;
if ($type != 'gif' && function_exists('imagecreatetruecolor')) {
$im = imagecreatetruecolor($width, $height);
} else {
$im = imagecreate($width, $height);
}
$r = Array(225, 255, 255, 223);
$g = Array(225, 236, 237, 255);
$b = Array(225, 236, 166, 125);
$key = mt_rand(0, 3);
$backColor = imagecolorallocate($im, $r[$key], $g[$key], $b[$key]); //背景色(随机)
$borderColor = imagecolorallocate($im, 100, 100, 100); //边框色
imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
imagerectangle($im, 0, 0, $width - 1, $height - 1, $borderColor);
$stringColor = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));
// 干扰
for ($i = 0; $i < 10; $i++) {
imagearc($im, mt_rand(-10, $width), mt_rand(-10, $height), mt_rand(30, 300), mt_rand(20, 200), 55, 44, $stringColor);
}
for ($i = 0; $i < 25; $i++) {
imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $stringColor);
}
for ($i = 0; $i < $length; $i++) {
//imagestring($im, 5, $i * 10 + 5, mt_rand(1, 8), $randval{$i}, $stringColor);
// 将原来的方法更改为如下方法,同时调整4个字符位置,使其大致位于中间部分,易于查看
imagettftext($im, $font_size, 0, $i * $width * 0.25, mt_rand($height * 0.7, $height * 0.9), $stringColor, $font_file, $randval[$i]);
}
Image::output($im, $type);
}