php二維碼生成
本文介紹兩種使用 php 生成二維碼的方法。
(1)利用google生成二維碼的開放接口,代碼如下:
/**
* google api 二維碼生成【QRcode可以存儲最多4296個字母數(shù)字類型的任意文本,具體可以查看二維碼數(shù)據(jù)格式】
* @param string $data 二維碼包含的信息,可以是數(shù)字、字符、二進制信息、漢字。不能混合數(shù)據(jù)類型,數(shù)據(jù)必須經(jīng)過UTF-8 URL-encoded.如果需要傳遞的信息超過2K個字節(jié),請使用POST方式
* @param int $widhtHeight 生成二維碼的尺寸設(shè)置
* @param string $EC_level 可選糾錯級別,QR碼支持四個等級糾錯,用來恢復丟失的、讀錯的、模糊的、數(shù)據(jù)。
* L-默認:可以識別已損失的7%的數(shù)據(jù)
* M-可以識別已損失15%的數(shù)據(jù)
* Q-可以識別已損失25%的數(shù)據(jù)
* H-可以識別已損失30%的數(shù)據(jù)
* @param int $margin 生成的二維碼離圖片邊框的距離
*/
function generateQRfromGoogle($data,$widhtHeight='150',$EC_level='L',$margin='0'){
$url=urlencode($data);
echo '<img src="http://chart.apis.google.com/chart?chs='.$widhtHeight.'x'.$widhtHeight.'&cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$data.'" widhtHeight="'.$widhtHeight.'" widhtHeight="'.$widhtHeight.'"/>';
}
使用方法:
$data='版權(quán)所有:http://www.dbjr.com.cn'; generateQRfromGoogle($data);
post方法實現(xiàn)請求google api 生成二維碼的方式:
function qrcode($width,$height,$string){
$post_data=array();
$post_data['cht']='qr';
$post_data['chs']=$width."x".$height;
$post_data['chl']=$string;
$post_data['choe']="UTF-8";
$url="http://chart.apis.google.com/chart";
$data_Array=array();
foreach($post_data as $key=>$value){
$data_Array[]=$key.'='.$value;
}
$data=implode("&",$data_Array);
$ch=curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec($ch);
//echo "<img src =\"data:image/png;base64,".base64_encode($result)."\" >"; 注意,不寫header的寫法
return $result;
}
使用方法:
header("Content-type:image/png");
$width=300;
$height=300;
$data='版權(quán)所有:http://www.dbjr.com.cn';
echo qrcode($width,$height,$data);
當然生成的圖片同上面是一樣的。
(2)使用php QR Code類庫生成二維碼
注意使用該類庫必須首先下載類庫包,下載地址:
地址:http://phpqrcode.sourceforge.net/
下載下來的壓縮包里面有很多示例,可以自行研究,下面給出一個簡單的使用案例(具體參數(shù)的意思和上面大同小異):
<?php include "./phpqrcode.php"; $data='版權(quán)所有:http://www.dbjr.com.cn'; $errorCorrectionLevel="L"; $matrixPointSize="4"; QRcode::png($data,false,$errorCorrectionLevel,$matrixPointSize);
以上所述就是本文的全部內(nèi)容了,希望對大家熟練掌握php生產(chǎn)二維碼能夠有所幫助。
相關(guān)文章
『PHP』PHP截斷函數(shù)mb_substr()使用介紹
截斷文章標題,控制在15個文字,接下來為大家講解下如何實現(xiàn)這個需求,感興趣的朋友可以參考下哈2013-04-04
fleaphp crud操作之findByField函數(shù)的使用方法
fleaphp crud操作之findByField函數(shù)的用法分享,需要的朋友可以參考下。2011-04-04

