首页 > 语言 > PHP > 正文

php 中文汉字验证码生成程序

2024-09-04 11:44:19
字体:大 中 小
来源:转载
供稿:网友

这款关于php 中文汉字验证码生成程序其实很简单的,生成中文汉字验证码需要字体来支持了,下面我们用的arial.ttf来支持生成,否则生成中文图片验证码会出乱码的.

php 中文汉字验证码生成程序代码如下:

  1. class securecode 
  2. { 
  3.     private static $instance=null; 
  4.     private $code = ''; 
  5.     private $fontfile; 
  6.     private $validate; 
  7.     private $image; 
  8.     private $specialadd = 'special string for securecode'; 
  9.     private $codeexpire=86400; 
  10.     private $codecookiename='secure_code'; 
  11.  
  12.     /** 
  13.      * 构造方法 
  14.      */ 
  15.     private function securecode() 
  16.     { 
  17.         $this->fontfile = dirname( __file__ ) . '/arial.ttf'; 
  18.     } 
  19.  
  20.     private function __construct() 
  21.     { 
  22.         $this->securecode(); 
  23.     } 
  24.      
  25.     public static function getinstance() 
  26.     { 
  27.         if (self::$instance==null)  
  28.             self::$instance=new self(); 
  29.          
  30.         return self::$instance; 
  31.     } 
  32.  
  33.     /** 
  34.      * 指定字体文件所在路径,默认为当前文件夹下arial.ttf文件 
  35.      * @param $fontfile 文件路径 
  36.      * @return void 
  37.      */ 
  38.     function loadfont($fontfile) 
  39.     { 
  40.         $this->fontfile = $fontfile; 
  41.     } 
  42.  
  43.     /** 
  44.      * 图片输出方法,在执行本方法前程序不应该有任何形式的输出 
  45.      * @return void; 
  46.      */ 
  47.     function stroke() 
  48.     { 
  49.         $this->savecode(); 
  50.         self::sendheader(); 
  51.         imagegif( $this->validate ); 
  52.         imagedestroy( $this->validate ); 
  53.         imagedestroy( $this->image ); 
  54.     } 
  55.  
  56.     /** 
  57.      * 图片保存方法 
  58.      * @param $filename 保存路径 
  59.      * @return void 
  60.      */ 
  61.     function save($filename) 
  62.     { 
  63.         $this->savecode(); 
  64.         imagegif( $this->validate , $filename ); 
  65.         imagedestroy( $this->validate ); 
  66.         imagedestroy( $this->image ); 
  67.     } 
  68.      
  69.   /** 
  70.      * 验证码验证方法 
  71.      * @param $input 要验证的字符串,即用户的输入内容 
  72.      * @return boolean 验证结果 
  73.      */ 
  74.     function verify($input) 
  75.     { 
  76.         $input=strtolower($input); 
  77.         $targetcode=$this->authcode($input); 
  78.         $code=$this->getcookie(); 
  79.         if (emptyempty($code)||$code!=$targetcode)  
  80.             $result= false; 
  81.         else 
  82.             $result=true; 
  83.         $_cookie[$this->codecookiename]=''; 
  84.         setcookie ( $this->codecookiename, '', - 1 ); 
  85.         return $result; 
  86.     } 
  87.  
  88.     /** 
  89.      * 图片创建方法 
  90.      * @return void; 
  91.      */ 
  92.     function createimage() 
  93.     { 
  94.         $this->randcode(); 
  95.          
  96.         $size = 30; 
  97.         $width = 90; 
  98.         $height = 35; 
  99.         $degrees = array ( 
  100.             rand( 0 , 30 ), rand( 0 , 30 ), rand( 0 , 30 ), rand( 0 , 30 ) 
  101.         ); 
  102.         
  103.  
  104.         for ($i = 0; $i < 4; ++$i) 
  105.         { 
  106.             if (rand() % 2); 
  107.             else $degrees[$i] = -$degrees[$i]; 
  108.         } 
  109.          
  110.         $this->image = imagecreatetruecolor( $size , $size ); 
  111.         $this->validate = imagecreatetruecolor( $width , $height ); 
  112.         $back = imagecolorallocate( $this->image , 255 , 255 , 255 ); 
  113.         $border = imagecolorallocate( $this->image , 0 , 0 , 0 ); 
  114.         imagefilledrectangle( $this->validate , 0 , 0 , $width , $height , $back ); 
  115.          
  116.         for ($i = 0; $i < 4; ++$i) 
  117.         { 
  118.             $temp = self::rgbtohsv( rand( 0 , 250 ) , rand( 0 , 150 ) , rand( 0 , 250 ) ); 
  119.              
  120.             if ($temp[2] > 60) $temp[2] = 60; 
  121.              
  122.             $temp = self::hsvtorgb( $temp[0] , $temp[1] , $temp[2] ); 
  123.             $textcolor[$i] = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] ); 
  124.         } 
  125.          
  126.         for ($i = 0; $i < 200; ++$i) 
  127.         { 
  128.             $randpixelcolor = imagecolorallocate( $this->validate , rand( 0 , 255 ) , rand( 0 , 255 ) , rand( 0 , 255 ) ); 
  129.             imagesetpixel( $this->validate , rand( 1 , 87 ) , rand( 1 , 35 ) , $randpixelcolor ); 
  130.         } 
  131.          
  132.         $temp = self::rgbtohsv( rand( 220 , 255 ) , rand( 220 , 255 ) , rand( 220 , 255 ) ); 
  133.          
  134.         if ($temp[2] < 200) $temp[2] = 255; 
  135.          
  136.         $temp = self::hsvtorgb( $temp[0] , $temp[1] , $temp[2] ); 
  137.         $randlinecolor = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] ); 
  138.          
  139.         self::imagelinethick( $this->validate , $textcolor[rand( 0 , 3 )] ); 
  140.          
  141.         imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back ); 
  142.         putenv( 'gdfontpath=' . realpath( '.' ) ); 
  143.          
  144.         // name the font to be used (note the lack of the .ttf extension 
  145.         imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[0] , $this->fontfile , $this->code[0] ); 
  146.          
  147.         $this->image = imagerotate( $this->image , $degrees[0] , $back ); 
  148.         imagecolortransparent( $this->image , $back ); 
  149.         imagecopymerge( $this->validate , $this->image , 1 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 ); 
  150.          
  151.         $this->image = imagecreatetruecolor( $size , $size ); 
  152.         imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back ); 
  153.         imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[1] , $this->fontfile , $this->code[1] ); 
  154.         $this->image = imagerotate( $this->image , $degrees[1] , $back ); 
  155.         imagecolortransparent( $this->image , $back ); 
  156.         imagecopymerge( $this->validate , $this->image , 21 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 ); 
  157.          
  158.         $this->image = imagecreatetruecolor( $size , $size ); 
  159.         imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back ); 
  160.         imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[2] , $this->fontfile , $this->code[2] ); 
  161.         $this->image = imagerotate( $this->image , $degrees[2] , $back ); 
  162.         imagecolortransparent( $this->image , $back ); 
  163.         imagecopymerge( $this->validate , $this->image , 41 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 ); 
  164.          
  165.         $this->image = imagecreatetruecolor( $size , $size ); 
  166.         imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back ); 
  167.         imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[3] , $this->fontfile , $this->code[3] ); 
  168.         $this->image = imagerotate( $this->image , $degrees[3] , $back ); 
  169.         imagecolortransparent( $this->image , $back ); 
  170.         imagecopymerge( $this->validate , $this->image , 61 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 ); 
  171.         imagerectangle( $this->validate , 0 , 0 , $width - 1 , $height - 1 , $border ); 
  172.     } 
  173. //开源代码Vevb.com 
  174.  /** 
  175.      * 获取随机生成的验证码 
  176.      * @return string 随机验证码,返回的验证码不进行任何处理 
  177.      */ 
  178.   function getcode() 
  179.     { 
  180.         return $this->code; 
  181.     } 
  182.  
  183.     /** 
  184.      * 生成随机码方法 
  185.      * @return void; 
  186.      */ 
  187.     protected function randcode() 
  188.     { 
  189.         $alphastr = 'abcdefghijklmnpqrstuvwxyz123456789'; 
  190.         $randstr = array ( 
  191.             $alphastr{rand( 0 , 33 )}, $alphastr{rand( 0 , 33 )}, $alphastr{rand( 0 , 33 )}, $alphastr{rand( 0 , 33 )} 
  192.         ); 
  193.         $this->code = strtolower( $randstr[0] . $randstr[1] . $randstr[2] . $randstr[3] ); 
  194.     } 
  195.  
  196.     /** 
  197.      * rgb色到hsv色转变方法 
  198.      * @param $r 
  199.      * @param $g 
  200.      * @param $b 
  201.      * @return array hsv数组 
  202.      */ 
  203.     protected static function rgbtohsv($r, $g, $b) 
  204.     { 
  205.         $tmp = min( $r , $g ); 
  206.         $min = min( $tmp , $b ); 
  207.         $tmp = max( $r , $g ); 
  208.         $max = max( $tmp , $b ); 
  209.         $v = $max; 
  210.         $delta = $max - $min; 
  211.          
  212.         if ($max != 0) $s = $delta / $max; // s 
  213.         else 
  214.         { 
  215.             $s = 0; 
  216.             //$h = undefinedcolor; 
  217.             return; 
  218.         } 
  219.         if ($r == $max) $h = ($g - $b) / $delta; // between yellow & magenta 
  220.         else if ($g == $max) $h = 2 + ($b - $r) / $delta; // between cyan & yellow 
  221.         else $h = 4 + ($r - $g) / $delta; // between magenta & cyan 
  222.         
  223.  
  224.  
  225.         $h *= 60; // degrees 
  226.         if ($h < 0) $h += 360; 
  227.         return array ( 
  228.             $h, $s, $v 
  229.         ); 
  230.     } 
  231.  
  232.     /** 
  233.      * 同上一方法功能相反 
  234.      * @param $h 
  235.      * @param $s 
  236.      * @param $v 
  237.      * @return array rgb数组 
  238.      */ 
  239.     protected static function hsvtorgb($h, $s, $v) 
  240.     { 
  241.         if ($s == 0) 
  242.         { 
  243.             // achromatic (grey) 
  244.             $r = $g = $b = $v; 
  245.             return; 
  246.         } 
  247.          
  248.         $h /= 60; // sector 0 to 5 
  249.         $i = floor( $h ); 
  250.         $f = $h - $i; // factorial part of h 
  251.         $p = $v * (1 - $s); 
  252.         $q = $v * (1 - $s * $f); 
  253.         $t = $v * (1 - $s * (1 - $f)); 
  254.          
  255.         switch ($i) 
  256.         { 
  257.             case 0 : 
  258.                 $r = $v; 
  259.                 $g = $t; 
  260.                 $b = $p; 
  261.                 break; 
  262.             case 1 : 
  263.                 $r = $q; 
  264.                 $g = $v; 
  265.                 $b = $p; 
  266.                 break; 
  267.             case 2 : 
  268.                 $r = $p; 
  269.                 $g = $v; 
  270.                 $b = $t; 
  271.                 break; 
  272.             case 3 : 
  273.                 $r = $p; 
  274.                 $g = $q; 
  275.                 $b = $v; 
  276.                 break; 
  277.             case 4 : 
  278.                 $r = $t; 
  279.                 $g = $p; 
  280.                 $b = $v; 
  281.                 break; 
  282.             default : // case 5: 
  283.                 $r = $v; 
  284.                 $g = $p; 
  285.                 $b = $q; 
  286.                 break; 
  287.         } 
  288.         return array ( 
  289.             $r, $g, $b 
  290.         ); 
  291.     } 
  292.  
  293.     /** 
  294.  
  295. 使用cookie保存验证码的方法 
  296.      * @return void 
  297.      */ 
  298.     protected function savecode() 
  299.     { 
  300.         $code = $this->authcode($this->code); 
  301.         $this->setcookie($code); 
  302.     } 
  303.  
  304.     /** 
  305.      * 验证码cookie值获取方法 
  306.      * @return string cookie值 
  307.      */ 
  308.     protected function getcookie() 
  309.     { 
  310.         if (emptyempty( $_cookie[$this->codecookiename] )) 
  311.         { 
  312.             return ''; 
  313.         } 
  314.         else 
  315.         { 
  316.             return addslashes($_cookie[$this->codecookiename]); 
  317.         } 
  318.     } 
  319.      
  320.     /** 
  321.      * 验证码cookie创建方法 
  322.      * @param string $code 要保存的验证码 
  323.      * @return void 
  324.      */ 
  325.     protected function setcookie($code) 
  326.     { 
  327.         $expire = $this->codeexpire > 0 ? $this->codeexpire + time() : 0; 
  328.         setcookie( $this->codecookiename , $code, $expire ); 
  329.     } 
  330.      
  331.     /** 
  332.      * 验证码加密方法 
  333.      * @param string $code 要加密的随机码 
  334.      * @return mixed 执行结果 
  335.      */ 
  336.     protected function authcode($code) 
  337.     { 
  338.         return md5($code.$this->specialadd); 
  339.     } 
  340.      
  341.     /** 
  342.      * 干扰线生成方法 
  343.      * @param resource $image 图片资源句柄 
  344.      * @param string $color 干扰线颜色 
  345.      */ 
  346.     protected static function imagelinethick($image, $color) 
  347.     { 
  348.         $k = rand( 5 , 20 ); 
  349.         for ($px = 0; $px < 400; $px = $px + 1) 
  350.         { 
  351.             $y = $k * sin( 0.1 * ($px) ); //$y=200+10*sin(0.1*($px-200)); 
  352.             for ($i = 0; $i < 2; $i++) 
  353.             { 
  354.                 imagesetpixel( $image , $px , $y + 10 + $i , $color ); 
  355.             } 
  356.          
  357.         } 
  358.     } 
  359.  
  360.     /** 
  361.      * http标头设置方法 
  362.      * @return void 
  363.      */ 
  364.     protected static function sendheader() 
  365.     { 
  366.         header( "pragma: no-cache" ); 
  367.         header( "cache-control: max-age=1, s-maxage=1, no-cache, must-revalidate" ); 
  368.         header( 'content-type: image/gif' ); 
  369.     } 
  370. } 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表