captcha.class.php 5.0 KB

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