残翅 发表于 2010-4-20 11:20:48

本帖最后由 残翅 于 2010-4-20 12:53 编辑

好东西,修改了下
类:
<?phpif ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* SimpleCaptcha class
*
*/
class Simple_captcha {
    /** Width of the image */
    public $width= 200;
    /** Height of the image */
    public $height = 70;
    /** Dictionary word file (empty for randnom text) */
    public $wordsFile = 'words/en.php';
    /**
   * Path for resource files (fonts, words, etc.)
   *
   * "resources" by default. For security reasons, is better move this
   * directory to another location outise the web server
   *
   */
    public $resourcesPath = '';
    /** Min word length (for non-dictionary random text generation) */
    public $minWordLength = 5;
    /**
   * Max word length (for non-dictionary random text generation)
   *
   * Used for dictionary words indicating the word-length
   * for font-size modification purposes
   */
    public $maxWordLength = 8;
    /** Sessionname to store the original text */
    public $session_var = 'captcha';
    /** Background color in RGB-array */
    public $backgroundColor = array(255, 255, 255);
    /** Foreground colors in RGB-array */
    public $colors = array(
      array(27,78,181), // blue
      array(22,163,35), // green
      array(214,36,7),// red
    );
    /** Shadow color in RGB-array or null */
    public $shadowColor = null; //array(0, 0, 0);
    /**
   * Font configuration
   *
   * - font: TTF file
   * - spacing: relative pixel space between character
   * - minSize: min font size
   * - maxSize: max font size
   */
    public $fonts = array(
      'Antykwa'=> array('spacing' => -3, 'minSize' => 27, 'maxSize' => 30, 'font' => 'AntykwaBold.ttf'),
      'Candice'=> array('spacing' =>-1.5,'minSize' => 28, 'maxSize' => 31, 'font' => 'Candice.ttf'),
      'DingDong' => array('spacing' => -2, 'minSize' => 24, 'maxSize' => 30, 'font' => 'Ding-DongDaddyO.ttf'),
      'Duality'=> array('spacing' => -2, 'minSize' => 30, 'maxSize' => 38, 'font' => 'Duality.ttf'),
      'Heineken' => array('spacing' => -2, 'minSize' => 24, 'maxSize' => 34, 'font' => 'Heineken.ttf'),
      'Jura'   => array('spacing' => -2, 'minSize' => 28, 'maxSize' => 32, 'font' => 'Jura.ttf'),
      'StayPuft' => array('spacing' =>-1.5,'minSize' => 28, 'maxSize' => 32, 'font' => 'StayPuft.ttf'),
      'Times'    => array('spacing' => -2, 'minSize' => 28, 'maxSize' => 34, 'font' => 'TimesNewRomanBold.ttf'),
      'VeraSans' => array('spacing' => -1, 'minSize' => 20, 'maxSize' => 28, 'font' => 'VeraSansBold.ttf'),
    );
    /** Wave configuracion in X and Y axes */
    public $Yperiod    = 12;
    public $Yamplitude = 14;
    public $Xperiod    = 11;
    public $Xamplitude = 5;
    /** letter rotation clockwise */
    public $maxRotation = 8;
    /**
   * Internal image size factor (for better image quality)
   * 1: low, 2: medium, 3: high
   */
    public $scale = 2;
    /**
   * Blur effect for better image quality (but slower image processing).
   * Better image results with scale=3
   */
    public $blur = false;
    /** Debug? */
    public $debug = false;
   
    /** Image format: jpeg or png */
    public $imageFormat = 'jpeg';

    /** GD image */
    public $im;
   
    public $text = 'tongyn';





    public function __construct($config = array()) {
   $this -> initialize($config);
    }

public function initialize($config = array()){
foreach ($config as $key => $val){
   if (isset($this->$key)){
    $this->$key = $val;
   }
}
}
function just_test(){
   if (empty($fontcfg)) {
            // Select the font configuration
            $fontcfg= $this->fonts;
      }
      // Full path of font file
      $fontfile = $this->resourcesPath.'/fonts/'.$fontcfg['font'];
      echo $fontfile;
}

    public function CreateImage() {
      $ini = microtime(true);
      /** Initialization */
      $this->ImageAllocate();
      
      /** Text insertion */
      //$text = $this->GetCaptchaText();
      $text = $this->text;
      //$text = 'tongyn';
      $fontcfg= $this->fonts;
      $this->WriteText($text, $fontcfg);

      //$_SESSION[$this->session_var] = $text;
      /** Transformations */
      $this->WaveImage();
      if ($this->blur && function_exists('imagefilter')) {
            imagefilter($this->im, IMG_FILTER_GAUSSIAN_BLUR);
      }
      $this->ReduceImage();

      if ($this->debug) {
            imagestring($this->im, 1, 1, $this->height-8,
                "$text {$fontcfg['font']} ".round((microtime(true)-$ini)*1000)."ms",
                $this->GdFgColor
            );
      }

      /** Output */
      $this->WriteImage();
      $this->Cleanup();
    }




    /**
   * Creates the image resources
   */
    protected function ImageAllocate() {
      // Cleanup
      if (!empty($this->im)) {
            imagedestroy($this->im);
      }
      $this->im = imagecreatetruecolor($this->width*$this->scale, $this->height*$this->scale);
      // Background color
      $this->GdBgColor = imagecolorallocate($this->im,
            $this->backgroundColor,
            $this->backgroundColor,
            $this->backgroundColor
      );
      imagefilledrectangle($this->im, 0, 0, $this->width*$this->scale, $this->height*$this->scale, $this->GdBgColor);
      // Foreground color 0-2
      $color         = $this->colors;
      $this->GdFgColor = imagecolorallocate($this->im, $color, $color, $color);
      // Shadow color
      if (!empty($this->shadowColor) && is_array($this->shadowColor) && sizeof($this->shadowColor) >= 3) {
            $this->GdShadowColor = imagecolorallocate($this->im,
                $this->shadowColor,
                $this->shadowColor,
                $this->shadowColor
            );
      }
    }


    /**
   * Text generation
   *
   * @return string Text
   */
    protected function GetCaptchaText() {
      $text = $this->GetDictionaryCaptchaText();
      if (!$text) {
            $text = $this->GetRandomCaptchaText();
      }
      return $text;
    }



    /**
   * Random text generation
   *
   * @return string Text
   */
    protected function GetRandomCaptchaText($length = null) {
      if (empty($length)) {
            $length = rand($this->minWordLength, $this->maxWordLength);
      }
      $words= "abcdefghijlmnopqrstvwyz";
      $vocals = "aeiou";
      $text= "";
      $vocal = rand(0, 1);
      for ($i=0; $i<$length; $i++) {
            if ($vocal) {
                $text .= substr($vocals, mt_rand(0, 4), 1);
            } else {
                $text .= substr($words, mt_rand(0, 22), 1);
            }
            $vocal = !$vocal;
      }
      return $text;
    }




    /**
   * Random dictionary word generation
   *
   * @param boolean $extended Add extended "fake" words
   * @return string Word
   */
    function GetDictionaryCaptchaText($extended = false) {
      if (empty($this->wordsFile)) {
            return false;
      }
      // Full path of words file
      if (substr($this->wordsFile, 0, 1) == '/') {
            $wordsfile = $this->wordsFile;
      } else {
            $wordsfile = $this->resourcesPath.'/'.$this->wordsFile;
      }
      $fp   = fopen($wordsfile, "r");
      $length = strlen(fgets($fp));
      if (!$length) {
            return false;
      }
      $line   = rand(1, (filesize($wordsfile)/$length)-2);
      if (fseek($fp, $length*$line) == -1) {
            return false;
      }
      $text = trim(fgets($fp));
      fclose($fp);

      /** Change ramdom volcals */
      if ($extended) {
            $text   = preg_split('//', $text, -1, PREG_SPLIT_NO_EMPTY);
            $vocals = array('a', 'e', 'i', 'o', 'u');
            foreach ($text as $i => $char) {
                if (mt_rand(0, 1) && in_array($char, $vocals)) {
                  $text[$i] = $vocals;
                }
            }
            $text = implode('', $text);
      }
      return $text;
    }





