天天看點

php生成二維碼

二維碼現在變得越來越流行了,這裡介紹兩端PHP的二維碼生成代碼,一段使用了Google開放的二維碼API,另一段使用的是PHP QR Code二維碼開源類庫,都很簡單。

1、使用Google Chart API生成二維碼圖檔

Google Chart API是一個功能十分強大的API,生成二維碼僅僅是其中的一個小功能。這裡隻介紹二維碼部分,如果想了解更多功能,可以登入官網位址:http://code.google.com/intl/zh-CN/apis/chart/

代碼如下:

<?php

$urlToEncode="http://www.weste.net";

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生成二維碼

2、使用PHP QR Code生成二維碼圖檔

PHP QR Code是一個開源的php二維碼開源類庫,基于libqrencode C庫,并提供API代碼建立QR條碼圖像,支援png、jpg格式。功能強大,使用起來也非常簡單。

demo代碼如下:

<?

include "./phpqrcode/phpqrcode.php";

$value="http://www.weste.net";

$errorCorrectionLevel = "L";

$matrixPointSize = "4";

QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);

exit;