Просмотр файла engine/classes/captcha.php

Размер файла: 6.32Kb
  1. <?php
  2.  
  3. class SimpleCaptcha {
  4.  
  5. public $width = 150;
  6. public $height = 60;
  7. public $minWordLength = 3;
  8. public $maxWordLength = 6;
  9. public $session_var = 'captcha';
  10. public $backgroundColor = array(255, 255, 255);
  11. public $colors = array(
  12. array(27,78,181), array(22,163,35), array(214,36,7), );
  13.  
  14. public $shadowColor = null;
  15. public $lineWidth = 0;
  16. public $Yperiod = 12;
  17. public $Yamplitude = 14;
  18. public $Xperiod = 11;
  19. public $Xamplitude = 5;
  20. public $maxRotation = 7;
  21. public $scale = 2;
  22. public $blur = false;
  23. public $debug = false;
  24. public $im;
  25. public $font_config = array('spacing' => 1, 'minSize' => 17, 'maxSize' => 19);
  26.  
  27. public function __construct($config = array()) {
  28. }
  29.  
  30. public function CreateImage() {
  31. $ini = microtime(true);
  32.  
  33. $this->ImageAllocate();
  34. $text = $this->GetRandomCaptchaText();
  35. $this->WriteText($text);
  36.  
  37. $_SESSION[$this->session_var] = $text;
  38.  
  39. if (!empty($this->lineWidth)) {
  40. $this->WriteLine();
  41. }
  42. $this->WaveImage();
  43. if ($this->blur && function_exists('imagefilter')) {
  44. imagefilter($this->im, IMG_FILTER_GAUSSIAN_BLUR);
  45. }
  46. $this->ReduceImage();
  47.  
  48.  
  49. if ($this->debug) {
  50. imagestring($this->im, 1, 1, $this->height-8,
  51. "$text ".round((microtime(true)-$ini), 4)."ms",
  52. $this->GdFgColor
  53. );
  54. }
  55.  
  56.  
  57. $this->WriteImage();
  58. }
  59.  
  60.  
  61. protected function ImageAllocate() {
  62. if (!empty($this->im)) {
  63. imagedestroy($this->im);
  64. }
  65.  
  66. $this->im = imagecreatetruecolor($this->width*$this->scale, $this->height*$this->scale);
  67.  
  68. $this->GdBgColor = imagecolorallocate($this->im,
  69. $this->backgroundColor[0],
  70. $this->backgroundColor[1],
  71. $this->backgroundColor[2]
  72. );
  73. //imagecolortransparent($this->im, $this->GdBgColor);
  74. imagefilledrectangle($this->im, 0, 0, $this->width*$this->scale, $this->height*$this->scale, $this->GdBgColor);
  75.  
  76. $color = $this->colors[mt_rand(0, sizeof($this->colors)-1)];
  77. $this->GdFgColor = imagecolorallocate($this->im, $color[0], $color[1], $color[2]);
  78.  
  79. if (!empty($this->shadowColor) && is_array($this->shadowColor) && sizeof($this->shadowColor) >= 3) {
  80. $this->GdShadowColor = imagecolorallocate($this->im,
  81. $this->shadowColor[0],
  82. $this->shadowColor[1],
  83. $this->shadowColor[2]
  84. );
  85. }
  86. }
  87.  
  88. protected function GetRandomCaptchaText($length = null) {
  89. if (empty($length)) {
  90. $length = rand($this->minWordLength, $this->maxWordLength);
  91. }
  92.  
  93. $words = 'bcdfghjlmnpqrstvwyz';
  94. $vocals = 'aeoui';
  95.  
  96. $text = null;
  97. $vocal = rand(0, 1);
  98. while (strlen($text) < $length)
  99. {
  100. if ($vocal) {
  101. $text .= substr($vocals, mt_rand(0, 4), 1); //$vocals{mt_rand(0, 4)}; //
  102. $vocal = false;
  103. } else {
  104. $text .= substr($words, mt_rand(0, 22), 1); //$words{mt_rand(0, 22)};
  105. $vocal = true;
  106. }
  107. }
  108. return $text;
  109. }
  110.  
  111.  
  112. protected function WriteLine() {
  113.  
  114. $x1 = $this->width*$this->scale*.15;
  115. $x2 = $this->textFinalX;
  116. $y1 = rand($this->height*$this->scale*.40, $this->height*$this->scale*.65);
  117. $y2 = rand($this->height*$this->scale*.40, $this->height*$this->scale*.65);
  118. $width = $this->lineWidth/2*$this->scale;
  119.  
  120. for ($i = $width*-1; $i <= $width; $i++) {
  121. imageline($this->im, $x1, $y1+$i, $x2, $y2+$i, $this->GdFgColor);
  122. }
  123. }
  124.  
  125.  
  126.  
  127.  
  128. protected function WriteText($text) {
  129.  
  130. $fontfile = $_SERVER['DOCUMENT_ROOT'] . '/engine/files/data/font.ttf';
  131. $lettersMissing = $this->maxWordLength-strlen($text);
  132. $fontSizefactor = 1+($lettersMissing*0.09);
  133.  
  134. $x = 20*$this->scale;
  135. $y = round(($this->height*27/40)*$this->scale);
  136. $length = strlen($text);
  137. for ($i=0; $i<$length; $i++) {
  138. $degree = rand($this->maxRotation*-1, $this->maxRotation);
  139. $fontsize = rand($this->font_config['minSize'], $this->font_config['maxSize'])*$this->scale*$fontSizefactor;
  140. $letter = substr($text, $i, 1);
  141.  
  142. if ($this->shadowColor) {
  143. $coords = imagettftext($this->im, $fontsize, $degree,
  144. $x+$this->scale, $y+$this->scale,
  145. $this->GdShadowColor, $fontfile, $letter);
  146. }
  147. $coords = imagettftext($this->im, $fontsize, $degree,
  148. $x, $y,
  149. $this->GdFgColor, $fontfile, $letter);
  150. $x += ($coords[2]-$x) + ($this->font_config['spacing'] * $this->scale);
  151. }
  152.  
  153. $this->textFinalX = $x;
  154. }
  155.  
  156. protected function WaveImage() {
  157. $xp = $this->scale*$this->Xperiod*rand(1,3);
  158. $k = rand(0, 100);
  159. for ($i = 0; $i < ($this->width*$this->scale); $i++) {
  160. imagecopy($this->im, $this->im,
  161. $i-1, sin($k+$i/$xp) * ($this->scale*$this->Xamplitude),
  162. $i, 0, 1, $this->height*$this->scale);
  163. }
  164.  
  165. $k = rand(0, 100);
  166. $yp = $this->scale*$this->Yperiod*rand(1,2);
  167. for ($i = 0; $i < ($this->height*$this->scale); $i++) {
  168. imagecopy($this->im, $this->im,
  169. sin($k+$i/$yp) * ($this->scale*$this->Yamplitude), $i-1,
  170. 0, $i, $this->width*$this->scale, 1);
  171. }
  172. }
  173.  
  174. protected function ReduceImage() {
  175. $imResampled = imagecreatetruecolor($this->width, $this->height);
  176. imagecopyresampled($imResampled, $this->im,
  177. 0, 0, 0, 0,
  178. $this->width, $this->height,
  179. $this->width*$this->scale, $this->height*$this->scale
  180. );
  181. imagedestroy($this->im);
  182. $this->im = $imResampled;
  183. }
  184.  
  185.  
  186. protected function WriteImage() {
  187. header("Content-type: image/png");
  188. imagepng($this->im);
  189. imagedestroy($this->im);
  190. }
  191. }