您好,欢迎访问一九零五行业门户网

基于GD库的php验证码类且支持中英文字体、背景、干扰点线、扭曲等

基于gd库的php验证码类(支持中英文字体、背景、干扰点线、扭曲.......) 
<?php /* * captcha class base on php gd lib * @author design * @version 1.0 * @copyright js8.in 2010 * @demo * include('captchaclass.php'); * $captchademo=new captcha(); * $captchademo->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->fontsize = 16; $this->fontfamily = 'c:\windows\fontssimyou.ttf'; //设置中文字体,可以改成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->fontcolor = sscanf($fc, '#%2x%2x%2x'); } //@设置字号 public function setfontsize($n) { $this->fontsize = $n; } //@设置字体 public function setfontfamily($ffurl) { $this->fontfamily = $ffurl; } //@设置字符语言 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; $i < $this->textnum; $i++) { $string = $string . ',' . $str[mt_rand(0, 29)]; } break; case 'cn': for ($i = 0; $i < $this->textnum; $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->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; $i < $this->textnum; $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; $i < $this->noisepoint; $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; $i < $this->noiseline; $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->distortionimage = imagecreatetruecolor($this->width, $this->height); imagefill($this->distortionimage, 0, 0, $this->randbgcolor); for ($x = 0; $x < $this->width; $x++) { for ($y = 0; $y < $this->height; $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; } } ?>
使用方法:
<?php //session_start(); header("content-type:image/png"); include('captcha5_class.php'); $captcha5 = new captcha(); //@设置验证码宽度 //$captcha5->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(); ?>
其它类似信息

推荐信息