php生成二維碼的幾種方式整理及使用實例
更新時間:2013年06月03日 15:03:49 投稿:whsnow
本文整理了一些php生成二維碼的方式:1.google開放api;2.php類庫PHP QR Code;3.libqrencode;4.QRcode Perl CGI & PHP scripts感興趣的朋友可以參考下哈
1.google開放api
復(fù)制代碼 代碼如下:
$urlToEncode="http://bbs.lewanchina.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.'"/>';
}

2.php類庫PHP QR Code
PHP QR Code is open source (LGPL) library for generating QR Code,
2-dimensional barcode. Based on libqrencode C library,
provides API for creating QR Code barcode images (PNG, JPEG thanks to GD2).
Implemented purely in PHP, with no external dependencies (except GD2 if needed).
地址:http://phpqrcode.sourceforge.net/
下載:http://sourceforge.net/projects/phpqrcode/
實例:
復(fù)制代碼 代碼如下:
<?
include "./phpqrcode/phpqrcode.php";
$value="http://www.weste.net";
$errorCorrectionLevel = "L";
$matrixPointSize = "4";
QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);
exit;
?>
主頁地址:http://phpqrcode.sourceforge.net/,站點里能直接下載源碼,下載后解壓到web目錄,通過網(wǎng)頁訪問,就可以看到demo演示。

Data表示要記錄的數(shù)據(jù),如果是存儲utf-8編碼的中文,最多984個。
ECC表示糾錯級別, 糾錯級別越高,生成圖片會越大。
L水平 7%的字碼可被修正
M水平 15%的字碼可被修正
Q水平 25%的字碼可被修正
H水平 30%的字碼可被修正Size表示圖片每個黑點的像素。
代碼的調(diào)用范例:
復(fù)制代碼 代碼如下:
include "qrlib.php";
QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, $margin);
/*
$data 數(shù)據(jù)
$filename 保存的圖片名稱
$errorCorrectionLevel 錯誤處理級別
$matrixPointSize 每個黑點的像素
$margin 圖片外圍的白色邊框像素
*/
QR碼詳細原理可以參考QR維基百科,中文版介紹比較少,英文版介紹很全面,推薦看英文版的。
另外,現(xiàn)在QR碼已經(jīng)有彩色版的了,還可以嵌入個性圖片,我在這方面做了些研究, 如果有興趣,可以和我聯(lián)系交流。我的聯(lián)系郵箱參考網(wǎng)站右邊欄目。
3.libqrencode
地址:http://fukuchi.org/works/qrencode/index.en.html
php支持請參考:http://hirokawa.netflowers.jp/entry/4900/
4.QRcode Perl CGI & PHP scripts
地址:http://www.swetake.com/qr/qr_cgi.html
PS:這里再提供一個本站二維碼工具(帶logo與各種定制功能),功能十分強大!免費供大家使用:
相關(guān)文章
PHP實現(xiàn)基于面向?qū)ο蟮膍ysqli擴展庫增刪改查操作工具類
這篇文章主要介紹了PHP實現(xiàn)基于面向?qū)ο蟮膍ysqli擴展庫增刪改查操作工具類,結(jié)合實例形式分析了mysqli增刪改查操作類的封裝與使用技巧,需要的朋友可以參考下2017-07-07PHP+sqlite數(shù)據(jù)庫操作示例(創(chuàng)建/打開/插入/檢索)
這篇文章主要介紹了PHP+sqlite數(shù)據(jù)庫操作的方法,簡單分析了sqlite數(shù)據(jù)庫的功能及相關(guān)操作技巧,包括創(chuàng)建,打開,插入,檢索及錯誤提示等,需要的朋友可以參考下2016-05-05golang、python、php、c++、c、java、Nodejs性能對比
這篇文章主要介紹了golang、python、php、c++、c、java、Nodejs性能對比的相關(guān)資料,需要的朋友可以參考下2017-03-03