php专区

 首页 > php专区 > PHP应用 > php类库 > 支持中文字母数字、自定义字体php验证码程序

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

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

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

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

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

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