里面有两个方法,分别是用内置字体和自定义字体生成验证码。
<?php /**
* 验证码生成类
* @example
* $pic = new uImage();
* $code = $pic->getVerifyCode();
* header("Content-type:image/png");
* $pic->captchaFromFont($font='RAVIE.TTF'); or $pic->captcha();
*/
class uImage {
/**
* 验证码字符
* @access protected
*/
protected $code;
/**
* 生成图片验证码,直接输出的是图片,字体大小是内置字体,最大是5
* @access public
* @param int $width 验证码图片宽度
* @param int $height 验证码图片的高度
* @param int $snow 背景雪花的数量
* @param int $line 干扰线的条数
*/
public function captcha($width=100, $height=30, $snow=80, $line=3) {
$pic = imagecreatetruecolor($width, $height);
$backageColor = imagecolorallocate($pic, 0xFF, 0xFF, 0xFF);
imagefill($pic, 0, 0, $backageColor);
//打雪花
for($i=0; $icode;
$eachW = $width / strlen($code); //图片依据字符个数分配等份数
$fontWidth = imagefontwidth(5); //取得字体宽度
$fontHeight = imagefontheight(5); //取得字体高度
for($i=0; $i<strlen>code;
$eachW = $width / strlen($code); //图片依据字符个数分配等份数
$codeArray = str_split($code);
for($i=0; $i<count> $height ? $padding : mt_rand($fontHeight+$padding, $height-$padding);
imagettftext($pic, $fontWeight, $angle, $x, $y, $color, $font, $codeArray[$i]); //用 TrueType 字体向图像写入文本
imagecolordeallocate ($pic, $color);
}
//输出
ob_start();
ob_clean();
imagepng($pic);
imagedestroy($pic);
}
/**
* 获取验证码
* @access public
* @param int $len 验证码字符的长度
* @return strint 生成的验证码字符
*/
public function getVerifyCode($len=4){
if(!isset($this->code)){
$this->code = $this->getCode($len);
}
return $this->code;
}
/**
* 生成验证码
* @access protected
* @param int $len 验证码字符的长度
* @return strint 生成的验证码字符
*/
protected function getCode($len) {
$str = "23456789abcdefghijklmnqrstuvwxyz";
$code = "";
for($i=0; $icode = $code;
return $code;
}
}</count></strlen>
以上就是PHP验证码类的内容,更多相关内容请关注PHP中文网(www.php.cn)!
php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!











