Advertisement
yellux

Patch captcha

Aug 23rd, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.56 KB | None | 0 0
  1. <?php
  2.  
  3.   /******************************************************************
  4.  
  5.    Projectname:   CAPTCHA class
  6.    Version:       2.0
  7.    Author:        Pascal Rehfeldt <Pascal@Pascal-Rehfeldt.com>
  8.    Last modified: 15. January 2006
  9.  
  10.    * GNU General Public License (Version 2, June 1991)
  11.    *
  12.    * This program is free software; you can redistribute
  13.    * it and/or modify it under the terms of the GNU
  14.    * General Public License as published by the Free
  15.    * Software Foundation; either version 2 of the License,
  16.    * or (at your option) any later version.
  17.    *
  18.    * This program is distributed in the hope that it will
  19.    * be useful, but WITHOUT ANY WARRANTY; without even the
  20.    * implied warranty of MERCHANTABILITY or FITNESS FOR A
  21.    * PARTICULAR PURPOSE. See the GNU General Public License
  22.    * for more details.
  23.  
  24.    Description:
  25.    This class can generate CAPTCHAs, see README for more details!
  26.  
  27.   ******************************************************************/
  28.  
  29. class captcha {
  30.  
  31.     private $_fontPath = 'Includes/font/';
  32.  
  33.     private $_fonts = array();
  34.  
  35.     private $_captchaString;
  36.  
  37.     public function create($captchaString) {
  38.         if (function_exists('imagettftext') == false)
  39.             trigger_error(__METHOD__ .'() : GD library don\'t exist !', E_USER_ERROR);
  40.  
  41.         $this->_fonts = $this->_getFonts();
  42.  
  43.         $this->_captchaString = $captchaString;
  44.  
  45.         $this->_makeCaptcha();
  46.     }
  47.  
  48.     private function _getFonts() {
  49.         if (! is_dir($this->_fontPath))
  50.             trigger_error(__METHOD__ .'() : Font path don\'t exist ! ('. $this->_fontPath .')', E_USER_ERROR);
  51.  
  52.         $fonts = array();
  53.  
  54.         foreach (array_diff(scandir($this->_fontPath), array('.', '..')) as $file) {
  55.             if (strtolower(pathinfo($this->_fontPath . $file, PATHINFO_EXTENSION )) == 'ttf')
  56.                 $fonts[] = $file;
  57.         }
  58.  
  59.         if (count($fonts) == 0)
  60.             trigger_error(__METHOD__ .'() : No font available for generate Captcha !', E_USER_ERROR);
  61.  
  62.         return $fonts;
  63.     }
  64.  
  65.     public static function stringGen() {
  66.         $charPool   = array_merge(range('A', 'Z'), range(0, 9));
  67.         $poolLength = count($charPool) - 1;
  68.         $str        = '';
  69.  
  70.         for ($i = 0; $i < CAPTCHA_LENGTH; $i++)
  71.             $str .= $charPool[mt_rand(0, $poolLength)];
  72.  
  73.         return $str;
  74.     }
  75.  
  76.     private function _getRandFont() {
  77.         return $this->_fontPath . $this->_fonts[mt_rand(0, count($this->_fonts) - 1 )];
  78.     }
  79.  
  80.     private function _makeCaptcha() {
  81.         //header('Content-type: image/png');
  82.  
  83.         $imageLength = CAPTCHA_LENGTH * 25 + 16;
  84.         $imageHeight = 50;
  85.  
  86.         $image       = imagecreate($imageLength, $imageHeight);
  87.         $bgColor     = imagecolorallocate($image, 255, 255, 255);
  88.         $stringColor = imagecolorallocate($image, 0, 0, 0);
  89.  
  90.         $filter = new captchaFilters($imageLength, $imageHeight);
  91.  
  92.         $filter->signs($image, $this->_getRandFont());
  93.  
  94.         $captchaStringLength = strlen((string) $this->_captchaString);
  95.  
  96.         for ($i = 0; $i < $captchaStringLength; $i++) {
  97.             imagettftext(
  98.                 $image,
  99.                 20,
  100.                 mt_rand(-15, 15),
  101.                 $i * 25 + 10,
  102.                 mt_rand(25, 45),
  103.                 $stringColor,
  104.                 $this->_getRandFont(),
  105.                 $this->_captchaString{$i}
  106.             );
  107.         }
  108.  
  109.         $filter->noise($image, CAPTCHA_NOISE);
  110.         $filter->blur($image, CAPTCHA_BLUR);
  111.  
  112.         imagepng($image);
  113.         imagedestroy($image);
  114.     }
  115.  
  116. }
  117.  
  118. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement