网站地图    收藏   

主页 > php专栏 > php类库 >

php高安全验证码生成程序 - php类库

来源:自学PHP网    时间:2014-11-30 12:53 作者: 阅读:次

[导读] 本生成程序需要调用一些字体库,你可以调用你系统自带的一些字体,当然也可以像dedecms一样自己把字体放到一个目录,这样在服务器上也可以使用.php高安全验证码生成程序实例代码如下...

php高安全验证码生成程序

本生成程序需要调用一些字体库,你可以调用你系统自带的一些字体,当然也可以像dedecms一样自己把字体放到一个目录,这样在服务器上也可以使用.

php高安全验证码生成程序实例代码如下:

  1. <?php  
  2. public function captcha(){  
  3. $font_dir = $_SERVER ["DOCUMENT_ROOT"] . "your_ttf_file.ttf"; // 字体库  
  4. $img_w = 58; // 设置图片宽  
  5. $img_h = 20; // 设置图片高  
  6. $font_size = 11; // 字体大小  
  7. $angle_l = -10; // 左偏角  
  8. $angle_r = 10; // 右偏角  
  9. $code_str = "ABCDEFGHJKLMNPQRSTUVWXYZ36";  
  10. $word_len = 4; // 验证码位数  
  11. $padding = 5; // 每两个文字之间间隔  
  12. $margin = 2; // 左侧边距  
  13. $base_line = 15; // 文字基线位置  
  14. $base_line_offset = 2; // 基准线偏移量  
  15. $pixel_num = 3; // 杂点数目基数  
  16. $pixel_color= 8; // 杂点只有 $pixel_color 种颜色 总的杂点数为$pixel_num*$pixel_color  
  17. $noise_font_size = 1; // 杂点字体大小  
  18. $session_key= "my.xoyo_captcha"; //自定义session键名 
  19.  
  20. header("Cache-Control: no-cache, must-revalidate");  
  21. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");  
  22. header("Pragma: no-cache");  
  23. header("Cache-control: private");  
  24. header('Content-Type: image/png'); 
  25.  
  26. session_start();  
  27. $word = "";  
  28. $code_str_len = strlen($code_str) - 1;  
  29. for ($i = 0; $i < $word_len; $i++) {  
  30. $word.= $code_str[rand(0, $code_str_len)];  
  31. }  
  32. $_SESSION [$session_key] = strtolower($word);  
  33. $image = imagecreatetruecolor($img_w, $img_h);  
  34. imagefilledrectangle($image, 0, 0, $img_w - 1, $img_h - 1, imagecolorallocate($image, mt_rand(235, 255), mt_rand(235, 255), mt_rand(235, 255))); 
  35.  
  36. //绘制杂点  
  37. for($i = 0; $i < $pixel_color; $i++){  
  38. $noise_color = imagecolorallocate( $image, mt_rand(150,225), mt_rand(150,225), mt_rand(150,225) );  
  39. for($j = 0; $j < $pixel_num; $j++) {  
  40. imagestring( $image, $noise_font_size, mt_rand(-10, $img_w), mt_rand(-10, $img_h), $code_str[mt_rand(0, $code_str_len)], $noise_color );  
  41. }  
  42. } 
  43.  
  44. //绘制文字  
  45. for ($i = 0; $i < $word_len; ++$i) {  
  46. $color = imagecolorallocate($image, mt_rand(0, 100), mt_rand(20, 120), mt_rand(50, 150));  
  47. imagettftext($image, $font_size, mt_rand($angle_l, $angle_r), $margin, mt_rand($base_line-$base_line_offset, $base_line+$base_line_offset), $color, $font, mb_substr($word, $i, 1, 'utf-8'));  
  48. $margin += (imagefontwidth($font_size) + $padding);  
  49. }//开源代码phpfensi.com 
  50.  
  51. imagepng($image);  
  52. imagedestroy($image);  
  53. exit;  
  54. } 
  55. ?> 

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论