php二維碼生成
本文介紹兩種使用 php 生成二維碼的方法。
(1)利用google生成二維碼的開放接口,代碼如下:
/** * google api 二維碼生成【QRcode可以存儲(chǔ)最多4296個(gè)字母數(shù)字類型的任意文本,具體可以查看二維碼數(shù)據(jù)格式】 * @param string $data 二維碼包含的信息,可以是數(shù)字、字符、二進(jìn)制信息、漢字。不能混合數(shù)據(jù)類型,數(shù)據(jù)必須經(jīng)過UTF-8 URL-encoded.如果需要傳遞的信息超過2K個(gè)字節(jié),請(qǐng)使用POST方式 * @param int $widhtHeight 生成二維碼的尺寸設(shè)置 * @param string $EC_level 可選糾錯(cuò)級(jí)別,QR碼支持四個(gè)等級(jí)糾錯(cuò),用來恢復(fù)丟失的、讀錯(cuò)的、模糊的、數(shù)據(jù)。 * L-默認(rèn):可以識(shí)別已損失的7%的數(shù)據(jù) * M-可以識(shí)別已損失15%的數(shù)據(jù) * Q-可以識(shí)別已損失25%的數(shù)據(jù) * H-可以識(shí)別已損失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方法實(shí)現(xiàn)請(qǐng)求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);
當(dāng)然生成的圖片同上面是一樣的。
(2)使用php QR Code類庫生成二維碼
注意使用該類庫必須首先下載類庫包,下載地址:
地址:http://phpqrcode.sourceforge.net/
下載下來的壓縮包里面有很多示例,可以自行研究,下面給出一個(gè)簡單的使用案例(具體參數(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)容了,希望對(duì)大家熟練掌握php生產(chǎn)二維碼能夠有所幫助。
相關(guān)文章
部署PHP項(xiàng)目應(yīng)該注意的幾點(diǎn)事項(xiàng)分享
這篇文章主要介紹了部署PHP項(xiàng)目應(yīng)該注意的幾點(diǎn)事項(xiàng),有需要的朋友可以參考一下2013-12-12PHP 過濾頁面中的BOM(實(shí)現(xiàn)代碼)
本篇文章是對(duì)PHP中過濾頁面中的BOM的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06『PHP』PHP截?cái)嗪瘮?shù)mb_substr()使用介紹
截?cái)辔恼聵?biāo)題,控制在15個(gè)文字,接下來為大家講解下如何實(shí)現(xiàn)這個(gè)需求,感興趣的朋友可以參考下哈2013-04-04fleaphp crud操作之findByField函數(shù)的使用方法
fleaphp crud操作之findByField函數(shù)的用法分享,需要的朋友可以參考下。2011-04-04