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

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

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

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

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