Вход Регистрация
* Taltos *

3D Captcha

  1. $capthca = new Capthca3d();
  2.  
  3. $capthca->render();
  4.  
  5.  
  6.  
  7.  
  8.  
  9. class Capthca3d{
  10.  
  11. const CHARS = 'WEafRTYIPAGHJKXBNM3479j';
  12.  
  13. protected $hypot = 8;
  14.  
  15. protected $image = null;
  16.  
  17.  
  18.  
  19. protected $text = '';
  20.  
  21.  
  22.  
  23. public function __construct()
  24.  
  25. {
  26.  
  27. $this->time = microtime(true);
  28.  
  29. $this->generateCode();
  30.  
  31.  
  32.  
  33. }
  34.  
  35. protected function generateCode()
  36.  
  37. {
  38.  
  39. $chars = self::CHARS;
  40.  
  41. for($i =0; $i<3; $i++){
  42.  
  43. $this->text .= $chars{ mt_rand(0,22)};
  44.  
  45. }
  46.  
  47. }
  48.  
  49.  
  50.  
  51. public function getText()
  52.  
  53. {
  54.  
  55. return $this->text;
  56.  
  57. }
  58.  
  59. protected function getProection($x1,$y1,$z1)
  60.  
  61. {
  62.  
  63. $x = $x1 * $this->hypot;
  64.  
  65. $y = $z1 * $this->hypot;
  66.  
  67. $z = -$y1 * $this->hypot;
  68.  
  69.  
  70.  
  71. $xx = 0.707106781187;
  72.  
  73. $xy = 0;
  74.  
  75. $xz = -0.707106781187;
  76.  
  77.  
  78.  
  79. $yx = 0.408248290464;
  80.  
  81. $yy = 0.816496580928;
  82.  
  83. $yz = 0.408248290464;
  84.  
  85.  
  86.  
  87. $cx = $xx*$x + $xy*$y + $xz*$z;
  88.  
  89. $cy = $yx*$x + $yy*$y + $yz*$z+ 20 * $this->hypot;
  90.  
  91. return array(
  92.  
  93. 'x' => $cx,
  94.  
  95. 'y' => $cy
  96.  
  97. );
  98.  
  99. }
  100.  
  101.  
  102.  
  103. function zFunction($x,$y){
  104.  
  105. $z = imagecolorat($this->image,$y/2,$x/2)>0?2.6:0;
  106.  
  107. if( $z != 0 ){
  108.  
  109. $z += mt_rand(0,60)/100;
  110.  
  111. }
  112.  
  113. $z += 1.4 * sin(($x+$this->startX)*3.141592654/15)*sin(($y+$this->startY)*3.141592654/15);
  114.  
  115. return $z;
  116.  
  117. }
  118.  
  119. public function render()
  120.  
  121. {
  122.  
  123. $xx =30;
  124.  
  125. $yy =60;
  126.  
  127.  
  128.  
  129. $this->image = imageCreateTrueColor($yy * $this->hypot , $xx * $this->hypot);
  130.  
  131.  
  132.  
  133. $whiteColor = imageColorAllocate($this->image,255,255,255);
  134.  
  135. imageFilledRectangle($this->image,0,0,$yy * $this->hypot , $xx * $this->hypot,$whiteColor);
  136.  
  137.  
  138.  
  139. $textColor = imageColorAllocate($this->image,0,0,0);
  140.  
  141. imageString($this->image, 5, 3, 0, $this->text, $textColor);
  142.  
  143.  
  144.  
  145.  
  146.  
  147. $this->startX = mt_rand(0,$xx);
  148.  
  149. $this->startY = mt_rand(0,$yy);
  150.  
  151.  
  152.  
  153. $coordinates = array();
  154.  
  155.  
  156.  
  157. for($x = 0; $x < $xx + 1; $x++){
  158.  
  159. for($y = 0; $y < $yy + 1; $y++){
  160.  
  161. $coordinates[$x][$y] = $this->getProection($x,$y,$this->zFunction($x,$y));
  162.  
  163. }
  164.  
  165. }
  166.  
  167.  
  168.  
  169. for($x = 0; $x < $xx; $x++){
  170.  
  171. for($y = 0; $y < $yy; $y++){
  172.  
  173. $coord = array();
  174.  
  175. $coord[] = $coordinates[$x][$y]['x'];
  176.  
  177. $coord[] = $coordinates[$x][$y]['y'];
  178.  
  179.  
  180.  
  181. $coord[] = $coordinates[$x+1][$y]['x'];
  182.  
  183. $coord[] = $coordinates[$x+1][$y]['y'];
  184.  
  185.  
  186.  
  187. $coord[] = $coordinates[$x+1][$y+1]['x'];
  188.  
  189. $coord[] = $coordinates[$x+1][$y+1]['y'];
  190.  
  191.  
  192.  
  193. $coord[] = $coordinates[$x][$y+1]['x'];
  194.  
  195. $coord[] = $coordinates[$x][$y+1]['y'];
  196.  
  197.  
  198.  
  199. $c = (int) ($this->zFunction($x,$y)*32);
  200.  
  201. $linesColor = imageColorAllocate($this->image, $c, $c, $c);
  202.  
  203. imageFilledPolygon($this->image, $coord, 4, $whiteColor);
  204.  
  205. imagePolygon($this->image, $coord, 4, $linesColor);
  206.  
  207. }
  208.  
  209. }
  210.  
  211.  
  212.  
  213. $textColor = imageColorAllocate($this->image,0,0,0);
  214.  
  215. imageString($this->image, 5, 3, 0, $this->text, $whiteColor);
  216.  
  217. imageString($this->image, 1, 3, 0, (microtime(true)-$this->time), $textColor);
  218.  
  219. header('Content-Type: image/png');
  220.  
  221.  
  222.  
  223. imagepng($this->image);
  224.  
  225. imagedestroy($this->image);
  226.  
  227. }
  228.  
  229. }
» Описание: Реализация
капчи, все четко и ничего лишнего, в стиле ооп
» Время добавления: 12 Марта 2015 в 11:19
» Посмотров: 1492
» textarea
» Рейтинг: [+0 | -0]
Комментарии [0]
Онлайн: 1
Реклама