[ Index ]

MailPress 544

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/class/options/form/field_types/captcha_gd2/captcha/ -> captcha.php (source)

   1  <?php
   2  if (!extension_loaded('gd')) return;
   3  
   4  /**
   5   * From an original idea of :
   6   *
   7   * @author  Jose Rodriguez <jose.rodriguez@exec.cl>
   8   * @license GPLv3
   9   * @link    http://code.google.com/p/cool-php-captcha
  10   * @package captcha
  11   * @version 0.3
  12   *
  13   */
  14  
  15  class MP_Form_field_type_captcha_gd2_googlelike
  16  {
  17      public $width  = 200;                                                        /** Width of the image */
  18      public $height = 70;                                                        /** Height of the image */
  19      public $minWordLength = 5;                                                    /** Min word length (for non-dictionary random text generation) */
  20      /**
  21       * Max word length (for non-dictionary random text generation)
  22       * 
  23       * Used for dictionary words indicating the word-length
  24       * for font-size modification purposes
  25       */
  26      public $maxWordLength = 8;
  27      public $session_var = 'mp_googlelike';                                            /** Sessionname to store the original text */
  28      public $backgroundColor = array(255, 255, 255);                                        /** Background color in RGB-array */
  29      public $colors = array(array(27,78,181)/*blue*/, array(22,163,35)/*green*/, array(214,36,7) /*red*/); /** Foreground colors in RGB-array */
  30      public $shadowColor = null; //array(0, 0, 0);                                        /** Shadow color in RGB-array or null */
  31  
  32      public $fonts = array(
  33          'Antykwa'  => array('spacing' => -3, 'minSize' => 27, 'maxSize' => 30, 'font' => 'AntykwaBold.ttf'),                //
  34          'Candice'  => array('spacing' =>-1.5,'minSize' => 28, 'maxSize' => 31, 'font' => 'Candice.ttf'),                // Font configuration
  35          'DingDong' => array('spacing' => -2, 'minSize' => 24, 'maxSize' => 30, 'font' => 'Ding-DongDaddyO.ttf'),             // - font: TTF file
  36          'Duality'  => array('spacing' => -2, 'minSize' => 30, 'maxSize' => 38, 'font' => 'Duality.ttf'),                // - spacing: relative pixel space between character
  37          'Heineken' => array('spacing' => -2, 'minSize' => 24, 'maxSize' => 34, 'font' => 'Heineken.ttf'),                // - minSize: min font size
  38          'Jura'     => array('spacing' => -2, 'minSize' => 28, 'maxSize' => 32, 'font' => 'Jura.ttf'),                    // - maxSize: max font size
  39          'StayPuft' => array('spacing' =>-1.5,'minSize' => 28, 'maxSize' => 32, 'font' => 'StayPuft.ttf'),
  40          'VeraSans' => array('spacing' => -1, 'minSize' => 20, 'maxSize' => 28, 'font' => 'VeraSansBold.ttf'),
  41          'VeraSeBd'    => array('spacing' => -2, 'minSize' => 24, 'maxSize' => 30, 'font' => 'VeraSeBd.ttf'),
  42      );
  43  
  44      public $Yperiod    = 12;                                                        /** Wave configuracion in X and Y axes */
  45      public $Yamplitude = 14;
  46      public $Xperiod    = 11;
  47      public $Xamplitude = 5;
  48  
  49      public $maxRotation = 8;                                                    /** letter rotation clockwise */
  50  
  51      /**
  52       * Internal image size factor (for better image quality)
  53       * 1: low, 2: medium, 3: high
  54       */
  55      public $scale = 2;
  56  
  57      /** 
  58       * Blur effect for better image quality (but slower image processing).
  59       * Better image results with scale=3
  60       */
  61      public $blur = false;
  62  
  63      /** Debug? */
  64      public $debug = false;
  65      
  66      /** Image format: jpeg or png */
  67      public $imageFormat = 'png';
  68  
  69  
  70      /** GD image */
  71      public $im;
  72  
  73  	function __construct()
  74      {
  75          session_start();
  76  
  77          $root = dirname(__FILE__) . '/words/';
  78          if (is_dir($root)) 
  79          {
  80              $dir = @opendir($root);
  81              if ($dir) while (($file = readdir($dir)) !== false)  if (($file{0} != '.') && (substr($file, -4) == '.php'))  $xlangs[] = strtolower(substr($file, 0, -4));
  82              @closedir($dir);
  83          }
  84  
  85          $lang = $xlangs[array_rand($xlangs)];
  86          if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) 
  87          {
  88              $lang_http  = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
  89              if (in_array($lang_http, $xlangs)) $lang = $lang_http;
  90          }
  91          $this->wordsFile = $root . "$lang.php";
  92  
  93          $this->CreateImage();
  94  
  95          ob_end_clean();
  96          if ($this->imageFormat == 'png' && function_exists('imagepng')) 
  97          {
  98              header('Content-type: image/png');
  99              imagepng($this->im);
 100          } else {
 101              header('Content-type: image/jpeg');
 102              imagejpeg($this->im, null, 80);
 103          }
 104          imagedestroy ($this->im);
 105          die();
 106      }
 107  
 108  	public function CreateImage() {
 109          $ini = microtime(true);
 110  
 111          /** Initialization */
 112          $this->ImageAllocate();
 113          
 114          /** Text insertion */
 115          $text = $this->GetCaptchaText();
 116  
 117          $fontcfg  = $this->fonts[array_rand($this->fonts)];
 118          $this->WriteText($text, $fontcfg);
 119  
 120          $_SESSION[$this->session_var] = $text;
 121  
 122          /** Transformations */
 123          $this->WaveImage();
 124          if ($this->blur && function_exists('imagefilter')) imagefilter($this->im, IMG_FILTER_GAUSSIAN_BLUR);
 125          $this->ReduceImage();
 126  
 127          if ($this->debug) imagestring($this->im, 1, 1, $this->height-8, "$text {$fontcfg['font']} ".round((microtime(true)-$ini)*1000)."ms", $this->GdFgColor );
 128      }
 129  
 130      /**
 131      * Creates the image resources
 132      */
 133  	protected function ImageAllocate() 
 134      {
 135          // Cleanup
 136          if (!empty($this->im)) imagedestroy($this->im);
 137  
 138          $this->im = imagecreatetruecolor($this->width*$this->scale, $this->height*$this->scale);
 139  
 140          // Background color
 141          $this->GdBgColor = imagecolorallocate($this->im, $this->backgroundColor[0], $this->backgroundColor[1], $this->backgroundColor[2] );
 142          imagefilledrectangle($this->im, 0, 0, $this->width*$this->scale, $this->height*$this->scale, $this->GdBgColor);
 143  
 144          // Foreground color
 145          $color = $this->colors[mt_rand(0, sizeof($this->colors)-1)];
 146          $this->GdFgColor = imagecolorallocate($this->im, $color[0], $color[1], $color[2]);
 147  
 148          // Shadow color
 149          if (!empty($this->shadowColor) && is_array($this->shadowColor) && sizeof($this->shadowColor) >= 3) $this->GdShadowColor = imagecolorallocate($this->im, $this->shadowColor[0], $this->shadowColor[1], $this->shadowColor[2] );
 150      }
 151  
 152      /**
 153      * Text generation
 154      *
 155      * @return string Text
 156      */
 157  	protected function GetCaptchaText() 
 158      {
 159          $text = $this->GetDictionaryCaptchaText();
 160          if (!$text) $text = $this->GetRandomCaptchaText();
 161          return $text;
 162      }
 163  
 164      /**
 165      * Random dictionary word generation
 166      *
 167      * @param boolean $extended Add extended "fake" words
 168      * @return string Word
 169      */
 170  	function GetDictionaryCaptchaText($extended = false) 
 171      {
 172          if (empty($this->wordsFile)) return false;
 173  
 174          $fp     = fopen($this->wordsFile, "r");
 175          $length = strlen(fgets($fp));
 176          if (!$length) return false;
 177  
 178          $line   = rand(1, (filesize($this->wordsFile)/$length)-2);
 179          if (fseek($fp, $length*$line) == -1) return false;
 180  
 181          $text = trim(fgets($fp));
 182          fclose($fp);
 183  
 184          /** Change ramdom volcals */
 185          if ($extended) 
 186          {
 187              $text   = preg_split('//', $text, -1, PREG_SPLIT_NO_EMPTY);
 188              $vocals = array('a', 'e', 'i', 'o', 'u');
 189              foreach ($text as $i => $char) if (mt_rand(0, 1) && in_array($char, $vocals)) $text[$i] = $vocals[mt_rand(0, 4)];
 190              $text = implode('', $text);
 191          }
 192          return $text;
 193      }
 194  
 195      /**
 196      * Random text generation
 197      *
 198      * @return string Text
 199      */
 200  	protected function GetRandomCaptchaText($length = null) 
 201      {
 202          if (empty($length)) $length = rand($this->minWordLength, $this->maxWordLength);
 203          $words  = "abcdefghijlmnopqrstvwyz";
 204          $vocals = "aeiou";
 205          $text  = "";
 206          $vocal = rand(0, 1);
 207          for ($i = 0; $i < $length; $i++) 
 208          {
 209              $text .= ($vocal) ? substr($vocals, mt_rand(0, 4), 1) : substr($words, mt_rand(0, 22), 1);
 210              $vocal = !$vocal;
 211          }
 212          return $text;
 213      }
 214  
 215      /**
 216      * Text insertion
 217      */
 218  	protected function WriteText($text, $fontcfg = array()) 
 219      {
 220      // Select the font configuration
 221          if (empty($fontcfg)) $fontcfg  = $this->fonts[array_rand($this->fonts)];
 222  
 223          // Full path of font file
 224          $fontfile = dirname(__FILE__) . '/fonts/' . $fontcfg['font'];
 225          /**
 226           *  Increase font-size for shortest words: 9% for each glyp missing */
 227          $lettersMissing = $this->maxWordLength-strlen($text);
 228          $fontSizefactor = 1+($lettersMissing*0.09);
 229  
 230          // Text generation (char by char)
 231          $x      = 20*$this->scale;
 232          $y      = round(($this->height*27/40)*$this->scale);
 233          $length = strlen($text);
 234          for ($i = 0; $i < $length; $i++) 
 235          {
 236              $degree   = rand($this->maxRotation*-1, $this->maxRotation);
 237              $fontsize = rand($fontcfg['minSize'], $fontcfg['maxSize'])*$this->scale*$fontSizefactor;
 238              $letter   = substr($text, $i, 1);
 239  
 240              if ($this->shadowColor) $coords = imagettftext($this->im, $fontsize, $degree, $x+$this->scale, $y+$this->scale, $this->GdShadowColor, $fontfile, $letter);
 241              $coords = imagettftext($this->im, $fontsize, $degree, $x, $y, $this->GdFgColor, $fontfile, $letter);
 242              $x += ($coords[2]-$x) + ($fontcfg['spacing']*$this->scale);
 243          }
 244      }
 245  
 246      /**
 247      * Wave filter
 248      */
 249  	protected function WaveImage() 
 250      {
 251          // X-axis wave generation
 252          $xp = $this->scale*$this->Xperiod*rand(1,3);
 253          $k = rand(0, 100);
 254          for ($i = 0; $i < ($this->width*$this->scale); $i++)  imagecopy($this->im, $this->im, $i-1, sin($k+$i/$xp) * ($this->scale*$this->Xamplitude), $i, 0, 1, $this->height*$this->scale);
 255  
 256          // Y-axis wave generation
 257          $k = rand(0, 100);
 258          $yp = $this->scale*$this->Yperiod*rand(1,2);
 259          for ($i = 0; $i < ($this->height*$this->scale); $i++) imagecopy($this->im, $this->im, sin($k+$i/$yp) * ($this->scale*$this->Yamplitude), $i-1, 0, $i, $this->width*$this->scale, 1);
 260      }
 261  
 262      /**
 263      * Reduce the image to the final size
 264      */
 265  	protected function ReduceImage() 
 266      {
 267          $imResampled = imagecreatetruecolor($this->width, $this->height);
 268          imagecopyresampled($imResampled, $this->im, 0, 0, 0, 0, $this->width, $this->height, $this->width*$this->scale, $this->height*$this->scale );
 269          imagedestroy($this->im);
 270          $this->im = $imResampled;
 271      }
 272  }
 273  new MP_Form_field_type_captcha_gd2_googlelike();


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1