验证码(数字,字母,数字字母混合,中文)
<?php
/*********************
* securityCode.class.php
* 验证码(数字,字母,数字字母混合,中文)
********************/
class SecurityCode{
private $img_width;
private $img_height;
private $mode;//1全部数字,2全部字母,3数字字母混合,4全部中文
private $text="的一是不了能好都然没日于起还发成事只作当想看文无开手十用主行方又如前所本见经头面公同三已老从动两长知民样现分将外但身些与高意进把法此实回二理美点月明其种声全工己话儿者向情部正名定女问力机给等几很业最间新什打便位因重被走电四第门相次东政海口使教西再平真听世气信北少关并内加化由却代军产入先山五太水万市眼体别处总才场师书比住员九笑性通目华报立马命张活难神数件安表原车白应路期叫死常提感金何更反合放做系计或司利受光王果亲界及今京务制解各任至清物台象记边共风战干接它许八特觉望直服毛林题建南度统色字请交爱让";
private $imageResource;//资源
public function __construct($width,$height,$mode){
$this->init($width,$height,$mode);
}
/*
* @function 初始化参数
* @param $width图片宽度 $height图片高度 $验证码的模式
*/
public function init($width,$height,$mode){
$this->img_width=$width;
$this->img_height=$height;
$this->mode=$mode;
}
public function getSecurityCodeImg(){
header("content-type:image/png");
$this->imageResource=imagecreatetruecolor($this->img_width,$this->img_height);
$white=imagecolorallocate($this->imageResource,255,255,255);
imagefill($this->imageResource,0,0,$white);
//写内容
$this->setContent();
//干扰点
$this->setPixel();
//随机线
$this->setLine();
//椭圆线
$this->setArc();
//以png数据格式绘制图片并输出
imagepng($this->imageResource);
//销毁资源
imagedestroy($this->imageResource);
}
/*
* @function 写内容
*/
public function setContent(){
$codeArr=$this->getCode();
//开启session
session_start();
$_SESSION["SecurityCode"]=implode("",$codeArr);
foreach($codeArr as $key=>$value){
$rantcolor=imagecolorallocate($this->imageResource,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));//因为0-120为深色
$fontsize=mt_rand(13,15);
$angle=mt_rand(-10,10);
$x=$key*intval($this->img_width/4)+mt_rand($fontsize,$fontsize*2);
$y=mt_rand(25,50);
$fontfile="./msyh.ttc";//字体所在的文件路径名
imagefttext($this->imageResource,$fontsize,$angle,$x,$y,$rantcolor,$fontfile,$value);
}
}
/*
* @function 设定干扰元素
*/
public function setPixel(){
$black=imagecolorallocate($this->imageResource,0,0,0);
for($i=0;$i<200;$i++){
imagesetpixel($this->imageResource,mt_rand(0,$this->img_width),mt_rand(0,$this->img_height),$black);
}
}
/*
* @function 设定干扰线
*/
public function setLine(){
$black=imagecolorallocate($this->imageResource,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));
imageline($this->imageResource,mt_rand(0,$this->img_width),mt_rand(0,$this->img_height),mt_rand(0,$this->img_width),mt_rand(0,$this->img_height),$black);
}
/*
* @function 设定干扰椭圆线
*/
public function setArc(){
$white=imagecolorallocate($this->imageResource,0,0,0);
$cx=mt_rand(0,$this->img_width);
$cy=mt_rand(0,$this->img_height);
$w=mt_rand(0,$this->img_width);
$h=mt_rand(0,$this->img_height);
imagearc($this->imageResource,$cx,$cy,$w,$h, 0, 360, $white);
}
/*
* @function 获得验证码数组
* @return array
*/
public function getCode(){
$tmp_arr=array();
if($this->mode==1){
//全部数字的
$tmp_arr=$this->getNumericCode();
}
else if($this->mode==2){
//全部字母
$tmp_arr=$this->getEnCode();
}
else if($this->mode==3){
//数字与字母混合
$tmp_arr=$this->getMixedCode();
}
else if($this->mode==4){
//全部中文
$tmp_arr=$this->getChinaCode();
}
return $tmp_arr;
}
/*
* @function 获得全数字数组
*/
public function getNumericCode(){
$codeArr=array();
for($i=0;$i<4;$i++){
$codeArr[]=mt_rand(0,9);
}
return $codeArr;
}
/*
* @function 获得全字母数组
*/
public function getEnCode(){
$codeArr=array();
for($i=0;$i<4;$i++){
$rant=mt_rand(1,2);
if($rant==1){
$codeArr[]=chr(mt_rand(65,90));
}
else{
$codeArr[]=chr(mt_rand(97,122));
}
}
return $codeArr;
}
/*
* @function 获得字母与数字的混合数组
*/
public function getMixedCode(){
$codeArr=array();
for($i=0;$i<4;$i++){
$rant=mt_rand(1,3);
if($rant==1){
$codeArr[]=mt_rand(0,9);
}
else if($rant==2){
$codeArr[]=chr(mt_rand(65,90));
}
else{
$codeArr[]=chr(mt_rand(97,122));
}
}
return $codeArr;
}
/*
* @function 获得全中文数组
*/
public function getChinaCode(){
//header("content-type:text/html;charset=utf-8");
$str_arr=str_split($this->text,3);//因为utf8里中文占3个字节
$codeArr=array();
for($i=0;$i<4;$i++){
$rant=mt_rand(0,count($str_arr)-1);
$codeArr[]=$str_arr[$rant];
}
return $codeArr;
}
}
$sc=new SecurityCode(200,60,4);
$sc->getSecurityCodeImg();
?>不足的地方是,无法将椭圆圆弧设定在一个范围内,衍生出的是字符集的不同ASCII、GBK、Unicode、UTF8的使用环境。