sfCaptchaGD.class.php 4.65 KB
Newer Older
Яков's avatar
first  
Яков committed
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
<?php
/**
 * sfCaptchaGD class.
 *
 * @package    sfCaptchaGD
 * @subpackage sfCaptchaGD
 * @author     Alex Kubyshkin <glint@techinfo.net.ru>
 * @version    1.0.3
 */
class sfCaptchaGD 
{
 
  public  $securityCode;
  private $border_color;
  private $background_color;
  private $fonts;
  private $fonts_dir;
  private $fontSize;
  private $fontColor;
  private $chars;
  private $codeLength;
  private $image_width;
  private $image_height;

  function __construct() {
      $this->border_color = sfConfig::get('app_sf_captchagd_border_color', "000000");
      $this->background_color = sfConfig::get('app_sf_captchagd_background_color', "DDDDDD");
      $this->fonts = sfConfig::get('app_sf_captchagd_fonts', array("akbar/akbar.ttf", "brushcut/BRUSHCUT.TTF", "molten/molten.ttf", "planet_benson/Planetbe.ttf", "whoobub/WHOOBUB_.TTF"));
      $this->fonts_dir = sfConfig::get('app_sf_captchagd_fonts_dir', sfConfig::get('sf_plugins_dir').'/sfCaptchaGDPlugin/data/fonts/');
      $this->fontSize = sfConfig::get('app_sf_captchagd_font_size', "16");
      $this->fontColor = sfConfig::get('app_sf_captchagd_font_color', array("252525", "8b8787", "550707", "3526E6", "88531E"));
      $this->chars = sfConfig::get('app_sf_captchagd_chars', "123456789");
      $this->codeLength = sfConfig::get('app_sf_captchagd_length', 4);
      $this->image_width = sfConfig::get('app_sf_captchagd_image_width', 100);
      $this->image_height = sfConfig::get('app_sf_captchagd_image_height', 30);
  }
    
  
  public function simpleRandString($length, $list) {
    /*
     * Generate random string
     * 
    */
    mt_srand((double)microtime()*1000000);
 
    $newstring = "";
 
    if ($length > 0) {
        while (strlen($newstring) < $length) {
            $newstring .= $list[mt_rand(0, strlen($list)-1)];
        }
    }
    return $newstring;
  }
 
  private function allocateColor($img, $color = ""){
    return imagecolorallocate($img,
                hexdec(substr($color, 0, 2)),
                hexdec(substr($color, 2, 2)), 
                hexdec(substr($color, 4, 2))
                );
  }
  
  public function generateImage($securityCode = NULL)
  {
    $context = sfContext::getInstance();
    $l = $context->getLogger();
    if ($context->getRequest()->getGetParameter('reload') || ! $securityCode || sfConfig::get('app_sf_captchagd_force_new_captcha', false)){
      $this->securityCode = $this->simpleRandString($this->codeLength, $this->chars);
    } else {
      $this->securityCode = $securityCode;
    }
    $context->getUser()->setAttribute('captcha', $this->securityCode);
 
    $this->img = imagecreatetruecolor($this->image_width, $this->image_height);
    $bc_color = $this->allocateColor($this->img, $this->background_color);
    $border_color = $this->allocateColor($this->img, $this->border_color);
    imagefill($this->img, 0, 0, $bc_color);
    imagerectangle($this->img, 0, 0, $this->image_width - 1, $this->image_height - 1, $border_color);
 
    foreach($this->fontColor as $fcolor)
    {
        $color[] = $this->allocateColor($this->img, $fcolor);
    }
 
    $fw = imagefontwidth($this->fontSize) + $this->image_width / 30;
    $fh = imagefontheight($this->fontSize);
 
    // create a new string with a blank space between each letter so it looks better
    $newstr = "";
    for ($i = 0; $i < strlen($this->securityCode); $i++) {
        $newstr .= $this->securityCode[$i] ." ";
    }
 
    // remove the trailing blank
    $newstr = trim($newstr);
 
    // center the string 
    $x = ($this->image_width * 0.95 - strlen($newstr) * $fw ) / 2;
 
    // create random lines over text
    $stripe_size_max = $this->image_height / 3;
   // for($i = 0; $i < 15; $i++){
    //    $x2 = rand(0, $this->image_width);
   //     $y2 = rand(0, $this->image_height);
    //    imageline($this->img, $x2, $y2, $x2 + rand(-$stripe_size_max, $stripe_size_max), $y2 + rand(-$stripe_size_max, $stripe_size_max), $color[rand(0, count($color) - 1)]);
    //}
    
    // output each character at a random height and standard horizontal spacing
    for ($i = 0; $i < strlen($newstr); $i++) {
        $hz = $fh + ($this->image_height - $fh) / 2 + mt_rand(-$this->image_height / 10, $this->image_height / 10);
 
        // randomize rotation
        $rotate = rand(-25, 25);
 
        // randomize font size
        $newFontSize = $this->fontSize + $this->fontSize * (rand(0, 3) / 10);
 
        $a = imagettftext($this->img, $newFontSize, $rotate, $x + ($fw * $i), $hz, $color[rand(0, count($color) - 1)], $this->fonts_dir.$this->fonts[rand(0, count($this->fonts) - 1)], $newstr[$i]);
    }
    $context->getResponse()->setContentType('image/png');
    imagepng($this->img);
    imagedestroy($this->img);
    $context->getResponse()->send();
  }
}
?>