二維碼如今變得愈來愈流行了,這裏介紹兩端PHP的二維碼生成代碼,一段使用了Google開放的二維碼API,另外一段使用的是PHP QR Code二維碼開源類庫,都很簡單。php
一、使用Google Chart API生成二維碼圖片html
Google Chart API是一個功能十分強大的API,生成二維碼僅僅是其中的一個小功能。這裏只介紹二維碼部分,若是想了解更多功能,能夠登陸官網地址:http://code.google.com/intl/zh-CN/apis/chart/api
代碼以下:google
<?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.'"/>';
}
?>url
生成的二維碼圖片:.net
二、使用PHP QR Code生成二維碼圖片code
PHP QR Code是一個開源的php二維碼開源類庫,基於libqrencode C庫,並提供API代碼建立QR條碼圖像,支持png、jpg格式。功能強大,使用起來也很是簡單。htm
demo代碼以下:圖片
<?
include "./phpqrcode/phpqrcode.php";
$value="http://www.weste.net";
$errorCorrectionLevel = "L";
$matrixPointSize = "4";
QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);
exit;
?>get
生成的二維碼圖片: