php產生二維碼的三種方法

來源:互聯網
上載者:User

最簡單最執行個體的goolge開源方法

1.google開放api

代碼如下:

$urlToEncode="http://www.helloweba.com"; generateQRfromGoogle($urlToEncode); /**  * google api 二維碼產生【QRcode可以儲存最多4296個字母數字類型的任意文本,具體可以查看二維碼資料格式】  * @param string $chl 二維碼包含的資訊,可以是數字、字元、二進位資訊、漢字。  不能混合資料類型,資料必須經過UTF-8 URL-encoded  * @param int $widhtHeight 產生二維碼的尺寸設定  * @param string $EC_level 可選錯誤修正層級,QR碼支援四個等級錯誤修正,用來恢複丟失的、讀錯的、模糊的、資料。  *                            L-預設:可以識別已損失的7%的資料  *                            M-可以識別已損失15%的資料  *                            Q-可以識別已損失25%的資料  *                            H-可以識別已損失30%的資料  * @param int $margin 產生的二維碼離圖片邊框的距離  */ function generateQRfromGoogle($chl,$widhtHeight ='150',$EC_level='L',$margin='0') {     $chl = urlencode($chl);     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="'.$widhtHeight.'     " widhtHeight="'.$widhtHeight.'"/>'; }

2.php類庫PHP QR Code

地址:http://phpqrcode.sourceforge.net/
下載:http://sourceforge.net/projects/phpqrcode/

下載官網提供的類庫後,只需要使用phpqrcode.php就可以產生二維碼了,當然您的PHP環境必須開啟支援GD2。phpqrcode.php提供了一個關鍵的png()方法,其中參數$text表示產生二位的的資訊文本;參數$outfile表示是否輸出二維碼圖片檔案,預設否;參數$level表示容錯率,也就是有被覆蓋的地區還能識別,分別是L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%);參數$size表示產生圖片大小,預設是3;參數$margin表示二維碼周圍邊框空白地區間距值;參數$saveandprint表示是否儲存二維碼並顯示。

代碼如下:

public static function png($text, $outfile=false, $level=QR_ECLEVEL_L, $size=3, $margin=4,  $saveandprint=false)  {     $enc = QRencode::factory($level, $size, $margin);     return $enc->encodePNG($text, $outfile, $saveandprint=false); }

調用PHP QR Code:

include 'phpqrcode.php'; QRcode::png('http://www.helloweba.com');


實際應用中,我們會在二維碼的中間加上自己的LOGO,已增強宣傳效果。那如何產生含有logo的二維碼呢?其實原理很簡單,先使用PHP QR Code產生一張二維碼圖片,然後再利用php的image相關函數,將事先準備好的logo圖片加入到剛產生的原始二維碼圖片中間,然後重建一張新的二維碼圖片。

include 'phpqrcode.php';  $value = 'http://www.helloweba.com'; //二維碼內容 $errorCorrectionLevel = 'L';//容錯層級 $matrixPointSize = 6;//產生圖片大小 //產生二維碼圖片 QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2); $logo = 'logo.png';//準備好的logo圖片 $QR = 'qrcode.png';//已經產生的原始二維碼圖   if ($logo !== FALSE) {     $QR = imagecreatefromstring(file_get_contents($QR));     $logo = imagecreatefromstring(file_get_contents($logo));     $QR_width = imagesx($QR);//二維碼圖片寬度     $QR_height = imagesy($QR);//二維碼圖片高度     $logo_width = imagesx($logo);//logo圖片寬度     $logo_height = imagesy($logo);//logo圖片高度     $logo_qr_width = $QR_width / 5;     $scale = $logo_width/$logo_qr_width;     $logo_qr_height = $logo_height/$scale;     $from_width = ($QR_width - $logo_qr_width) / 2;     //重新組合圖片並調整大小     imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,      $logo_qr_height, $logo_width, $logo_height); } //輸出圖片 imagepng($QR, 'helloweba.png'); echo '<img src="helloweba.png">';

對於第二種方法:如果不使用$filename,第二個參數為false的時候,就不會把二維碼圖片儲存,而是直接輸出。

現在還有一些如有:libqrencode與QRcode Perl CGI & PHP scripts二維碼產生外掛程式大家喜歡也可看看。

3.基於jquery的二維碼產生外掛程式qrcode,在頁面中調用該外掛程式就能產生對應的二維碼。

qrcode其實是通過使用jQuery實現圖形渲染,畫圖,支援canvas(HTML5)和table兩種方式,

可以到https://github.com/jeromeetienne/jquery-qrcode擷取最新的代碼。

如何使用

(1)、首先在頁面中加入jquery庫檔案和qrcode外掛程式。

<script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="jquery.qrcode.min.js"></script>

(2)、在頁面中需要顯示二維碼的地方加入以下代碼:

<p id="code"></p>

(3)、調用qrcode外掛程式。

qrcode支援canvas和table兩種方式進行圖片渲染,預設使用canvas方式,效率最高,當然要瀏覽器支援html5。直接調用如下:

$('#code').qrcode("http://www.helloweba.com"); //任一字元串

您也可以通過以下方式調用:

$("#code").qrcode({     render: "table", //table方式     width: 200, //寬度     height:200, //高度     text: "www.helloweba.com" //任意內容 });

這樣就可以在頁面中直接產生一個二維碼,你可以用手機“掃一掃”功能讀取二維碼資訊。

(4).識別中文

我們實驗的時候發現不能識別中文內容的二維碼,通過尋找多方資料瞭解到,jquery-qrcode是採用charCodeAt()方式進行編碼轉換的。而這個方法預設會擷取它的Unicode編碼,如果有中文內容,在產生二維碼前就要把字串轉換成UTF-8,然後再產生二維碼。您可以通過以下函數來轉換中文字串:

function toUtf8(str) {        var out, i, len, c;        out = "";        len = str.length;        for(i = 0; i < len; i++) {            c = str.charCodeAt(i);            if ((c >= 0x0001) && (c <= 0x007F)) {                out += str.charAt(i);            } else if (c > 0x07FF) {                out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));                out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));                out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));            } else {                out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));                out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));            }        }        return out;    }

以下樣本:

var str = toUtf8("普羅旺斯沒有故事"); $('#code').qrcode(str);

本文介紹了php產生二維碼的三種方法,更多相關內容請關注php中文網。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.