VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • 支持中文字母数字、自定义字体php验证码程序

验证码常用于登陆页面、留言页面、注册页面,验证码的原理很简单:利用GD库创建一个图片,图片当然要加上必要的干扰码,然后在服务器端存入SESSION,等用户提交的时候判断session是否相同.

支持中文字母数字、自定义字体php验证码程序代码如下:

  1. <?php 
  2. /* 
  3. * Captcha Class base on PHP GD Lib 
  4. * @author Design 
  5. * @version 1.0 
  6. * @copyright js8.in 2010 
  7. * @demo 
  8. * include('captchaClass.php'); 
  9. * $captchaDemo=new Captcha(); 
  10. * $captchaDemo->createImage(); 
  11. */ 
  12. class Captcha{ 
  13.  //@定义验证码图片高度 
  14.  private $height; 
  15.  //@定义验证码图片宽度 
  16.  private $width; 
  17.  //@定义验证码字符个数 
  18.  private $textNum; 
  19.  //@定义验证码字符内容 
  20.  private $textContent; 
  21.  //@定义字符颜色 
  22.  private $fontColor; 
  23.  //@定义随机出的文字颜色 
  24.  private $randFontColor; 
  25.  //@定义字体大小 
  26.  private $fontSize; 
  27.  //@定义字体 
  28.  private $fontFamily; 
  29.  //@定义背景颜色 
  30.  private $bgColor; 
  31.  //@定义随机出的背景颜色 
  32.  private $randBgColor; 
  33.  //@定义字符语言 
  34.  private $textLang; 
  35.  //@定义干扰点数量 
  36.  private $noisePoint; 
  37.  //@定义干扰线数量 
  38.  private $noiseLine; 
  39.  //@定义是否扭曲 
  40.  private $distortion; 
  41.  //@定义扭曲图片源 
  42.  private $distortionImage; 
  43.  //@定义是否有边框 
  44.  private $showBorder; 
  45.  //@定义验证码图片源 
  46.  private $image; 
  47.   
  48.  //@Constructor 构造函数 
  49.  public function Captcha(){ 
  50.  $this->textNum=4; 
  51.  $this->fontSize=16; 
  52.  $this->fontFamily='c:\windows\fontsSIMYOU.ttf';//设置中文字体,可以改成linux的目录 
  53.  $this->textLang='en'; 
  54.  $this->noisePoint=30; 
  55.  $this->noiseLine=3; 
  56.  $this->distortion=false; 
  57.  $this->showBorder=false; 
  58.  } 
  59.  
  60.  
  61.   
  62.  //@设置图片宽度 
  63.  public function setWidth($w){ 
  64.  $this->width=$w; 
  65.  } 
  66.   
  67.  //@设置图片高度 
  68.  public function setHeight($h){ 
  69.  $this->height=$h; 
  70.  } 
  71.   
  72.  //@设置字符个数 
  73.  public function setTextNumber($textN){ 
  74.  $this->textNum=$textN; 
  75.  } 
  76.   
  77.  //@设置字符颜色 
  78.  public function setFontColor($fc){ 
  79.  $this->fontColor=sscanf($fc,'#%2x%2x%2x'); 
  80.  } 
  81.   
  82.  //@设置字号 
  83.  public function setFontSize($n){ 
  84.  $this->fontSize=$n; 
  85.  } 
  86.   
  87.  //@设置字体 
  88.  public function setFontFamily($ffUrl){ 
  89.  $this->fontFamily=$ffUrl; 
  90.  } 
  91.   
  92.  //@设置字符语言 
  93.  public function setTextLang($lang){ 
  94.  $this->textLang=$lang; 
  95.  } 
  96.   
  97.  //@设置图片背景 
  98.  public function setBgColor($bc){ 
  99.  $this->bgColor=sscanf($bc,'#%2x%2x%2x'); 
  100.  } 
  101.   
  102.  //@设置干扰点数量 
  103.  public function setNoisePoint($n){ 
  104.  $this->noisePoint=$n; 
  105.  } 
  106.   
  107.  //@设置干扰线数量 
  108.  public function setNoiseLine($n){ 
  109.  $this->noiseLine=$n; 
  110.  } 
  111.   
  112.  //@设置是否扭曲 
  113.  public function setDistortion($b){ 
  114.  $this->distortion=$b; 
  115.  } 
  116.   
  117.  //@设置是否显示边框 
  118.  public function setShowBorder($border){ 
  119.  $this->showBorder=$border; 
  120.  } 
  121.   
  122.  //@初始化验证码图片 
  123.  public function initImage(){ 
  124.  if(emptyempty($this->width)){$this->width=floor($this->fontSize*1.3)*$this->textNum+10;} 
  125.  if(emptyempty($this->height)){$this->height=$this->fontSize*2;} 
  126.  $this->image=imagecreatetruecolor($this->width,$this->height); 
  127.  if(emptyempty($this->bgColor)){ 
  128.  $this->randBgColor=imagecolorallocate($this->image,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255)); 
  129.  }else{ 
  130.  $this->randBgColor=imagecolorallocate($this->image,$this->bgColor[0],$this->bgColor[1],$this->bgColor[2]); 
  131.  } 
  132.  imagefill($this->image,0,0,$this->randBgColor); 
  133.  } 
  134.   
  135.  //@产生随机字符 
  136.  public function randText($type){ 
  137.  $string=''; 
  138.  switch($type){ 
  139.  case 'en': 
  140.  $str='ABCDEFGHJKLMNPQRSTUVWXY3456789'; 
  141.  for($i=0;$i<$this->textNum;$i++){ 
  142.  $string=$string.','.$str[mt_rand(0,29)]; 
  143.  } 
  144.  break; 
  145.  case 'cn': 
  146.  for($i=0;$i<$this->textNum;$i++) { 
  147.  $string=$string.','.chr(rand(0xB0,0xCC)).chr(rand(0xA1,0xBB)); 
  148.  } 
  149.  $string=iconv('GB2312','UTF-8',$string); //转换编码到utf8 
  150.  break; 
  151.  } 
  152.  return substr($string,1); 
  153.  } 
  154.   
  155.  //@输出文字到验证码 
  156.  public function createText(){ 
  157.  $textArray=explode(',',$this->randText($this->textLang)); 
  158.  $this->textContent=join('',$textArray); 
  159.  if(emptyempty($this->fontColor)){ 
  160.  $this->randFontColor=imagecolorallocate($this->image,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100)); 
  161.  }else{ 
  162.  $this->randFontColor=imagecolorallocate($this->image,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]); 
  163.  } 
  164.  for($i=0;$i<$this->textNum;$i++){ 
  165.  $angle=mt_rand(-1,1)*mt_rand(1,20); 
  166.  imagettftext($this->image,$this->fontSize,$angle,5+$i*floor($this->fontSize*1.3),floor($this->height*0.75),$this->randFontColor,$this->fontFamily,$textArray[$i]); 
  167.  } 
  168.  } 
  169.   
  170.  //@生成干扰点 
  171.  public function createNoisePoint(){ 
  172.  for($i=0;$i<$this->noisePoint;$i++){ 
  173.  $pointColor=imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); 
  174.  imagesetpixel($this->image,mt_rand(0,$this->width),mt_rand(0,$this->height),$pointColor); 
  175.  } 
  176.   
  177.  } 
  178.   
  179.  //@产生干扰线 
  180.  public function createNoiseLine(){ 
  181.  for($i=0;$i<$this->noiseLine;$i++) { 
  182.  $lineColor=imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),20); 
  183.  imageline($this->image,0,mt_rand(0,$this->width),$this->width,mt_rand(0,$this->height),$lineColor); 
  184.  } 
  185.  } 
  186.   
  187.  //@扭曲文字 
  188.  public function distortionText(){ 
  189.  $this->distortionImage=imagecreatetruecolor($this->width,$this->height); 
  190.  imagefill($this->distortionImage,0,0,$this->randBgColor); 
  191.  for($x=0;$x<$this->width;$x++){ 
  192.  for($y=0;$y<$this->height;$y++){ 
  193.  $rgbColor=imagecolorat($this->image,$x,$y); 
  194.  imagesetpixel($this->distortionImage,(int)($x+sin($y/$this->height*2*M_PI-M_PI*0.5)*3),$y,$rgbColor); 
  195.  } 
  196.  } 
  197.  $this->image=$this->distortionImage; 
  198.  } 
  199.   
  200.  //@生成验证码图片 
  201.  public function createImage(){ 
  202.  $this->initImage(); //创建基本图片 
  203.  $this->createText(); //输出验证码字符 
  204.  if($this->distortion){$this->distortionText();} //扭曲文字 
  205.  $this->createNoisePoint(); //产生干扰点 
  206.  $this->createNoiseLine(); //产生干扰线 
  207.  if($this->showBorder){imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$this->randFontColor);} //添加边框 
  208.  imagepng($this->image); 
  209.  imagedestroy($this->image); 
  210.  if($this->distortion){imagedestroy($this->$distortionImage);} 
  211.  return $this->textContent; 
  212.  }//开源代码phpfensi.com 
  213.   
  214. } 
  215. ?> 
  216.  
  217. //使用方法: 
  218.  
  219. <?php 
  220. //session_start(); 
  221. header("Content-type:image/png"); 
  222. include('captcha5_class.php'); 
  223. $captcha5=new Captcha(); 
  224.   
  225. //@设置验证码宽度 
  226. //$captcha5->setWidth(200); 
  227.   
  228. //@设置验证码高度 
  229. //$captcha5->setHeight(50); 
  230.   
  231. //@设置字符个数 
  232. $captcha5->setTextNumber(5); 
  233.   
  234. //@设置字符颜色 
  235. //$captcha5->setFontColor('#ff9900'); 
  236.   
  237. //@设置字号大小 
  238. //$captcha5->setFontSize(25); 
  239.   
  240. //@设置字体 
  241. $captcha5->setFontFamily('c:\windows\fonts\STXINGKA.TTF'); 
  242.   
  243. //@设置语言 
  244. $captcha5->setTextLang('cn'); 
  245.   
  246. //@设置背景颜色 
  247. //$captcha5->setBgColor('#000000'); 
  248.   
  249. //@设置干扰点数量 
  250. //$captcha5->setNoisePoint(600); 
  251.   
  252. //@设置干扰线数量 
  253. //$captcha5->setNoiseLine(10); 
  254.   
  255. //@设置是否扭曲 
  256. //$captcha5->setDistortion(true); 
  257.   
  258. //@设置是否显示边框 
  259. $captcha5->setShowBorder(true); 
  260.   
  261. //输出验证码 
  262. $code=$captcha5->createImage(); 
  263. //$_SESSION['captchaCode']['content']=$code; 
  264. //$_SESSION['captchaCode']['time']=microtime(); 
  265. ?> 
  266.  

出处:http://www.phpfensi.com/php/20140823/4680.html


相关教程