3D captcha

  1. <?php
  2. /* 3D Captcha */
  3.  
  4. /*
  5. Реализация
  6. капчи, все четко и ничего лишнего, в стиле ооп
  7. */
  8.  
  9. $capthca = new Capthca3d();
  10. $capthca->render();
  11.  
  12.  
  13. class Capthca3d{
  14. const CHARS = 'WEafRTYIPAGHJKXBNM3479j';
  15. protected $hypot = 8;
  16. protected $image = null;
  17.  
  18. protected $text = '';
  19.  
  20. public function __construct()
  21. {
  22. $this->time = microtime(true);
  23. $this->generateCode();
  24.  
  25. }
  26. protected function generateCode()
  27. {
  28. $chars = self::CHARS;
  29. for($i =0; $i<3; $i++){
  30. $this->text .= $chars{ mt_rand(0,22)};
  31. }
  32. }
  33.  
  34. public function getText()
  35. {
  36. return $this->text;
  37. }
  38. protected function getProection($x1,$y1,$z1)
  39. {
  40. $x = $x1 * $this->hypot;
  41. $y = $z1 * $this->hypot;
  42. $z = -$y1 * $this->hypot;
  43.  
  44. $xx = 0.707106781187;
  45. $xy = 0;
  46. $xz = -0.707106781187;
  47.  
  48. $yx = 0.408248290464;
  49. $yy = 0.816496580928;
  50. $yz = 0.408248290464;
  51.  
  52. $cx = $xx*$x + $xy*$y + $xz*$z;
  53. $cy = $yx*$x + $yy*$y + $yz*$z+ 20 * $this->hypot;
  54. return array(
  55. 'x' => $cx,
  56. 'y' => $cy
  57. );
  58. }
  59.  
  60. function zFunction($x,$y){
  61. $z = imagecolorat($this->image,$y/2,$x/2)>0?2.6:0;
  62. if( $z != 0 ){
  63. $z += mt_rand(0,60)/100;
  64. }
  65. $z += 1.4 * sin(($x+$this->startX)*3.141592654/15)*sin(($y+$this->startY)*3.141592654/15);
  66. return $z;
  67. }
  68. public function render()
  69. {
  70. $xx =30;
  71. $yy =60;
  72.  
  73. $this->image = imageCreateTrueColor($yy * $this->hypot , $xx * $this->hypot);
  74.  
  75. $whiteColor = imageColorAllocate($this->image,255,255,255);
  76. imageFilledRectangle($this->image,0,0,$yy * $this->hypot , $xx * $this->hypot,$whiteColor);
  77.  
  78. $textColor = imageColorAllocate($this->image,0,0,0);
  79. imageString($this->image, 5, 3, 0, $this->text, $textColor);
  80.  
  81.  
  82. $this->startX = mt_rand(0,$xx);
  83. $this->startY = mt_rand(0,$yy);
  84.  
  85. $coordinates = array();
  86.  
  87. for($x = 0; $x < $xx + 1; $x++){
  88. for($y = 0; $y < $yy + 1; $y++){
  89. $coordinates[$x][$y] = $this->getProection($x,$y,$this->zFunction($x,$y));
  90. }
  91. }
  92.  
  93. for($x = 0; $x < $xx; $x++){
  94. for($y = 0; $y < $yy; $y++){
  95. $coord = array();
  96. $coord[] = $coordinates[$x][$y]['x'];
  97. $coord[] = $coordinates[$x][$y]['y'];
  98.  
  99. $coord[] = $coordinates[$x+1][$y]['x'];
  100. $coord[] = $coordinates[$x+1][$y]['y'];
  101.  
  102. $coord[] = $coordinates[$x+1][$y+1]['x'];
  103. $coord[] = $coordinates[$x+1][$y+1]['y'];
  104.  
  105. $coord[] = $coordinates[$x][$y+1]['x'];
  106. $coord[] = $coordinates[$x][$y+1]['y'];
  107.  
  108. $c = (int) ($this->zFunction($x,$y)*32);
  109. $linesColor = imageColorAllocate($this->image, $c, $c, $c);
  110. imageFilledPolygon($this->image, $coord, 4, $whiteColor);
  111. imagePolygon($this->image, $coord, 4, $linesColor);
  112. }
  113. }
  114.  
  115. $textColor = imageColorAllocate($this->image,0,0,0);
  116. imageString($this->image, 5, 3, 0, $this->text, $whiteColor);
  117. imageString($this->image, 1, 3, 0, (microtime(true)-$this->time), $textColor);
  118. header('Content-Type: image/png');
  119.  
  120. imagepng($this->image);
  121. imagedestroy($this->image);
  122. }
  123. }
  124.  
  125. ?>
Реализация
капчи, все четко и ничего лишнего, в стиле ооп

Реклама

Мы в соцсетях

tw tg yt gt