免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2004 | 回复: 1

支持中文字母数字、自定义字体php验证码程序 [复制链接]

论坛徽章:
0
发表于 2012-03-02 17:18 |显示全部楼层
支持中文字母数字、自定义字体php验证码程序
  1. <?php
  2. /*
  3. * Captcha Class base on PHP GD Lib
  4. * @author Design
  5. * @version 1.0
  6. * @copyright http://www.hzhuti.com/nokia/n97/ 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. //@Constructor 构造函数
  48. public function Captcha(){
  49. $this->textNum=4;
  50. $this->fontSize=16;
  51. $this->fontFamily='c:\windows\fontsSIMYOU.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. public function setWidth($w){
  60. $this->width=$w;
  61. }

  62. //@设置图片高度
  63. public function setHeight($h){
  64. $this->height=$h;
  65. }

  66. //@设置字符个数
  67. public function setTextNumber($textN){
  68. $this->textNum=$textN;
  69. }

  70. //@设置字符颜色
  71. public function setFontColor($fc){
  72. $this->fontColor=sscanf($fc,'#%2x%2x%2x');
  73. }

  74. //@设置字号
  75. public function setFontSize($n){
  76. $this->fontSize=$n;
  77. }

  78. //@设置字体
  79. public function setFontFamily($ffUrl){
  80. $this->fontFamily=$ffUrl;
  81. }

  82. //@设置字符语言
  83. public function setTextLang($lang){
  84. $this->textLang=$lang;
  85. }

  86. //@设置图片背景
  87. public function setBgColor($bc){
  88. $this->bgColor=sscanf($bc,'#%2x%2x%2x');
  89. }

  90. //@设置干扰点数量
  91. public function setNoisePoint($n){
  92. $this->noisePoint=$n;
  93. }

  94. //@设置干扰线数量
  95. public function setNoiseLine($n){
  96. $this->noiseLine=$n;
  97. }

  98. //@设置是否扭曲
  99. public function setDistortion($b){
  100. $this->distortion=$b;
  101. }

  102. //@设置是否显示边框
  103. public function setShowBorder($border){
  104. $this->showBorder=$border;
  105. }

  106. //@初始化验证码图片
  107. public function initImage(){
  108. if(empty($this->width)){$this->width=floor($this->fontSize*1.3)*$this->textNum+10;}
  109. if(empty($this->height)){$this->height=$this->fontSize*2;}
  110. $this->image=imagecreatetruecolor($this->width,$this->height);
  111. if(empty($this->bgColor)){
  112. $this->randBgColor=imagecolorallocate($this->image,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
  113. }else{
  114. $this->randBgColor=imagecolorallocate($this->image,$this->bgColor[0],$this->bgColor[1],$this->bgColor[2]);
  115. }
  116. imagefill($this->image,0,0,$this->randBgColor);
  117. }

  118. //@产生随机字符
  119. public function randText($type){
  120. $string='';
  121. switch($type){
  122. case 'en':
  123. $str='ABCDEFGHJKLMNPQRSTUVWXY3456789';
  124. for($i=0;$i<$this->textNum;$i++){
  125. $string=$string.','.$str[mt_rand(0,29)];
  126. }
  127. break;
  128. case 'cn':
  129. for($i=0;$i<$this->textNum;$i++) {
  130. $string=$string.','.chr(rand(0xB0,0xCC)).chr(rand(0xA1,0xBB));
  131. }
  132. $string=iconv('GB2312','UTF-8',$string); //转换编码到utf8
  133. break;
  134. }
  135. return substr($string,1);
  136. }

  137. //@输出文字到验证码
  138. public function createText(){
  139. $textArray=explode(',',$this->randText($this->textLang));
  140. $this->textContent=join('',$textArray);
  141. if(empty($this->fontColor)){
  142. $this->randFontColor=imagecolorallocate($this->image,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
  143. }else{
  144. $this->randFontColor=imagecolorallocate($this->image,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);
  145. }
  146. for($i=0;$i<$this->textNum;$i++){
  147. $angle=mt_rand(-1,1)*mt_rand(1,20);
  148. imagettftext($this->image,$this->fontSize,$angle,5+$i*floor($this->fontSize*1.3),floor($this->height*0.75),$this->randFontColor,$this->fontFamily,$textArray[$i]);
  149. }
  150. }

  151. //@生成干扰点
  152. public function createNoisePoint(){
  153. for($i=0;$i<$this->noisePoint;$i++){
  154. $pointColor=imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  155. imagesetpixel($this->image,mt_rand(0,$this->width),mt_rand(0,$this->height),$pointColor);
  156. }

  157. }

  158. //@产生干扰线
  159. public function createNoiseLine(){
  160. for($i=0;$i<$this->noiseLine;$i++) {
  161. $lineColor=imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),20);
  162. imageline($this->image,0,mt_rand(0,$this->width),$this->width,mt_rand(0,$this->height),$lineColor);
  163. }
  164. }

  165. //@扭曲文字
  166. public function distortionText(){
  167. $this->distortionImage=imagecreatetruecolor($this->width,$this->height);
  168. imagefill($this->distortionImage,0,0,$this->randBgColor);
  169. for($x=0;$x<$this->width;$x++){
  170. for($y=0;$y<$this->height;$y++){
  171. $rgbColor=imagecolorat($this->image,$x,$y);
  172. imagesetpixel($this->distortionImage,(int)($x+sin($y/$this->height*2*M_PI-M_PI*0.5)*3),$y,$rgbColor);
  173. }
  174. }
  175. $this->image=$this->distortionImage;
  176. }

  177. //@生成验证码图片
  178. public function createImage(){
  179. $this->initImage(); //创建基本图片
  180. $this->createText(); //输出验证码字符
  181. if($this->distortion){$this->distortionText();} //扭曲文字
  182. $this->createNoisePoint(); //产生干扰点
  183. $this->createNoiseLine(); //产生干扰线
  184. if($this->showBorder){imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$this->randFontColor);} //添加边框
  185. imagepng($this->image);
  186. imagedestroy($this->image);
  187. if($this->distortion){imagedestroy($this->$distortionImage);}
  188. return $this->textContent;
  189. }

  190. }
  191. ?>使用方法:

  192. <?php
  193. //session_start();
  194. header("Content-type:image/png");
  195. include('captcha5_class.php');
  196. $captcha5=new Captcha();

  197. //@设置验证码宽度
  198. //$captcha5->setWidth(200);

  199. //@设置验证码高度
  200. //$captcha5->setHeight(50);

  201. //@设置字符个数
  202. $captcha5->setTextNumber(5);

  203. //@设置字符颜色
  204. //$captcha5->setFontColor('#ff9900');

  205. //@设置字号大小
  206. //$captcha5->setFontSize(25);

  207. //@设置字体
  208. $captcha5->setFontFamily('c:\windows\fonts\STXINGKA.TTF');

  209. //@设置语言
  210. $captcha5->setTextLang('cn');

  211. //@设置背景颜色
  212. //$captcha5->setBgColor('#000000');

  213. //@设置干扰点数量
  214. //$captcha5->setNoisePoint(600);

  215. //@设置干扰线数量
  216. //$captcha5->setNoiseLine(10);

  217. //@设置是否扭曲
  218. //$captcha5->setDistortion(true);

  219. //@设置是否显示边框
  220. $captcha5->setShowBorder(true);

  221. //输出验证码
  222. $code=$captcha5->createImage();
  223. //$_SESSION['captchaCode']['content']=$code;
  224. //$_SESSION['captchaCode']['time']=microtime();
  225. ?>
复制代码

论坛徽章:
0
发表于 2012-03-02 17:18 |显示全部楼层
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP