码迷,mamicode.com
首页 > Web开发 > 详细

PHP生成验证码及单实例应用

时间:2015-12-01 14:47:59      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

/* note:
 * this     指向当前对象本身
 * self     指向当前类
 * parent   指向父类
 */

/* 验证码工具类
 * @author pandancode
 * @date 20150-12-1
 */
class Verification_Code{
    
    private $_vode;
    private $_vode_len;
    private $_img_handle;
    private $_img_width;
    private $_img_height;
    private $_img_type;
    private $_img_bg_color;
    private $_img_color;
    
    /* 单实例 */
    private static $_instance;
    
    /* 获取当前实例 */
    public static function getInstance(){
        if( !(self::$_instance instanceof self) ){
            self::$_instance = new self;
        }
        return self::$_instance;
    }
    
    /* 构造函数,设置 为private,禁止外部访问 */
    private function __Construct(){}
    
    /* 设置 为private,禁止外部访问,禁止外部访问 */
    private function __clone(){}
    
    /* 生成验证码图片前设置参数
     * @param string vode 验证码 
     * @param int vode_len 验证码长度 
     * @param string img_type 生成图片格式,默认为png 
     */
    public function set_param($vode,$vode_len,$width,$height,$img_type=‘png‘,$img_bg_color=array(‘R‘=>250,‘G‘=>‘250‘,‘B‘=>‘250‘),$img_color=array(‘R‘=>0,‘G‘=>0,‘B‘=>0)){
        $this->_vode = $vode;
        $this->_vode_len = $vode_len;
        $this->_img_width = $width;
        $this->_img_height = $height;
        $this->_img_type = $_img_type;
        $this->_img_bg_color = $img_bg_color;
        $this->_img_color = $img_color;
    }
    
    /* 生成图片 
     */
    public function create_img(){
        $this->_img_handle = ImageCreate($this->_img_width,$this->_img_height);
        $this->_img_bg_color = ImageColorAllocate($this->_img_handle,$this->_img_bg_color[‘R‘],$this->_img_bg_color[‘G‘],$this->_img_bg_color[‘B‘]);
        $this->_img_color = ImageColorAllocate($this->_img_handle,$this->_img_color[‘R‘],$this->_img_color[‘G‘],$this->_img_color[‘B‘]);
        
        //填充背景颜色
        Imagefill($this->_img_handle,0,0,$this->_img_bg_color);
        ImageString($this->_img_handle,10,10,10,$this->_vode,$this->_img_color);
        
        ob_clean();
        header(‘Content-type:image/png‘);
        Imagepng($this->_img_handle);
    }
    
}

$img_tool = Verification_Code::getInstance();
$img_tool->set_param(‘ABVS‘,4,80,40);
$img_tool->create_img();

 

PHP生成验证码及单实例应用

标签:

原文地址:http://www.cnblogs.com/pandang/p/5009953.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!