里面有两个方法,分别是用内置字体和自定义字体生成验证码。
<?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; $i<=$snow;$i++) {
$color = imagecolorallocate($pic, mt_rand(150, 230), mt_rand(150, 230), mt_rand(150, 230));
imagechar($pic, 1, mt_rand(0, $width), mt_rand(0, $height), "*", $color);
imagecolordeallocate ($pic, $color);
}
//画干扰线
for($i=0; $i<=$line; $i++) {
$x1 = mt_rand(2, $width * 0.2);
$x2 = mt_rand($width * 0.8, $width - 2);
$y1 = mt_rand(2, $height - 2);
$y2 = mt_rand(2, $height - 2);
$color = imagecolorallocate($pic, mt_rand(130, 250), mt_rand(130, 250), mt_rand(130, 250));
imageline($pic, $x1, $y1, $x2, $y2, $color);
imagecolordeallocate ($pic, $color);
}
//画字符
$code = $this->code;
$eachw = $width / strlen($code); //图片依据字符个数分配等份数
$fontwidth = imagefontwidth(5); //取得字体宽度
$fontheight = imagefontheight(5); //取得字体高度
for($i=0; $i<strlen($code);$i++) {
$color = imagecolorallocate($pic, mt_rand(30, 155), mt_rand(30, 155), mt_rand(30, 150));
$x = mt_rand($eachw * $i, $eachw * ($i+1)-$fontwidth);
$y = mt_rand(3, $height-$fontheight);
imagechar($pic, 5, $x, $y, $code{$i}, $color); //水平画字符
imagecolordeallocate ($pic, $color);
}
//输出
ob_start();
ob_clean();
imagepng($pic);
imagedestroy($pic);
}
/**
* 根据自定义字体生成验证码
* @access public
* @param string $font 字符文件, truetype 字体文件,.ttf字体
* @param int $fontweight 字符大小
* @param int $width 图片宽
* @param int $height 图片高
* @param int $snow 背景雪花个数
* @param int $line 干扰线条数
* @param int $padding 图片内边距
*/
public function captchafromfont($font, $fontweight=16, $width=100, $height=30, $snow=80, $line=3, $padding=3){
if(!isset($font)){
return false;
}
$pic = imagecreatetruecolor($width, $height);
$backagecolor = imagecolorallocate($pic, 0xff, 0xff, 0xff);
imagefill($pic, 0, 0, $backagecolor);
imagecolordeallocate ($pic, $backagecolor);
//打雪花
for($i=0; $i<=$snow;$i++) {
$color = imagecolorallocate($pic, mt_rand(150, 230), mt_rand(150, 230), mt_rand(150, 230));
imagechar($pic, 1, mt_rand(0, $width), mt_rand(0, $height), "*", $color);
imagecolordeallocate ($pic, $color);
}
//画干扰线
for($i=0; $i<=$line; $i++) {
$x1 = mt_rand(2, $width * 0.2);
$x2 = mt_rand($width * 0.8, $width - 2);
$y1 = mt_rand(2, $height - 2);
$y2 = mt_rand(2, $height - 2);
$color = imagecolorallocate($pic, mt_rand(130, 250), mt_rand(130, 250), mt_rand(130, 250));
imageline($pic, $x1, $y1, $x2, $y2, $color);
imagecolordeallocate ($pic, $color);
}
//画字符
$code = $this->code;
$eachw = $width / strlen($code); //图片依据字符个数分配等份数
$codearray = str_split($code);
for($i=0; $i<count($codearray); $i++){
//取得字符宽高
$fontbox = imagettfbbox($fontweight, 0, $font, $codearray[$i]);
$fontwidth = $fontbox[2] - $fontbox[0];
$fontheight = $fontbox[1] - $fontbox[7];
$color = imagecolorallocate($pic, mt_rand(30, 155), mt_rand(30, 155), mt_rand(30, 150)); //字符颜色
$angle = mt_rand(-20, 20); //字符角度
if($i==0){
$start = $eachw * $i+$padding;
$end = $eachw * ($i+1)-$fontwidth;
}
elseif($i == count($codearray)){
$start = $eachw * $i;
$end = $eachw * ($i+1)-$fontwidth-$padding;
}
else{
$start = $eachw * $i;
$end = $eachw * ($i+1)-$fontwidth-$padding;
}
$x = $start < $end ? mt_rand($start, $end) : $start;
$y = ($fontheight+$padding) > $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; $i<$len; $i++) {
$code .= $str{mt_rand(0, strlen($str)-1)};
}
$this->code = $code;
return $code;
}
}
以上就是php验证码类的内容。