PHP动态生成图片验证码的程序源代码

后端开发   发布日期:2023年05月13日   浏览次数:482

1.PHP生成验证码图片的核心代码:

  1. get_code.php
  2.  
  3. < ?php session_start();
  4. //验证码类
  5. class ValidateCode {
  6. private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789'; //随机因子
  7. private $code; //验证码
  8. private $codelen = 4; //验证码长度
  9. private $width = 90; //宽度
  10. private $height = 30; //高度
  11. private $img; //图形资源句柄
  12. private $font; //指定的字体
  13. private $fontsize = 17; //指定字体大小
  14. private $fontcolor; //指定字体颜色
  15. //构造方法初始化
  16. public
  17. function __construct() {
  18. $this - >font = dirname(__FILE__).'/css/font/Elephant.ttf'; //注意字体路径要写对,否则显示不了图片
  19. }
  20. //生成随机码
  21. private
  22. function createCode() {
  23. $_len = strlen($this - >charset) - 1;
  24. for ($i = 0; $i < $this - >codelen; $i++) {
  25. $this - >code. = $this - >charset[mt_rand(0, $_len)];
  26. }
  27. }
  28. //生成背景
  29. private
  30. function createBg() {
  31. $this - >img = imagecreatetruecolor($this - >width, $this - >height);
  32. $color = imagecolorallocate($this - >img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
  33. imagefilledrectangle($this - >img, 0, $this - >height, $this - >width, 0, $color);
  34. }
  35. //生成文字
  36. private
  37. function createFont() {
  38. $_x = $this - >width / $this - >codelen;
  39. for ($i = 0; $i < $this - >codelen; $i++) {
  40. $this - >fontcolor = imagecolorallocate($this - >img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
  41. imagettftext($this - >img, $this - >fontsize, mt_rand( - 30, 30), $_x * $i + mt_rand(1, 5), $this - >height / 1.4, $this - >fontcolor, $this - >font, $this - >code[$i]);
  42. }
  43. }
  44. //生成线条、雪花
  45. private
  46. function createLine() {
  47. //线条
  48. for ($i = 0; $i < 6; $i++) {
  49. $color = imagecolorallocate($this - >img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
  50. imageline($this - >img, mt_rand(0, $this - >width), mt_rand(0, $this - >height), mt_rand(0, $this - >width), mt_rand(0, $this - >height), $color);
  51. }
  52. //雪花
  53. for ($i = 0; $i < 100; $i++) {
  54. $color = imagecolorallocate($this - >img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
  55. imagestring($this - >img, mt_rand(1, 5), mt_rand(0, $this - >width), mt_rand(0, $this - >height), '*', $color);
  56. }
  57. }
  58. //输出
  59. private
  60. function outPut() {
  61. header('Content-type:image/jpeg');
  62. imagepng($this - >img);
  63. imagedestroy($this - >img);
  64. }
  65. //对外生成
  66. public
  67. function doimg() {
  68. $this - >createBg();
  69. $this - >createCode();
  70. $this - >createLine();
  71. $this - >createFont();
  72. $this - >outPut();
  73. }
  74. //获取验证码
  75. public
  76. function getCode() {
  77. return strtoupper($this - >code);
  78. }
  79. }
  80. $_vc = new ValidateCode(); //实例化一个对象
  81. $_vc - >doimg();
  82.  
  83. $_SESSION['login_check_code'] = $_vc - >getCode(); //验证码保存到SESSION中
  84. ? >

2.在HTML使用PHP验证码的方法

  1. <div id="codeimg">
  2. <img id="refresh" src="get_code.php" style="cursor:pointer">
  3. </div>

3.提交时验证验证码是否正确

  1. $code=strtoupper($_POST['code']); //统一都将验证码字符转为大写字母
  2.  
  3. if ($code!=$_SESSION["login_check_code"]){
  4. echo "您获取的验证码不正确";
  5. exit;
  6. }

以上就是PHP动态生成图片验证码的程序源代码的详细内容,更多关于PHP动态生成图片验证码的程序源代码的资料请关注九品源码其它相关文章!