复制代码 代码如下:
createimage(); 
*/ 
class captcha{ 
//@定义验证码图片高度 
private $height; 
//@定义验证码图片宽度 
private $width; 
//@定义验证码字符个数 
private $textnum; 
//@定义验证码字符内容 
private $textcontent; 
//@定义字符颜色 
private $fontcolor; 
//@定义随机出的文字颜色 
private $randfontcolor; 
//@定义字体大小 
private $fontsize; 
//@定义字体 
private $fontfamily; 
//@定义背景颜色 
private $bgcolor; 
//@定义随机出的背景颜色 
private $randbgcolor; 
//@定义字符语言 
private $textlang; 
//@定义干扰点数量 
private $noisepoint; 
//@定义干扰线数量 
private $noiseline; 
//@定义是否扭曲 
private $distortion; 
//@定义扭曲图片源 
private $distortionimage; 
//@定义是否有边框 
private $showborder; 
//@定义验证码图片源 
private $image; 
//@constructor 构造函数 
public function captcha(){ 
$this->textnum=4; 
$this->f 
$this->f//设置中文字体,可以改成linux的目录 
$this->textlang='en'; 
$this->noisepoint=30; 
$this->noiseline=3; 
$this->distortion=false; 
$this->showborder=false; 
} 
//@设置图片宽度 
public function setwidth($w){ 
$this->width=$w; 
} 
//@设置图片高度 
public function setheight($h){ 
$this->height=$h; 
} 
//@设置字符个数 
public function settextnumber($textn){ 
$this->textnum=$textn; 
} 
//@设置字符颜色 
public function setfontcolor($fc){ 
$this->f 
} 
//@设置字号 
public function setfontsize($n){ 
$this->f 
} 
//@设置字体 
public function setfontfamily($ffurl){ 
$this->f 
} 
//@设置字符语言 
public function settextlang($lang){ 
$this->textlang=$lang; 
} 
//@设置图片背景 
public function setbgcolor($bc){ 
$this->bgcolor=sscanf($bc,'#%2x%2x%2x'); 
} 
//@设置干扰点数量 
public function setnoisepoint($n){ 
$this->noisepoint=$n; 
} 
//@设置干扰线数量 
public function setnoiseline($n){ 
$this->noiseline=$n; 
} 
//@设置是否扭曲 
public function setdistortion($b){ 
$this->distortion=$b; 
} 
//@设置是否显示边框 
public function setshowborder($border){ 
$this->showborder=$border; 
} 
//@初始化验证码图片 
public function initimage(){ 
if(empty($this->width)){$this->width=floor($this->fontsize*1.3)*$this->textnum+10;} 
if(empty($this->height)){$this->height=$this->fontsize*2;} 
$this->image=imagecreatetruecolor($this->width,$this->height); 
if(empty($this->bgcolor)){ 
$this->randbgcolor=imagecolorallocate($this->image,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255)); 
}else{ 
$this->randbgcolor=imagecolorallocate($this->image,$this->bgcolor[0],$this->bgcolor[1],$this->bgcolor[2]); 
} 
imagefill($this->image,0,0,$this->randbgcolor); 
} 
//@产生随机字符 
public function randtext($type){ 
$string=''; 
switch($type){ 
case 'en': 
$str='abcdefghjklmnpqrstuvwxy3456789'; 
for($i=0;$itextnum;$i++){ 
$string=$string.','.$str[mt_rand(0,29)]; 
} 
break; 
case 'cn': 
for($i=0;$itextnum;$i++) { 
$string=$string.','.chr(rand(0xb0,0xcc)).chr(rand(0xa1,0xbb)); 
} 
$string=iconv('gb2312','utf-8',$string); //转换编码到utf8 
break; 
} 
return substr($string,1); 
} 
//@输出文字到验证码 
public function createtext(){ 
$textarray=explode(',',$this->randtext($this->textlang)); 
$this->textc 
if(empty($this->fontcolor)){ 
$this->randf 
}else{ 
$this->randf 
} 
for($i=0;$itextnum;$i++){ 
$angle=mt_rand(-1,1)*mt_rand(1,20); 
imagettftext($this->image,$this->fontsize,$angle,5+$i*floor($this->fontsize*1.3),floor($this->height*0.75),$this->randfontcolor,$this->fontfamily,$textarray[$i]); 
} 
} 
//@生成干扰点 
public function createnoisepoint(){ 
for($i=0;$inoisepoint;$i++){ 
$pointcolor=imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); 
imagesetpixel($this->image,mt_rand(0,$this->width),mt_rand(0,$this->height),$pointcolor); 
} 
} 
//@产生干扰线 
public function createnoiseline(){ 
for($i=0;$inoiseline;$i++) { 
$linecolor=imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),20); 
imageline($this->image,0,mt_rand(0,$this->width),$this->width,mt_rand(0,$this->height),$linecolor); 
} 
} 
//@扭曲文字 
public function distortiontext(){ 
$this->distorti 
imagefill($this->distortionimage,0,0,$this->randbgcolor); 
for($x=0;$xwidth;$x++){ 
for($y=0;$yheight;$y++){ 
$rgbcolor=imagecolorat($this->image,$x,$y); 
imagesetpixel($this->distortionimage,(int)($x+sin($y/$this->height*2*m_pi-m_pi*0.5)*3),$y,$rgbcolor); 
} 
} 
$this->image=$this->distortionimage; 
} 
//@生成验证码图片 
public function createimage(){ 
$this->initimage(); //创建基本图片 
$this->createtext(); //输出验证码字符 
if($this->distortion){$this->distortiontext();} //扭曲文字 
$this->createnoisepoint(); //产生干扰点 
$this->createnoiseline(); //产生干扰线 
if($this->showborder){imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$this->randfontcolor);} //添加边框 
imagepng($this->image); 
imagedestroy($this->image); 
if($this->distortion){imagedestroy($this->$distortionimage);} 
return $this->textcontent; 
} 
} 
?>使用方法: 
setwidth(200); 
//@设置验证码高度 
//$captcha5->setheight(50); 
//@设置字符个数 
$captcha5->settextnumber(5); 
//@设置字符颜色 
//$captcha5->setfontcolor('#ff9900'); 
//@设置字号大小 
//$captcha5->setfontsize(25); 
//@设置字体 
$captcha5->setfontfamily('c:\windows\fonts\stxingka.ttf'); 
//@设置语言 
$captcha5->settextlang('cn'); 
//@设置背景颜色 
//$captcha5->setbgcolor('#000000'); 
//@设置干扰点数量 
//$captcha5->setnoisepoint(600); 
//@设置干扰线数量 
//$captcha5->setnoiseline(10); 
//@设置是否扭曲 
//$captcha5->setdistortion(true); 
//@设置是否显示边框 
$captcha5->setshowborder(true); 
//输出验证码 
$code=$captcha5->createimage(); 
//$_session['captchacode']['content']=$code; 
//$_session['captchacode']['time']=microtime(); 
?>
以上就介绍了12306验证码 支持中文字母数字、自定义字体php验证码代码,包括了12306验证码方面的内容,希望对php教程有兴趣的朋友有所帮助。
   
 
   