php+验证码设置字体,转载 支持中文字母数字、自定义字体php验证码程序

class Captcha{

[email protected]

private $height;

[email protected]

private $width;

[email protected]

private $textNum;

[email protected]

private $textContent;

[email protected]

private $fontColor;

[email protected]

private $randFontColor;

[email protected]

private $fontSize;

[email protected]

private $fontFamily;

[email protected]

private $bgColor;

[email protected]

private $randBgColor;

[email protected]

private $textLang;

[email protected]

private $noisePoint;

[email protected]

private $noiseLine;

[email protected]

private $distortion;

[email protected]

private $distortionImage;

[email protected]

private $showBorder;

[email protected]

private $image;

[email protected] 构造函数

public function Captcha(){

$this->textNum=4;

$this->fontSize=16;

$this->fontFamily=‘c:\windows\fontsSIMYOU.ttf‘;//设置中文字体,可以改成linux的目录

$this->textLang=‘en‘;

$this->noisePoint=30;

$this->noiseLine=3;

$this->distortion=false;

$this->showBorder=false;

}

[email protected]

public function setWidth($w){

$this->width=$w;

}

[email protected]

public function setHeight($h){

$this->height=$h;

}

[email protected]

public function setTextNumber($textN){

$this->textNum=$textN;

}

[email protected]

public function setFontColor($fc){

$this->fontColor=sscanf($fc,‘#%2x%2x%2x‘);

}

[email protected]

public function setFontSize($n){

$this->fontSize=$n;

}

[email protected]

public function setFontFamily($ffUrl){

$this->fontFamily=$ffUrl;

}

[email protected]

public function setTextLang($lang){

$this->textLang=$lang;

}

[email protected]

public function setBgColor($bc){

$this->bgColor=sscanf($bc,‘#%2x%2x%2x‘);

}

[email protected]

public function setNoisePoint($n){

$this->noisePoint=$n;

}

[email protected]

public function setNoiseLine($n){

$this->noiseLine=$n;

}

[email protected]

public function setDistortion($b){

$this->distortion=$b;

}

[email protected]

public function setShowBorder($border){

$this->showBorder=$border;

}

[email protected]

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);

}

[email protected]

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);

}

[email protected]

public function createText(){

$textArray=explode(‘,‘,$this->randText($this->textLang));

$this->textContent=join(‘‘,$textArray);

if(empty($this->fontColor)){

$this->randFontColor=imagecolorallocate($this->image,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));

}else{

$this->randFontColor=imagecolorallocate($this->image,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);

}

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]);

}

}

[email protected]

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);

}

}

[email protected]

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);

}

}

[email protected]

public function distortionText(){

$this->distortionImage=imagecreatetruecolor($this->width,$this->height);

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;

}

[email protected]

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;

}

}

?>使用方法:

//session_start();

header("Content-type:image/png");

include(‘captcha5_class.php‘);

$captcha5=new Captcha();

[email protected]

//$captcha5->setWidth(200);

[email protected]

//$captcha5->setHeight(50);

[email protected]

$captcha5->setTextNumber(5);

[email protected]

//$captcha5->setFontColor(‘#ff9900‘);

[email protected]

//$captcha5->setFontSize(25);

[email protected](这里的字体 使用前确保是有的)

$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();

?>

转载的类 测试的 先去掉session

本文转载自:php开发

原文:http://www.cnblogs.com/havoe/p/4375334.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值