checkcode.class.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * 生成验证码
  4. * @author chenzhouyu
  5. * 类用法
  6. * $checkcode = new checkcode();
  7. * $checkcode->doimage();
  8. * //取得验证
  9. * $_SESSION['code']=$checkcode->get_code();
  10. */
  11. class checkcode {
  12. //验证码的宽度
  13. public $width=130;
  14. //验证码的高
  15. public $height=50;
  16. //设置字体的地址
  17. private $font;
  18. //设置字体色
  19. public $font_color;
  20. //设置随机生成因子
  21. public $charset = 'abcdefghkmnprstuvwyzABCDEFGHKLMNPRSTUVWYZ23456789';
  22. //设置背景色
  23. public $background = '#EDF7FF';
  24. //生成验证码字符数
  25. public $code_len = 4;
  26. //字体大小
  27. public $font_size = 20;
  28. //验证码
  29. private $code;
  30. //图片内存
  31. private $img;
  32. //文字X轴开始的地方
  33. private $x_start;
  34. function __construct() {
  35. $rand = rand(0,1);
  36. if($rand==0) {
  37. $this->font = PC_PATH.'libs'.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'font'.DIRECTORY_SEPARATOR.'elephant.ttf';
  38. } else {
  39. $this->font = PC_PATH.'libs'.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'font'.DIRECTORY_SEPARATOR.'Vineta.ttf';
  40. }
  41. }
  42. /**
  43. * 生成随机验证码。
  44. */
  45. protected function creat_code() {
  46. $code = '';
  47. $charset_len = strlen($this->charset)-1;
  48. for ($i=0; $i<$this->code_len; $i++) {
  49. $code .= $this->charset[rand(1, $charset_len)];
  50. }
  51. $this->code = $code;
  52. }
  53. /**
  54. * 获取验证码
  55. */
  56. public function get_code() {
  57. return strtolower($this->code);
  58. }
  59. /**
  60. * 生成图片
  61. */
  62. public function doimage() {
  63. $code = $this->creat_code();
  64. $this->img = imagecreatetruecolor($this->width, $this->height);
  65. if (!$this->font_color) {
  66. $this->font_color = imagecolorallocate($this->img, rand(0,156), rand(0,156), rand(0,156));
  67. } else {
  68. $this->font_color = imagecolorallocate($this->img, hexdec(substr($this->font_color, 1,2)), hexdec(substr($this->font_color, 3,2)), hexdec(substr($this->font_color, 5,2)));
  69. }
  70. //设置背景色
  71. $background = imagecolorallocate($this->img,hexdec(substr($this->background, 1,2)),hexdec(substr($this->background, 3,2)),hexdec(substr($this->background, 5,2)));
  72. //画一个柜形,设置背景颜色。
  73. imagefilledrectangle($this->img,0, $this->height, $this->width, 0, $background);
  74. $this->creat_font();
  75. $this->creat_line();
  76. $this->output();
  77. }
  78. /**
  79. * 生成文字
  80. */
  81. private function creat_font() {
  82. $x = $this->width/$this->code_len;
  83. for ($i=0; $i<$this->code_len; $i++) {
  84. imagettftext($this->img, $this->font_size, rand(-30,30), $x*$i+rand(0,5), $this->height/1.4, $this->font_color, $this->font, $this->code[$i]);
  85. if($i==0)$this->x_start=$x*$i+5;
  86. }
  87. }
  88. /**
  89. * 画线
  90. */
  91. private function creat_line() {
  92. imagesetthickness($this->img, 3);
  93. $xpos = ($this->font_size * 2) + rand(-5, 5);
  94. $width = $this->width / 2.66 + rand(3, 10);
  95. $height = $this->font_size * 2.14;
  96. if ( rand(0,100) % 2 == 0 ) {
  97. $start = rand(0,66);
  98. $ypos = $this->height / 2 - rand(10, 30);
  99. $xpos += rand(5, 15);
  100. } else {
  101. $start = rand(180, 246);
  102. $ypos = $this->height / 2 + rand(10, 30);
  103. }
  104. $end = $start + rand(75, 110);
  105. imagearc($this->img, $xpos, $ypos, $width, $height, $start, $end, $this->font_color);
  106. if ( rand(1,75) % 2 == 0 ) {
  107. $start = rand(45, 111);
  108. $ypos = $this->height / 2 - rand(10, 30);
  109. $xpos += rand(5, 15);
  110. } else {
  111. $start = rand(200, 250);
  112. $ypos = $this->height / 2 + rand(10, 30);
  113. }
  114. $end = $start + rand(75, 100);
  115. imagearc($this->img, $this->width * .75, $ypos, $width, $height, $start, $end, $this->font_color);
  116. }
  117. /**
  118. * 输出图片
  119. */
  120. private function output() {
  121. header("content-type:image/png\r\n");
  122. imagepng($this->img);
  123. imagedestroy($this->img);
  124. }
  125. }