    /**
   * Text insertion
   */
    protected function WriteText($text, $fontcfg = array()) {
      if (empty($fontcfg)) {
            // Select the font configuration
            $fontcfg= $this->fonts;
      }
      // Full path of font file
      $fontfile = $this->resourcesPath.DIRECTORY_SEPARATOR.'fonts'.DIRECTORY_SEPARATOR.$fontcfg['font'];

      /** Increase font-size for shortest words: 9% for each glyp missing */
      $lettersMissing = $this->maxWordLength-strlen($text);
      $fontSizefactor = 1+($lettersMissing*0.09);
      // Text generation (char by char)
      $x      = 20*$this->scale;
      $y      = round(($this->height*27/40)*$this->scale);
      $length = strlen($text);
      for ($i=0; $i<$length; $i++) {
            $degree   = rand($this->maxRotation*-1, $this->maxRotation);
            $fontsize = rand($fontcfg['minSize'], $fontcfg['maxSize'])*$this->scale*$fontSizefactor;
            $letter   = substr($text, $i, 1);
            if ($this->shadowColor) {
                $coords = imagettftext($this->im, $fontsize, $degree,
                  $x+$this->scale, $y+$this->scale,
                  $this->GdShadowColor, $fontfile, $letter);
            }
            $coords = imagettftext($this->im, $fontsize, $degree,
                $x, $y,
                $this->GdFgColor, $fontfile, $letter);
            $x += ($coords-$x) + ($fontcfg['spacing']*$this->scale);
      }
    }

    /**
   * Wave filter
   */
    protected function WaveImage() {
      // X-axis wave generation
      $xp = $this->scale*$this->Xperiod*rand(1,3);
      $k = rand(0, 100);
      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);
      }
      // Y-axis wave generation
      $k = rand(0, 100);
      $yp = $this->scale*$this->Yperiod*rand(1,2);
      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);
      }
    }


    /**
   * Reduce the image to the final size
   */
    protected function ReduceImage() {
      // Reduzco el tama駉 de la imagen
      $imResampled = imagecreatetruecolor($this->width, $this->height);
      imagecopyresampled($imResampled, $this->im,
            0, 0, 0, 0,
            $this->width, $this->height,
            $this->width*$this->scale, $this->height*$this->scale
      );
      imagedestroy($this->im);
      $this->im = $imResampled;
    }




    /**
   * File generation
   */
    protected function WriteImage() {
      if ($this->imageFormat == 'png' && function_exists('imagepng')) {
            header("Content-type: image/png");
            imagepng($this->im);
      } else {
            header("Content-type: image/jpeg");
            imagejpeg($this->im, null, 80);
      }
    }



    /**
   * Cleanup
   */
    protected function Cleanup() {
      imagedestroy($this->im);
    }
}


// END CI_Config class
/* End of file SimpleCaptcha.php */

残翅 发表于 2010-4-20 11:22:08

本帖最后由 残翅 于 2010-4-20 12:55 编辑

控制器
function captcha(){
$this->load->helper('string');
$this->load->library('session');
$word = random_string('alnum',4);
$base_url = $this->config->item('base_url');
$this->session->unset_userdata('captcha_word');
$this->session->set_userdata('captcha_word',$word);
$font_dir = dirname(__FILE__);
$font_dir = str_replace(DIRECTORY_SEPARATOR.'system'.DIRECTORY_SEPARATOR.'application'.DIRECTORY_SEPARATOR.'controllers','',$font_dir);
$this->load->library('simple_captcha',array('resourcesPath' => $font_dir,'text' => $word,'width' => 130,'backgroundColor' => array(237,247,255),'height' => 50,'fonts' => array( 'Times' => array('spacing' => -2, 'minSize' => 14, 'maxSize' => 20, 'font' => 'TimesNewRomanBold.ttf'))));
$this -> simple_captcha -> CreateImage();
}

残翅 发表于 2010-4-20 11:23:05

本帖最后由 残翅 于 2010-4-20 12:53 编辑

视图:
<img src="home/captcha" id="captcha" style="cursor:pointer;float:left;"onClick="this.src='home/captcha/'+Math.random()"/>

残翅 发表于 2010-4-20 12:50:07

字体目录“fonts”放在根目录下

wyh0416 发表于 2010-5-3 12:50:51

请问:
4、在view中调用
      <img src="<?php echo xxxx/getcode" id="captcha" title="点击图片更新" /><br/>
这个XXXX是什么啊?

残翅 发表于 2010-5-7 11:08:47

回复 15# wyh0416


    xxxx是控制器

dongfang521 发表于 2010-5-28 18:57:10

感谢分享,呵呵

longjianghu 发表于 2010-6-7 22:59:24

不错正需要

longjianghu 发表于 2010-6-10 22:11:38

为什么图片显示不出来呢?

cisso 发表于 2010-6-22 17:25:11

不知道有没有大侠回复一下,为什么在最后createImage的时候并没有生成图片,
可是GD库我是打开了的啊:dizzy:
页: 1 [2] 3 4 5
查看完整版本: 推荐一个验证码生成的代码:cool-php-captcha