[ Index ]

PHP Cross Reference of phpBB 3.0 Beta 3

title

Body

[close]

/includes/captcha/ -> captcha_gd.php (source)

   1  <?php
   2  /** 
   3  *
   4  * @package VC
   5  * @version $Id: captcha_gd.php,v 1.14 2006/11/03 23:09:15 davidmj Exp $
   6  * @copyright (c) 2006 phpBB Group 
   7  * @license http://opensource.org/licenses/gpl-license.php GNU Public License 
   8  *
   9  */
  10  
  11  /**
  12  * Based on PHP-Class hn_captcha Version 1.3, released 11-Apr-2006
  13  * Original Author - Horst Nogajski, horst@nogajski.de
  14  *
  15  * @package VC
  16  */
  17  class captcha
  18  {
  19  	function execute($code)
  20      {
  21          global $config;
  22          $stats = gd_info();
  23  
  24          if (substr($stats['GD Version'], 0, 7) === 'bundled')
  25          {
  26              $bundled = true;
  27          }
  28          else
  29          {
  30              $bundled = false;
  31          }
  32  
  33          preg_match('/[\\d.]+/', $stats['GD Version'], $version);
  34          if (version_compare($version[0], '2.0.1', '>='))
  35          {
  36              $gd_version = 2;
  37          }
  38          else
  39          {
  40              $gd_version = 1;
  41          }
  42  
  43          // set dimension of image
  44          $lx = 360;
  45          $ly = 96;
  46  
  47          // create the image, stay compat with older versions of GD
  48          if ($gd_version === 2)
  49          {
  50              $func1 = 'imagecreatetruecolor';
  51              $func2 = 'imagecolorallocate';
  52          }
  53          else
  54          {
  55              $func1 = 'imagecreate';
  56              $func2 = 'imagecolorclosest';
  57          }
  58  
  59          $image = $func1($lx, $ly);
  60  
  61          if ($bundled)
  62          {
  63              imageantialias($image, true);
  64          }
  65  
  66          // set background color
  67          $back =  imagecolorallocate($image, mt_rand(224, 255), mt_rand(224, 255), mt_rand(224, 255));
  68          imagefilledrectangle($image, 0, 0, $lx, $ly, $back);
  69  
  70          // allocates the 216 websafe color palette to the image
  71          if ($gd_version === 1)
  72          {
  73              for ($r = 0; $r <= 255; $r += 51)
  74              {
  75                  for ($g = 0; $g <= 255; $g += 51)
  76                  {
  77                      for ($b = 0; $b <= 255; $b += 51)
  78                      {
  79                          imagecolorallocate($image, $r, $g, $b);
  80                      }
  81                  }
  82              }
  83          }
  84  
  85  
  86          // fill with noise or grid
  87          if ($config['captcha_gd_noise'])
  88          {
  89              // random characters in background with random position, angle, color
  90              for ($i = 0 ; $i < 72; $i++)
  91              {
  92                  $size    = mt_rand(8, 23);
  93                  $angle    = mt_rand(0, 360);
  94                  $x        = mt_rand(0, 360);
  95                  $y        = mt_rand(0, (int)($ly - ($size / 5)));
  96                  $color    = $func2($image, mt_rand(160, 224), mt_rand(160, 224), mt_rand(160, 224));
  97                  $text    = chr(mt_rand(45, 250));
  98                  imagettftext($image, $size, $angle, $x, $y, $color, $this->get_font(), $text);
  99              }
 100          }
 101          else
 102          {
 103              // generate grid
 104              for ($i = 0; $i < $lx; $i += 13)
 105              {
 106                  $color    = $func2($image, mt_rand(160, 224), mt_rand(160, 224), mt_rand(160, 224));
 107                  imageline($image, $i, 0, $i, $ly, $color);
 108              }
 109              for ($i = 0; $i < $ly; $i += 11)
 110              {
 111                  $color    = $func2($image, mt_rand(160, 224), mt_rand(160, 224), mt_rand(160, 224));
 112                  imageline($image, 0, $i, $lx, $i, $color);
 113              }
 114          }
 115  
 116          $len = strlen($code);
 117  
 118          for ($i = 0, $x = mt_rand(20, 40); $i < $len; $i++)
 119          {
 120              $text    = strtoupper($code[$i]);
 121              $angle    = mt_rand(-30, 30);
 122              $size    = mt_rand(20, 40);
 123              $y        = mt_rand((int)($size * 1.5), (int)($ly - ($size / 7)));
 124  
 125              $color    = $func2($image, mt_rand(0, 127), mt_rand(0, 127), mt_rand(0, 127));
 126              $shadow = $func2($image, mt_rand(127, 254), mt_rand(127, 254), mt_rand(127, 254));
 127              $font = $this->get_font();
 128  
 129              imagettftext($image, $size, $angle, $x + (int)($size / 15), $y, $shadow, $font, $text);
 130              imagettftext($image, $size, $angle, $x, $y - (int)($size / 15), $color, $font, $text);
 131  
 132              $x += $size + 4;
 133          }
 134  
 135          // Output image
 136          header('Content-Type: image/png');
 137          header('Cache-control: no-cache, no-store');
 138          imagepng($image);
 139          imagedestroy($image);
 140      }
 141  
 142  	function get_font()
 143      {
 144          static $fonts = array();
 145      
 146          if (!sizeof($fonts))
 147          {
 148              global $phpbb_root_path;
 149      
 150              $dr = opendir($phpbb_root_path . 'includes/captcha/fonts');
 151              while (false !== ($entry = readdir($dr)))
 152              {
 153                  if (strtolower(pathinfo($entry, PATHINFO_EXTENSION)) == 'ttf')
 154                  {
 155                      $fonts[] = $phpbb_root_path . 'includes/captcha/fonts/' . $entry;
 156                  }
 157              }
 158              closedir($dr);
 159          }
 160  
 161          return $fonts[array_rand($fonts)];
 162      }
 163  }
 164  
 165  ?>


Generated: Wed Nov 22 00:35:05 2006 Cross-referenced by PHPXref 0.6