zoukankan      html  css  js  c++  java
  • PHP生成验证码及单实例应用

    /* 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();
    

      

  • 相关阅读:
    协变与逆变
    反射
    TreeCombo
    使用TreeCombo示例
    Calling R from java using JRI
    [转]相似度计算常用方法综述
    [转]聚类算法KMeans, KMedoids, GMM, Spectral clustering,Ncut
    Learning D3.js with App iLearning D3.js
    [repost ]经典的机器学习方面源代码库
    [转]数据挖掘中所需的概率论与数理统计知识、上
  • 原文地址:https://www.cnblogs.com/pandang/p/5074008.html
Copyright © 2011-2022 走看看