VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • php 验证码图片程序

  1. <?php 
  2. /* 
  3. *文件名:class.safeCode.php 
  4. *类名:safeCode 
  5. *目的:生成web应用时所需的验证码图片 
  6. *当前版本:1.0.2 
  7. *作者:NoAngels  
  8. *联系方式:flare_1023@163.com QQ:82535599 MSN:atizu@hotmail.com 
  9. *更新内容: 
  10. *版本1.0.2 
  11. *1.更新了对linux主机支持,设置GDFONTPATH环境变量, 
  12. * putenv('GDFONTPATH=' . realpath('.')); 
  13. *2.对不支持antialias函数的时候避免采用此方法,避免低版本的GD库 
  14. *版本1.0.1 
  15. *1.更新了绘制指定类型图片,支持类型有:png,jpeg,gif.默认为png(此效果最佳) 
  16. *2.优化了部分代码  
  17. */ 
  18. class safeCode{ 
  19.         function __construct(){ 
  20.                 #设置默认safe安全保护码 
  21.                 $this->__safe = substr(md5(time()), 0 , 4); 
  22.         } 
  23.         function __destruct(){ 
  24.                 #释放资源 
  25.                 unset($this->__img, $this->__canvasWidth, $this->__canvasHeight, $this->__codeLength, $this->__spacePerChar, $this->__code, $this->__safe, $this->__background, $this->__borderColor, $this->__colors, $this->__font, $this->__sessionName); 
  26.         } 
  27.         function setCanvas($width = 200, $height = 60){ 
  28.                 #设置画布宽度以及高度.有默认值 
  29.                 $this->__canvasWidth = $width
  30.                 $this->__canvasHeight = $height
  31.                 $this->__img = imagecreatetruecolor($width$height);                 
  32.                 return
  33.         } 
  34.         function setSafe($char){ 
  35.                 #设置安全码,不调用系统会自己设置 
  36.                 $this->__safe = $char
  37.                 return
  38.         } 
  39.         function setCodeLength($num = 8){ 
  40.                 #设置验证码长度 
  41.                 $this->__codeLength = $num
  42.         }         
  43.         function setFont($fontName){ 
  44.                 #设置绘图时所用的字体,字体文件必须与类文件同目录 
  45.                 $this->__font = $fontName
  46.                 return
  47.         } 
  48.         function setBackground($arg1 = 255, $arg2 = 255, $arg3 = 255){ 
  49.                 #设置背景颜色 
  50.                 $this->__background = imagecolorallocate($this->__img, $arg1$arg2$arg3); 
  51.                 return;  
  52.         } 
  53.         function setBorderColor($arg1 = 128, $arg2 = 128, $arg3 = 128){ 
  54.                 #设置边框颜色 
  55.                 $this->__borderColor = imagecolorallocate($this->__img, $arg1$arg2$arg3); 
  56.         } 
  57.         function setFontColor($arr){ 
  58.                 #设置绘图时所用颜色,参数为颜色集合数组 
  59.                 foreach($arr as $color){ 
  60.                         $this->__colors[] = imagecolorallocate($this->__img, $color[0], $color[1], $color[2]); 
  61.                 } 
  62.                 return
  63.         }         
  64.         function setSession($sessionName = 'safeCode'){ 
  65.                 #设置session变量名,默认为$_SESSION['safeCode'
  66.                 $this->__sessionName = $sessionName
  67.                 #$_SESSION[$sessionName] = $this->__code; 
  68.                 return
  69.         } 
  70.         function display($type = 'png'){ 
  71.                 #显示图片,可指定显示类型,目前支持png(默认),jpeg,gif 
  72.                 $this->__setSpacePerChar(); 
  73.                 imagefilledrectangle($this->__img, 1, 1, $this->__canvasWidth - 2, $this->__canvasHeight -2, $this->__background); 
  74.                 imagerectangle($this->__img, 0, 0, $this->__canvasWidth - 1, $this->__canvasHeight - 1, $this->__borderColor); 
  75.                 $this->__drawText($this->__colors, $this->__getCode(), $this->__font); 
  76.                 #修正linux以及低版本的GD库问题 
  77.                 if(function_exists(imageantialias)){ 
  78.                    imageantialias($this->__img, true); 
  79.                 } 
  80.                 $_SESSION[$this->__sessionName] = $this->__code; 
  81.                 switch ($type){ 
  82.                         case 'png'
  83.                                 header('Content-type:image/png');                                 
  84.                                 imagepng($this->__img);         
  85.                         case 'jpeg'
  86.                                 header('Content-type:image/jpeg');                                 
  87.                                 imagejpeg($this->__img, '' , 75); 
  88.                         case 'gif'
  89.                                 header('Content-type:image/gif');                                 
  90.                                 imagegif($this->__img);         
  91.                         default
  92.                                 header('Content-type:image/png');                                 
  93.                                 imagepng($this->__img);                                                                                                                 
  94.                 } 
  95.                 imagegif($this->__img);         
  96.         }         
  97.         private function __setSpacePerChar(){ 
  98.                 #私有函数,设置字符间隔 
  99.                 $this->__spacePerChar = $this->__canvasWidth / $this->__codeLength; 
  100.                 return
  101.         } 
  102.         private function __getCode(){ 
  103.                 #获取验证码 
  104.                 $this->__code = substr(md5(time().$this->__safe), rand(0, 24), $this->__codeLength); 
  105.                 return $this->__code; 
  106.         } 
  107.         private function  __drawText($colors$code$fontName){ 
  108.                 #开始绘图 
  109.                 #设置GBFONTPATH为当前目录,不然linux环境下会报错 
  110.                 putenv('GDFONTPATH=' . realpath('.')); 
  111.                 for($i = 0; $i < strlen($code); $i++){ 
  112.                         $color = $colors[$i % count($colors)]; 
  113.                         imagettftext( 
  114.                                 $this->__img, 
  115.                                 24 + rand(0, 8), 
  116.                                 -20 + rand(0, 40), 
  117.                                 ($i + 0.3) * $this->__spacePerChar, 
  118.                                 $this->__canvasHeight - 20 + rand(0, 20), 
  119.                                 $color
  120.                                 $fontName
  121.                                 $code{$i
  122.                         ); 
  123.                 } 
  124.                 #绘制随机实线 
  125.                 for($i = 0; $i < 400; $i++){ 
  126.                         $x1 = rand(5, $this->__canvasWidth - 5); 
  127.                         $y1 = rand(5, $this->__canvasHeight - 5); 
  128.                         $x2 = $x1 - 4 + rand(0, 8); 
  129.                         $y2 = $y1 - 4 + rand(0, 8); 
  130.                         imageline($this->__img, $x1$y1$x2$y2$this->__colors[rand(0, count($this->__colors) - 1)]); 
  131.                 } 
  132.                 return
  133.         }//开源代码phpfensi.com 
  134.         private $__img = NULL; 
  135.         private $__canvasWidth = 120; 
  136.         private $__canvasHeight = 80; 
  137.         private $__codeLength = 8; 
  138.         private $__spacePerChar = 0; 
  139.         private $__code = 12345678; 
  140.         private $__safe = 2008; 
  141.         private $__background = NULL; 
  142.         private $__borderColor = NULL;         
  143.         private $__colors = array(); 
  144.         private $__font = 'arial.ttf'
  145.         private $__sessionName = ''
  146. ?> 

使用方法,代码如下:

  1. <?php 
  2. #测试文件 
  3. include_once('class.safeCode.php'); 
  4. session_start(); 
  5. $safeCode = new safeCode; 
  6. $safeCode->setCanvas(200, 50); 
  7. $safeCode->setCodeLength(8); 
  8. $safeCode->setSafe('2008');#设置安全码,不设置有默认值,动态生成 
  9. $safeCode->setBackground(); 
  10. $safeCode->setBorderColor(); 
  11. $safeCode->setFont('arial.ttf');#设置字体.必须同目录,字体文件 
  12. $colors = array(array(128, 64, 192), array(192, 64, 128), array(108, 192, 64));//开源代码phpfensi.com 
  13. $safeCode->setFontColor($colors); 
  14. $safeCode->setSession(); 
  15. $safeCode->display('jpeg'); 
  16. ?> 
  17.  

原文链接:http://www.phpfensi.com/php/20140820/4506.html

相关教程