Pull to refresh

Море волнуется раз ..., или Капча 4D

Reading time 2 min
Views 4K
Как то я уже писал топик про каптчу 3D
Дай карму! :)
А вчера решил слегка переделать, добавить рюшечек и еще одно измерение.
Долго сказка пишется, да быстро дело делается, рушил сделал и вот наваял гиперкуб Капчу 4D.

Пара примеров:
image
image
Код также прилагается:
  1. <pre>
  2. <?php
  3. /**
  4. * @author Andrii Kasian
  5. */
  6. $captcha = new Captcha4d();
  7. $captcha->render();
  8. class Captcha4d{
  9. const CHARS = 'WEafRTYIPAGHJKXBNM3479j';
  10. protected $hypot = 5;
  11. protected $image = null;
  12. protected $_sin = array();
  13. protected $text = '';
  14. public function __construct()
  15. {
  16. $this->time = microtime(true);
  17. $this->generateCode();
  18. }
  19. protected function generateCode()
  20. {
  21. $chars = self::CHARS;
  22. for($i =0; $i<3; $i++){
  23. $this->text .= $chars{ mt_rand(0,22)};
  24. }
  25. }
  26. public function getText()
  27. {
  28. return $this->text;
  29. }
  30. protected function getProection($x, $y, $z)
  31. {
  32. $xx = 0.70710;
  33. $xz = 0;
  34. $xy = 0.70710;
  35. $yx = 0.40824;
  36. $yz = 0.81649;
  37. $yy = -0.40824;
  38. $cx = $xx*$x + $xy*$y + $xz*$z - 5;
  39. $cy = $yx*$x + $yy*$y + $yz*$z + 20;
  40. return array(
  41. 'x' => $cx * $this->hypot,
  42. 'y' => $cy * $this->hypot
  43. );
  44. }
  45. function zFunction($x,$y){
  46. $z = imagecolorat($this->image,$y/2,$x/2)>0?3:0;
  47. if( $z != 0 ){
  48. $z += -2+ 2*
  49. $this->_sin[($x+$this->startX)%30]
  50. *
  51. $this->_sin[($y+$this->startY)%30];
  52. }
  53. $z += mt_rand(0,30)/50;
  54. return $z;
  55. }
  56. public function render()
  57. {
  58. $xx = 30;
  59. $yy = 60;
  60. $animation = new Imagick();
  61. $animation->setFormat( "gif" );
  62. $cw = new ImagickPixel("white");
  63. $cb = new ImagickPixel("#000000");
  64. $this->image = imageCreateTrueColor(100, 20);
  65. $whiteColor = imageColorAllocate($this->image,255,255,255);
  66. imageFilledRectangle($this->image,0,0,$yy * $this->hypot , $xx * $this->hypot, $whiteColor);
  67. $textColor = imageColorAllocate($this->image,0,0,0);
  68. imageString($this->image, 5, 3, 0, $this->text, $textColor);
  69. $cof = 2*3.141592654/$xx;
  70. for($x = 0; $x < $xx + 1; $x++){
  71. $this->_sin[$x] = sin($x*$cof);
  72. }
  73. $this->startX = mt_rand(0,$xx);
  74. $this->startY = mt_rand(0,$yy);
  75. $draw = new ImagickDraw();
  76. $countFrame = 25;
  77. for ( $i = 0; $i < $countFrame; $i++ ) {
  78. $this->startX += $xx / $countFrame;
  79. $coordinates = array();
  80. for($x = 0; $x < $xx + 1; $x++){
  81. for($y = 0; $y < $yy + 1; $y++){
  82. $coordinates[$x][$y] = $this->getProection($x,$y,$this->zFunction($x,$y));
  83. }
  84. }
  85. $animation->newImage( $yy * $this->hypot , $xx * $this->hypot, $cw);
  86. $im = new ImagickDraw();
  87. $im->setFillColor($cw);
  88. $im->setStrokeColor($cb);
  89. $im->setStrokeAntialias(true);
  90. for($x = 0; $x < $xx; $x++){
  91. for($y = 0; $y < $yy; $y++){
  92. $coord = array();
  93. $coord[] = $coordinates[$x][$y];
  94. $coord[] = $coordinates[$x+1][$y];
  95. $coord[] = $coordinates[$x+1][$y+1];
  96. $coord[] = $coordinates[$x][$y+1];
  97. $im->polygon($coord);
  98. }
  99. }
  100. $animation->drawImage($im);
  101. $animation->setImageDelay( 100/$countFrame );
  102. }
  103. header( "Content-Type: image/gif" );
  104. echo $animation->getImagesBlob();die();
  105. }
  106. }


Приятного понедельника, хабра товарищи!

PS. Эта капча не предназначено для использования в качестве капчи (ломает пользователям мозг, и забивает канал трафиком)
Tags:
Hubs:
+185
Comments 139
Comments Comments 139

Articles