php生成验证码图片程序

一款国外网站下载的php生成验证码图片代码,这个比较实用我们还举例说明了,有需要的朋友按上面保存成php就可以用了.

php生成验证码图片程序代码如下:

  1. <?php
  2. session_start();
  3. if( isset($_POST['submit'])) {
  4. if( $_SESSION['security_code'] == $_POST['security_code'] && !emptyempty($_SESSION['security_code'] ) ) {
  5. // Insert you code for processing the form here, e.g emailing the submission, entering it into a database.
  6. echo 'Thank you. Your message said "'.$_POST['message'].'"';
  7. unset($_SESSION['security_code']);
  8. } else {
  9. // Insert your code for showing an error message here
  10. echo 'Sorry, you have provided an invalid security code';
  11. }
  12. } else {
  13. ?>
  14. <form action="form.php" method="post">
  15. <label for="name">Name: </label><input type="text" name="name" /><br />
  16. <label for="email">Email: </label><input type="text" name="email" /><br />
  17. <label for="message">Message: </label><textarea rows="5" cols="30" name="message" ></textarea><br />
  18. <img src="CaptchaSecurityImages.php?width=100&height=40&characters=5" /><br />//开源代码phpfensi.com
  19. <label for="security_code">Security Code: </label><input name="security_code" type="text" /><br />
  20. <input type="submit" name="submit" value="Submit" />
  21. </form>
  22. <?php
  23. }
  24. ?>

验证程序 CaptchaSecurityImages.php,代码如下:

  1. <?php
  2. session_start();
  3. /*
  4. * File: CaptchaSecurityImages.php
  5. * Author: Simon Jarvis
  6. * Copyright: 2006 Simon Jarvis
  7. * Date: 03/08/06
  8. * Updated: 07/02/07
  9. * Requirements: PHP 4/5 with GD and FreeType libraries
  10. * Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version 2
  15. * of the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details:
  21. * http://www.gnu.org/licenses/gpl.html
  22. *
  23. */
  24. class CaptchaSecurityImages {
  25. var $font = 'monofont.ttf';
  26. function generateCode($characters) {
  27. /* list all possible characters, similar looking characters and vowels have been removed */
  28. $possible = '23456789bcdfghjkmnpqrstvwxyz';
  29. $code = '';
  30. $i = 0;
  31. while ($i < $characters) {
  32. $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
  33. $i++;
  34. }
  35. return $code;
  36. }
  37. function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
  38. $code = $this->generateCode($characters);
  39. /* font size will be 75% of the image height */
  40. $font_size = $height * 0.75;
  41. $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
  42. /* set the colours */
  43. $background_color = imagecolorallocate($image, 255, 255, 255);
  44. $text_color = imagecolorallocate($image, 20, 40, 100);
  45. $noise_color = imagecolorallocate($image, 100, 120, 180);
  46. /* generate random dots in background */
  47. for( $i=0; $i<($width*$height)/3; $i++ ) {
  48. imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
  49. }
  50. /* generate random lines in background */
  51. for( $i=0; $i<($width*$height)/150; $i++ ) {
  52. imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
  53. }
  54. /* create textbox and add text */
  55. $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
  56. $x = ($width - $textbox[4])/2;
  57. $y = ($height - $textbox[5])/2;
  58. imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
  59. /* output captcha image to browser */
  60. header('Content-Type: image/jpeg');
  61. imagejpeg($image);
  62. imagedestroy($image);
  63. $_SESSION['security_code'] = $code;
  64. }
  65. }
  66. $width = isset($_GET['width']) ? $_GET['width'] : '120';
  67. $height = isset($_GET['height']) ? $_GET['height'] : '40';
  68. $characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6';
  69. //开源代码phpfensi.com
  70. $captcha = new CaptchaSecurityImages($width,$height,$characters);
  71. ?>