/phpsso_server/phpcms/libs/classes/checkcode.class.php

https://github.com/hxzyzz/ddc · PHP · 143 lines · 81 code · 24 blank · 38 comment · 11 complexity · 464d23134b3ec8927fb34386b65921ac MD5 · raw file

  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. public $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. $this->font = PC_PATH.'libs'.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'font'.DIRECTORY_SEPARATOR.'elephant.ttf';
  36. }
  37. /**
  38. * Éú³ÉËæ»úÑéÖ¤Âë¡£
  39. */
  40. protected function creat_code() {
  41. $code = '';
  42. $charset_len = strlen($this->charset)-1;
  43. for ($i=0; $i<$this->code_len; $i++) {
  44. $code .= $this->charset[rand(1, $charset_len)];
  45. }
  46. $this->code = $code;
  47. }
  48. /**
  49. * »ñÈ¡ÑéÖ¤Âë
  50. */
  51. public function get_code() {
  52. return strtolower($this->code);
  53. }
  54. /**
  55. * Éú³ÉͼƬ
  56. */
  57. public function doimage() {
  58. $this->creat_code();
  59. $this->img = imagecreatetruecolor($this->width, $this->height);
  60. if (!$this->font_color) {
  61. $this->font_color = imagecolorallocate($this->img, rand(0,156), rand(0,156), rand(0,156));
  62. } else {
  63. $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)));
  64. }
  65. //ÉèÖñ³¾°É«
  66. $background = imagecolorallocate($this->img,hexdec(substr($this->background, 1,2)),hexdec(substr($this->background, 3,2)),hexdec(substr($this->background, 5,2)));
  67. //»­Ò»¸ö¹ñÐΣ¬ÉèÖñ³¾°ÑÕÉ«¡£
  68. imagefilledrectangle($this->img,0, $this->height, $this->width, 0, $background);
  69. $this->creat_font();
  70. $this->creat_line();
  71. $this->output();
  72. }
  73. /**
  74. * Éú³ÉÎÄ×Ö
  75. */
  76. private function creat_font() {
  77. $x = $this->width/$this->code_len;
  78. for ($i=0; $i<$this->code_len; $i++) {
  79. 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]);
  80. if($i==0)$this->x_start=$x*$i+5;
  81. }
  82. }
  83. /**
  84. * »­Ïß
  85. */
  86. private function creat_line() {
  87. imagesetthickness($this->img, 3);
  88. $xpos = ($this->font_size * 2) + rand(-5, 5);
  89. $width = $this->width / 2.66 + rand(3, 10);
  90. $height = $this->font_size * 2.14;
  91. if ( rand(0,100) % 2 == 0 ) {
  92. $start = rand(0,66);
  93. $ypos = $this->height / 2 - rand(10, 30);
  94. $xpos += rand(5, 15);
  95. } else {
  96. $start = rand(180, 246);
  97. $ypos = $this->height / 2 + rand(10, 30);
  98. }
  99. $end = $start + rand(75, 110);
  100. imagearc($this->img, $xpos, $ypos, $width, $height, $start, $end, $this->font_color);
  101. $color = $colors[rand(0, sizeof($colors) - 1)];
  102. if ( rand(1,75) % 2 == 0 ) {
  103. $start = rand(45, 111);
  104. $ypos = $this->height / 2 - rand(10, 30);
  105. $xpos += rand(5, 15);
  106. } else {
  107. $start = rand(200, 250);
  108. $ypos = $this->height / 2 + rand(10, 30);
  109. }
  110. $end = $start + rand(75, 100);
  111. imagearc($this->img, $this->width * .75, $ypos, $width, $height, $start, $end, $this->font_color);
  112. }
  113. //Êä³öͼƬ
  114. private function output() {
  115. header("content-type:image/png\r\n");
  116. imagepng($this->img);
  117. imagedestroy($this->img);
  118. }
  119. }