<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * zsb
 *
 * 验证码生成类
 *
 * @package         zsb
 * @author          EllisLab Dev Team
 * @copyright       Copyright (c) 2006 - 2014, HOUSE365, Inc.
 * @link            http://nj.sell.house.com
 * @since           Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * 验证码生成类
 *
 * 验证码生成类功能描述
 *
 * @package         zsb
 * @subpackage      Libraries
 * @category        Libraries
 * @author          fisher
 * @link
 */
class Validcode
{
    /**
    * 验证码
    *
    * @access private
    * @var string
    */
    private $authnum = '';


    /**
    * Constructor
    */
    public function __construct() 
    {
    
    }
    
    /**
    * set_authnum
    *
    * 设置验证码
    *
    * @access	public
    * @param	string $authnum 验证码
    * @return	void
    */
    private function _set_authnum($authnum)
    {
        $this->authnum = $authnum;
    }

    /**
    * get_authnum
    *
    * 获取验证码
    *
    * @access	public
    * @param	void
    * @return	string $authnum 验证码
    */
    public function get_authnum()
    {
        return $this->authnum;
    }

    /**
    * create_validcode
    *
    * 创建验证图片
    *
    * @access	public
    * @param	int $uid 用户编号
    * @return	void
    */
    public function create_validcode()
    {
        $fontSize = 28;													//定义字体大小
        $length = 4;													//定义字符串长度
        $strNum = $this->get_verify($length);							//获取一个随机字符串
        $this->_set_authnum($strNum);									//付值给session

        $width = 100;													//定义图片宽度
        $height = 40;													//定义图片高度
        $im = imagecreate($width,$height);									//生成一张指定宽高的图片
        $backgroundcolor = imagecolorallocate ($im, 255, 255, 255);				//生成背景色
        $frameColor = imageColorAllocate($im, 150, 150, 150);					//生成边框色
        $font = realpath("./esf_system/fonts/arial.ttf");						        //提取字体文件，开始写字
        for($i = 0; $i < $length; $i++) {
            $charY = $height-8 + rand(-1,1);								//定义字符Y坐标
            $charX = $i*26+2;											//定义字符X坐标
                                                                    //生成字符颜色
            $text_color = imagecolorallocate($im, mt_rand(50, 200), mt_rand(50, 128), mt_rand(50, 200));
            $angle = rand(-20,20);										//生成字符角度
                                                                    //写入字符
            imageTTFText($im, $fontSize, $angle, $charX,  $charY, $text_color, $font, $strNum[$i]);
        }
        for($i=0; $i <= 5; $i++) {											//循环画背景线
            $linecolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            $linex = mt_rand(1, $width-1);
            $liney = mt_rand(1, $height-1);
            imageline($im, $linex, $liney, $linex + mt_rand(0, 4) - 2, $liney + mt_rand(0, 4) - 2, $linecolor);
        }
        for($i=0; $i <= 32; $i++) {											//循环画背景点,生成麻点效果
            $pointcolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            imagesetpixel($im, mt_rand(1, $width-1), mt_rand(1, $height-1), $pointcolor);
        }
        imagerectangle($im, 0, 0, $width-1 , $height-1 , $frameColor);				//画边框
        //ob_clean();
        header('Content-type: image/png');
        imagepng($im);
        imagedestroy($im);
    }


    /**
    * get_verify
    *
    * 生成固定长度的随机字符串
    *
    * @access	public
    * @param	int $length 长度
    * @return	string
    */
    public function get_verify($length)
    {
        $strings = Array('0','1','2','3','4','5','6','7','8','9');
        $chrNum = "";
        $count = count($strings);
        for ($i = 1; $i <= $length; $i++) {								//循环随机取字符生成字符串
            $chrNum .= $strings[rand(0,$count-1)];
        }
        return $chrNum;
    }
}

/* End of file validcode_base_model.php */
/* Location: ./zsb/model/validcode_base_model.php */
