当前位置:首页 > PHP教程 > php图像处理 > 列表

php生成图形验证码几种方法总结

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-18 15:06:34 浏览: 评论:0 

生成图形验证码需要使用php GD库来生成,如果你没开户GD库我们需要在php.ini文件找到extension=php_gd2.dll 去掉前面的;就行了,然后重启apache 或iis环境即可.

我们先来检查一下自己的php是不是打开了gd库,代码如下:

  1. <?php 
  2. if(extension_loaded('gd')) { 
  3.   echo '你可以使用gd<br>'; 
  4.   foreach(gd_info() as $cate=>$value) 
  5.     echo "$cate: $value<br>"; 
  6. }else 
  7.   echo '你没有安装gd扩展'; 
  8. ?> 

如果有返回信息就正确可以常用使用了.

例1,代码如下:

  1. <?php 
  2. /** 
  3.  * vCode(m,n,x,y) m个数字  显示大小为n   边宽x   边高y 
  4.  * 自己改写记录session $code 
  5.  */ 
  6. session_start();  
  7. vCode(4, 15); //4个数字,显示大小为15 
  8.  
  9. function vCode($num = 4, $size = 20, $width = 0, $height = 0) { 
  10.  !$width && $width = $num * $size * 4 / 5 + 5; 
  11.  !$height && $height = $size + 10;  
  12.  // 去掉了 0 1 O l 等 
  13.  $str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW"; 
  14.  $code = ''; 
  15.  for ($i = 0; $i < $num; $i++) { 
  16.   $code .= $str[mt_rand(0, strlen($str)-1)]; 
  17.  }  
  18.  // 画图像 
  19.  $im = imagecreatetruecolor($width, $height);  
  20.  // 定义要用到的颜色 
  21.  $back_color = imagecolorallocate($im, 235, 236, 237); 
  22.  $boer_color = imagecolorallocate($im, 118, 151, 199); 
  23.  $text_color = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));  
  24.  // 画背景 
  25.  imagefilledrectangle($im, 0, 0, $width, $height, $back_color);  
  26.  // 画边框 
  27.  imagerectangle($im, 0, 0, $width-1, $height-1, $boer_color);  
  28.  // 画干扰线 
  29.  for($i = 0;$i < 5;$i++) { 
  30.   $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); 
  31.   imagearc($im, mt_rand(- $width, $width), mt_rand(- $height, $height), mt_rand(30, $width * 2), mt_rand(20, $height * 2), mt_rand(0, 360), mt_rand(0, 360), $font_color); 
  32.  }  
  33.  // 画干扰点 
  34.  for($i = 0;$i < 50;$i++) { 
  35.   $font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255)); 
  36.   imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $font_color); 
  37.  }  
  38.  // 画验证码 
  39.  @imagefttext($im, $size , 0, 5, $size + 3, $text_color, 'c:\WINDOWS\Fonts\simsun.ttc', $code); 
  40.  $_SESSION["VerifyCode"]=$code;  
  41.  header("Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate"); 
  42.  header("Content-type: image/png;charset=gb2312"); 
  43.  imagepng($im); 
  44.  imagedestroy($im); 
  45. }//开源代码phpfensi.com 
  46. ?> 

例2:使用PHP,结合session和GD库扩展开发的一个生成验证码的例子(w3c推荐),可以很方便的用于项目中,而且样式美观,代码如下:

  1. ​//首先开启session 
  2.  
  3. session_start(); 
  4. //定义前台显示验证码长&宽 
  5. $image_width = 120; 
  6. $image_height = 40; 
  7. $characters_on_image = 6; 
  8. $font = './monofont.ttf'; 
  9.  
  10. //The characters that can be used in the CAPTCHA code. 
  11. //avoid confusing characters (l 1 and i for example) 
  12. $possible_letters = '23456789bcdfghjkmnpqrstvwxyz'; 
  13. $random_dots = 10; 
  14. $random_lines = 30; 
  15. $captcha_text_color="0x142864"; 
  16. $captcha_noice_color = "0x142864"; 
  17. //定义要生成验证码的字符串 
  18. $code = ''; 
  19.  
  20. $i = 0; 
  21. while ($i < $characters_on_image) { 
  22. $code .= substr($possible_letters, mt_rand(0, strlen($possible_letters)-1), 1); 
  23. $i++; 
  24. } 
  25.  
  26. $font_size = $image_height * 0.75; 
  27. $image = @imagecreate($image_width, $image_height); 
  28.  
  29. /* setting the background, text and noise colours here */ 
  30. $background_color = imagecolorallocate($image, 255, 255, 255); 
  31.  
  32. $arr_text_color = hexrgb($captcha_text_color); 
  33. $text_color = imagecolorallocate($image, $arr_text_color['red'], 
  34.         $arr_text_color['green'], $arr_text_color['blue']); 
  35.  
  36. $arr_noice_color = hexrgb($captcha_noice_color); 
  37. $image_noise_color = imagecolorallocate($image, $arr_noice_color['red'], 
  38.         $arr_noice_color['green'], $arr_noice_color['blue']); 
  39.  
  40. /* generating the dots randomly in background */ 
  41. for( $i=0; $i<$random_dots; $i++ ) { 
  42. imagefilledellipse($image, mt_rand(0,$image_width), 
  43.  mt_rand(0,$image_height), 2, 3, $image_noise_color); 
  44. } 
  45.  
  46. /* generating lines randomly in background of image */ 
  47. for( $i=0; $i<$random_lines; $i++ ) { 
  48. imageline($image, mt_rand(0,$image_width), mt_rand(0,$image_height), 
  49.  mt_rand(0,$image_width), mt_rand(0,$image_height), $image_noise_color); 
  50. } 
  51.  
  52. /* create a text box and add 6 letters code in it */ 
  53. $textbox = imagettfbbox($font_size, 0, $font, $code); 
  54. $x = ($image_width - $textbox[4])/2; 
  55. $y = ($image_height - $textbox[5])/2; 
  56. imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code); 
  57.  
  58. /* Show captcha image in the page html page */ 
  59. header('Content-Type: image/jpeg');// defining the image type to be shown in browser widow 
  60. imagejpeg($image);//showing the image 
  61. imagedestroy($image);//destroying the image instance 
  62. //设置session,做验证 
  63. $_SESSION['6_letters_code'] = $code; 
  64.  
  65. function hexrgb ($hexstr) 
  66. { 
  67.   $int = hexdec($hexstr); 
  68.  
  69.   return array("red" => 0xFF & ($int >> 0x10), 
  70.                "green" => 0xFF & ($int >> 0x8), 
  71.                "blue" => 0xFF & $int); 
  72. } 

个人推荐推荐第二个生成验证码程序代码,各位同学可尝试参考对比,最后一个是W3C标准生成的也是利用了php gd库.

Tags: php生成图形验证码

分享到: