一个非常不错的 php QR Code 类库 使用类

//引入phpqucode类库
require_once "phpqrcode.php";
class Custom_QRcode extends Custom_Object {
    private $qrcodePath;
    public function __construct($option=array()){
        $_option=array(
                 "basePath"=>APPLICATION_PATH."/",
                 "relativePath"=>"/",
                 "errorCorrectionLevel"=>"L",//// 纠错级别:7%L、15%M、25%Q、30%H--字�a可被修正
                    "matrixPointSize"=>4,//100x100--5:125--6:150.....(max10)
                 "margin"=>2,
                
         );
        $this->setConfig($_option);
         $this->setConfig($option);
    }
    /*
    $data 数据
     $filename 保存的图片名称
     $errorCorrectionLevel 错误处理级别
     $matrixPointSize 每个黑点的像素
     $margin 图片外围的白色边框像素
    */
    public function createQRcode($data=''){
            if(!$data){return false;}
            $options = $this->_option;
            $PNG_TEMP_DIR = $options['basePath'].$options['relativePath'].'/';
            if(!is_dir($PNG_TEMP_DIR)){
                mkdir($PNG_TEMP_DIR,0777,true);//php 5
            }
            $errorCorrectionLevel = $options['errorCorrectionLevel'];
            $matrixPointSize = $options['matrixPointSize'];
            $margin = $options['margin'];
            $qrname = $this->getNewFileName($data);
            $this->qrcodePath = $options['relativePath'].'/'.$qrname;
            $filename = $PNG_TEMP_DIR.$qrname;
            QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, $margin); 
            return true;
    }
    
    /*** 设置配置选项* * @param $option* @return void*/
    
    public function setConfig($option){ 
        foreach($option as $key=>$val){
            $this->_option[$key]=$val;
        } 
    }
    /*�@取文件名*/
    public function getNewFileName($data){
        $ecl = $this->_option['errorCorrectionLevel'];
        $mps = $this->_option['matrixPointSize'];
        $md5 = md5($data.'|'.$ecl.'|'.$mps);
        $extName = '.png';
        return substr($md5,8,16).rand(1000,10000).$extName;
    }
    //�@取�D片相�β��;
    public function getQrPicPath(){
        return $this->qrcodePath;
    }
}

//生成二维码用法
    $qrdata = 'http://sourceforge.net/projects/phpqrcode/';//qrcode 类库下载地址
    $QRcode = new Custom_QRcode(array(
     "relativePath"=>"/qrcode/",
    ));
    $QRcode->createQRcode($qrdata);
    $qrcodesrc = $QRcode->getQrPicPath();

本文出自 “飞翔2013” 博客,谢绝转载!

你可能感兴趣的:(PHP,QRCode,类库)