Капча 4D на php

Февраль 2nd, 2010 по admin Оставить ответ »

Наткнулся на Хабре на код капчи которая ламает моск :
пришлось немного подправить, ибо код приведенный на хабре потерял символы…….

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

9 комментариев

  1. Dimuns:

    Прежде чем публиковать проверили бы работает она или нет!

  2. admin:

    сорри, немного код подправил, теперь все работает как часы.

  3. Артем:

    у меня чет на 69й строка ошибку выдает
    Class ‘Imagick’ not found in … on line 69

  4. admin:

    эт у Вас модуль не подулючен imagick
    если у Вас убунта то делаем так
    sudo aptitude install make php5-dev php-pear
    sudo aptitude remove php5-imagick
    sudo aptitude install libmagick9-dev
    sudo pecl install imagick
    sudo /etc/init.d/apache2 restart
    в php.ini добавляем imagick.so должно помочь

    у меня линукс Suse уставнол все модули апача + пхп — все отлично
    у хостера gentoo тоже код работает..

  5. Артем:

    ниче не понимаю)))) я начинающий=) у меня ток денвер запущен)

  6. Артем:

    и на винде пишу)

  7. Артем:

    как его под виндой запустить?

  8. admin:

    предположу что Вам надо эту длл подрубить в php.ini http://www.sk89q.com/2010/03/vc6-windows-binaries-for-imagick-2-3-0/
    а вообще откройте для себя линукс, сам долго сопротивлялся, зато сейчас сайтик раскатываю на локале, тесирую, заливаю на боевой и никаких проблем, все работает как часы :)

  9. Артем:

    спасибо

Добавить комментарий

Вы должны войти на сайт чтобы оставить комментарий.