captcha.class.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. defined('IN_IA') or exit('Access Denied');
  3. class Captcha {
  4. public $maxAngle = 15; /*角度*/
  5. public $maxOffset = 5; /*偏移量*/
  6. public $phrase = '';
  7. public function build($width, $height) {
  8. $image = @imagecreatetruecolor($width, $height);
  9. if(empty($image)) {
  10. return error('1', 'Not supplied GD');
  11. }
  12. $bg = imagecolorallocate($image, $this->rand(200, 255), $this->rand(200, 255), $this->rand(200, 255));
  13. imagefill($image, 0, 0, $bg);
  14. //干扰线
  15. $square = $width * $height * 3;
  16. $effects = $this->rand($square/2000, $square/1000);
  17. for ($e = 0; $e < $effects; $e++) {
  18. $this->drawLine($image, $width, $height);
  19. }
  20. $this->phrase = $this->phrase();
  21. $color = $this->writePhrase($image, $this->phrase, $this->font(), $width, $height);
  22. $square = $width * $height;
  23. $effects = $this->rand($square/3000, $square/2000);
  24. if ($this->maxFrontLines !== 0) {
  25. for ($e = 0; $e < $effects; $e++) {
  26. $this->drawLine($image, $width, $height, $color);
  27. }
  28. }
  29. $image = $this->distort($image, $width, $height, $bg);
  30. $this->image = $image;
  31. return $this;
  32. }
  33. public function output($quality = 90) {
  34. header('content-type: image/png');
  35. imagepng($this->image);
  36. imagedestroy($this->image);
  37. }
  38. protected function phrase() {
  39. //默认为字母数字,将来可扩展汉字
  40. return random(4, true);
  41. }
  42. protected function rand($min, $max) {
  43. mt_srand((double) microtime() * 1000000);
  44. return mt_rand($min, $max);
  45. }
  46. protected function drawLine($image, $width, $height, $tcol = null) {
  47. if ($tcol === null) {
  48. $tcol = imagecolorallocate($image, $this->rand(100, 255), $this->rand(100, 255), $this->rand(100, 255));
  49. }
  50. if ($this->rand(0, 1)) { // Horizontal
  51. $Xa = $this->rand(0, $width/2);
  52. $Ya = $this->rand(0, $height);
  53. $Xb = $this->rand($width/2, $width);
  54. $Yb = $this->rand(0, $height);
  55. } else { // Vertical
  56. $Xa = $this->rand(0, $width);
  57. $Ya = $this->rand(0, $height/2);
  58. $Xb = $this->rand(0, $width);
  59. $Yb = $this->rand($height/2, $height);
  60. }
  61. imagesetthickness($image, $this->rand(1, 3));
  62. imageline($image, $Xa, $Ya, $Xb, $Yb, $tcol);
  63. }
  64. protected function writePhrase($image, $phrase, $font, $width, $height) {
  65. $size = $width / strlen($phrase) - $this->rand(0, 3) - 1;
  66. $box = imagettfbbox($size, 0, $font, $phrase);
  67. $textWidth = $box[2] - $box[0];
  68. $textHeight = $box[1] - $box[7];
  69. $x = ($width - $textWidth) / 2;
  70. $y = ($height - $textHeight) / 2 + $size;
  71. $textColor = array($this->rand(0, 150), $this->rand(0, 150), $this->rand(0, 150));
  72. $col = imagecolorallocate($image, $textColor[0], $textColor[1], $textColor[2]);
  73. $length = strlen($phrase);
  74. for ($i=0; $i<$length; $i++) {
  75. $box = imagettfbbox($size, 0, $font, $phrase[$i]);
  76. $w = $box[2] - $box[0];
  77. $angle = $this->rand(-$this->maxAngle, $this->maxAngle);
  78. $offset = $this->rand(-$this->maxOffset, $this->maxOffset);
  79. imagettftext($image, $size, $angle, $x, $y + $offset, $col, $font, $phrase[$i]);
  80. $x += $w;
  81. }
  82. return $col;
  83. }
  84. public function distort($image, $width, $height, $bg) {
  85. $contents = imagecreatetruecolor($width, $height);
  86. $X = $this->rand(0, $width);
  87. $Y = $this->rand(0, $height);
  88. $phase = $this->rand(0, 10);
  89. $scale = 1.1 + $this->rand(0, 10000) / 30000;
  90. for ($x = 0; $x < $width; $x++) {
  91. for ($y = 0; $y < $height; $y++) {
  92. $Vx = $x - $X;
  93. $Vy = $y - $Y;
  94. $Vn = sqrt($Vx * $Vx + $Vy * $Vy);
  95. if ($Vn != 0) {
  96. $Vn2 = $Vn + 4 * sin($Vn / 30);
  97. $nX = $X + ($Vx * $Vn2 / $Vn);
  98. $nY = $Y + ($Vy * $Vn2 / $Vn);
  99. } else {
  100. $nX = $X;
  101. $nY = $Y;
  102. }
  103. $nY = $nY + $scale * sin($phase + $nX * 0.2);
  104. $p = $this->interpolate(
  105. $nX - floor($nX),
  106. $nY - floor($nY),
  107. $this->getCol($image, floor($nX), floor($nY), $bg),
  108. $this->getCol($image, ceil($nX), floor($nY), $bg),
  109. $this->getCol($image, floor($nX), ceil($nY), $bg),
  110. $this->getCol($image, ceil($nX), ceil($nY), $bg)
  111. );
  112. if ($p == 0) {
  113. $p = $bg;
  114. }
  115. imagesetpixel($contents, $x, $y, $p);
  116. }
  117. }
  118. return $contents;
  119. }
  120. protected function interpolate($x, $y, $nw, $ne, $sw, $se) {
  121. list($r0, $g0, $b0) = $this->getRGB($nw);
  122. list($r1, $g1, $b1) = $this->getRGB($ne);
  123. list($r2, $g2, $b2) = $this->getRGB($sw);
  124. list($r3, $g3, $b3) = $this->getRGB($se);
  125. $cx = 1.0 - $x;
  126. $cy = 1.0 - $y;
  127. $m0 = $cx * $r0 + $x * $r1;
  128. $m1 = $cx * $r2 + $x * $r3;
  129. $r = (int) ($cy * $m0 + $y * $m1);
  130. $m0 = $cx * $g0 + $x * $g1;
  131. $m1 = $cx * $g2 + $x * $g3;
  132. $g = (int) ($cy * $m0 + $y * $m1);
  133. $m0 = $cx * $b0 + $x * $b1;
  134. $m1 = $cx * $b2 + $x * $b3;
  135. $b = (int) ($cy * $m0 + $y * $m1);
  136. return ($r << 16) | ($g << 8) | $b;
  137. }
  138. protected function getRGB($col) {
  139. return array(
  140. (int) ($col >> 16) & 0xff,
  141. (int) ($col >> 8) & 0xff,
  142. (int) ($col) & 0xff,
  143. );
  144. }
  145. protected function getCol($image, $x, $y, $background) {
  146. $L = imagesx($image);
  147. $H = imagesy($image);
  148. if ($x < 0 || $x >= $L || $y < 0 || $y >= $H) {
  149. return $background;
  150. }
  151. return imagecolorat($image, $x, $y);
  152. }
  153. protected function font() {
  154. return IA_ROOT . '/web/resource/fonts/captcha.ttf';
  155. }
  156. }
  157. ?>