PHP二维码生成的方法(google APi,PHP类库,libqrencode等)

二维码 ,又称二维条形码,它是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的,在代码编制上巧妙地利用构成计算机内部逻辑基础的“0”、“1”比特流的概念,使用若干个与二进制相对应的几何形体来表示文字数值信息,通过图象输入设备或光电扫描设备自动识读以实现信息自动处理。它具有条码技术的一些共性:每种码制有其特定的字符集;每个字符占有一定的宽度;具有一定的校验功能等。同时还具有对不同行的信息自动识别功能、及处理图形旋转变化等特点。

  PHP生成QR Code的google API

复制代码
$urlToEncode="http://www.google.com";
generateQRfromGoogle($urlToEncode);
function generateQRfromGoogle($chl,$widhtHeight ='150',$EC_level='L',$margin='0') {
   $url = urlencode($url);
  echo '<img src="http://chart.apis.google.com/chart?
  chs='.$widhtHeight.'x'.$widhtHeight.'&
  cht=qr&chld='.$EC_level.'|'.$margin.'&
  chl='.$chl.'" alt="QR code" widhtHeight="'.$size.'" widhtHeight="'.$size.'"/>';
}
复制代码

  PHP生成QR Code的 php QR Code 类库

复制代码
<?php
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--字碼可被修正
                    "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;
        

// 下面注释了把二维码图片保存到本地的代码,如果要保存图片,用$fileName替换第二个参数false
//$path = "images/";
// 生成的文件名
//$fileName = $path.$size.'.png';

//QRcode::png($data,false, $level, $size);


            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;
    }
    //獲取圖片相對路徑;
    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();
复制代码

  JQuery生成QR Code

复制代码

<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="jquery.qrcode.min.js"></script>
<div id="qrcode"></div>
<script type="text/javascript">
$("#qrcode").qrcode("helin");
//或者你可以设置生成的二维码的宽和高还有颜色等
$("#qrcode").qrcode({
  render: 'canvas',// render method: 'canvas' or 'div'
 // width and height in pixel
  width: 100,
  height: 100,
  color: '#3a3',// QR code color
  bgColor: null,// background color, null for transparent background
  text: 'http://qrcode'// the encoded text

});

</script>

posted @ 2013-11-04 09:53  hechunhua  阅读(363)  评论(0编辑  收藏  举报