Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /******************************************************************
- Projectname: CAPTCHA class
- Version: 2.0
- Author: Pascal Rehfeldt <Pascal@Pascal-Rehfeldt.com>
- Last modified: 15. January 2006
- * GNU General Public License (Version 2, June 1991)
- *
- * This program is free software; you can redistribute
- * it and/or modify it under the terms of the GNU
- * General Public License as published by the Free
- * Software Foundation; either version 2 of the License,
- * or (at your option) any later version.
- *
- * This program is distributed in the hope that it will
- * be useful, but WITHOUT ANY WARRANTY; without even the
- * implied warranty of MERCHANTABILITY or FITNESS FOR A
- * PARTICULAR PURPOSE. See the GNU General Public License
- * for more details.
- Description:
- This class can generate CAPTCHAs, see README for more details!
- ******************************************************************/
- class captcha {
- private $_fontPath = 'Includes/font/';
- private $_fonts = array();
- private $_captchaString;
- public function create($captchaString) {
- if (function_exists('imagettftext') == false)
- trigger_error(__METHOD__ .'() : GD library don\'t exist !', E_USER_ERROR);
- $this->_fonts = $this->_getFonts();
- $this->_captchaString = $captchaString;
- $this->_makeCaptcha();
- }
- private function _getFonts() {
- if (! is_dir($this->_fontPath))
- trigger_error(__METHOD__ .'() : Font path don\'t exist ! ('. $this->_fontPath .')', E_USER_ERROR);
- $fonts = array();
- foreach (array_diff(scandir($this->_fontPath), array('.', '..')) as $file) {
- if (strtolower(pathinfo($this->_fontPath . $file, PATHINFO_EXTENSION )) == 'ttf')
- $fonts[] = $file;
- }
- if (count($fonts) == 0)
- trigger_error(__METHOD__ .'() : No font available for generate Captcha !', E_USER_ERROR);
- return $fonts;
- }
- public static function stringGen() {
- $charPool = array_merge(range('A', 'Z'), range(0, 9));
- $poolLength = count($charPool) - 1;
- $str = '';
- for ($i = 0; $i < CAPTCHA_LENGTH; $i++)
- $str .= $charPool[mt_rand(0, $poolLength)];
- return $str;
- }
- private function _getRandFont() {
- return $this->_fontPath . $this->_fonts[mt_rand(0, count($this->_fonts) - 1 )];
- }
- private function _makeCaptcha() {
- //header('Content-type: image/png');
- $imageLength = CAPTCHA_LENGTH * 25 + 16;
- $imageHeight = 50;
- $image = imagecreate($imageLength, $imageHeight);
- $bgColor = imagecolorallocate($image, 255, 255, 255);
- $stringColor = imagecolorallocate($image, 0, 0, 0);
- $filter = new captchaFilters($imageLength, $imageHeight);
- $filter->signs($image, $this->_getRandFont());
- $captchaStringLength = strlen((string) $this->_captchaString);
- for ($i = 0; $i < $captchaStringLength; $i++) {
- imagettftext(
- $image,
- 20,
- mt_rand(-15, 15),
- $i * 25 + 10,
- mt_rand(25, 45),
- $stringColor,
- $this->_getRandFont(),
- $this->_captchaString{$i}
- );
- }
- $filter->noise($image, CAPTCHA_NOISE);
- $filter->blur($image, CAPTCHA_BLUR);
- imagepng($image);
- imagedestroy($image);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement