現(xiàn)在的生活中二維碼無處不在,使用非常廣泛。二維碼實際是一種條形碼,它可以將網(wǎng)址、文字、照片等信息通過相應(yīng)的編碼算法
編譯成為一個方塊形條碼圖案,手機(jī)用戶可以通過攝像頭和解碼軟件將相關(guān)信息重新解碼并查看內(nèi)容。哪么在PHP編程中如何實現(xiàn)
生成二維碼呢?下面我們來看PHP是如何通過php QR Code類庫生成二維碼。
1.google開放api
$urlToEncode="http://gz.altmi.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).
使用用例:
<?php
include('./phpqrcode/phpqrcode.php');
// 二維碼數(shù)據(jù)
$data = 'http://gz.altmi.com';
// 生成的文件名
$filename = $errorCorrectionLevel.'|'.$matrixPointSize.'.png';
// 糾錯級別:L、M、Q、H
$errorCorrectionLevel = 'L';
// 點的大小:1到10
$matrixPointSize = 4;
QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
官方給出的用例:
<?php
# include這兩個文件之一:
/*
qrlib.php for full version (also you have to provide all library files
form package plus cache dir)
OR phpqrcode.php for merged version (only one file,
but slower and less accurate code because disabled cache
and quicker masking configured)
*/
# 兩句話解釋:
# 包含qrlib.php的話需要同其它文件放到一起:文件、文件夾。
# phpqrcode.php是合并后版本,只需要包含這個文件,但生成的圖片速度慢而且不太準(zhǔn)確
# 以下給出兩種用法:
# 創(chuàng)建一個二維碼文件
QRcode::png('code data text', 'filename.png');
// creates file
# 生成圖片到瀏覽器
QRcode::png('some othertext 1234');
// creates code image and outputs it directly into browser
地址:http://phpqrcode.sourceforge.net/
下載:http://sourceforge.net/projects/phpqrcode/
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
原文:http://blog.chinaunix.net/uid-20787846-id-3492930.html
---------------
使用PHP QR Code類庫創(chuàng)建二維碼
使用舉例瀏覽器輸出:
<?
include "phpqrcode/phpqrcode.php";
$value="http://s.bookphone.cn/chinabook/index.php/adminhtml/Croles/admin";
$errorCorrectionLevel = "L";
$matrixPointSize = "4";
QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);
exit;
?>
文件輸出二維碼
include('phpqrcode/phpqrcode.php');
// 二維碼數(shù)據(jù)
$data = 'http://s.bookphone.cn';
// 生成的文件名
$filename = '1111.png';
// 糾錯級別:L、M、Q、H
$errorCorrectionLevel = 'L';
// 點的大小:1到10
$matrixPointSize = 4;
QRcode::png($data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
生成中間帶logo的二維碼
<?php
include('phpqrcode/phpqrcode.php');
$value='http://xy.bookphone.cn';
$errorCorrectionLevel = 'L';
$matrixPointSize = 6;
QRcode::png($value, 'xiangyang.png', $errorCorrectionLevel, $matrixPointSize, 2);
echo "QR code generated"."<br />";
$logo = 'logo.png';
$QR = 'xiangyang.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_height = imagesy($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,'xiangyanglog.png');
?>