国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php二維碼生成

2024-05-04 23:39:38
字體:
供稿:網(wǎng)友

在二維碼越來越普及之際。我們需要了解一下什么是二維碼,作為了一個(gè)php programer,我們更需要知道如何使用 php生成二維碼。廢話少說,下面說正題。

本文介紹兩種使用 php 生成二維碼的方法。

(1)利用google生成二維碼的開放接口,代碼如下:

 

 
  1. /**  
  2. * google api 二維碼生成【QRcode可以存儲(chǔ)最多4296個(gè)字母數(shù)字類型的任意文本,具體可以查看二維碼數(shù)據(jù)格式】  
  3. * @param string $data 二維碼包含的信息,可以是數(shù)字、字符、二進(jìn)制信息、漢字。不能混合數(shù)據(jù)類型,數(shù)據(jù)必須經(jīng)過UTF-8 URL-encoded.如果需要傳遞的信息超過2K個(gè)字節(jié),請(qǐng)使用POST方式  
  4. * @param int $widhtHeight 生成二維碼的尺寸設(shè)置  
  5. * @param string $EC_level 可選糾錯(cuò)級(jí)別,QR碼支持四個(gè)等級(jí)糾錯(cuò),用來恢復(fù)丟失的、讀錯(cuò)的、模糊的、數(shù)據(jù)。  
  6. * L-默認(rèn):可以識(shí)別已損失的7%的數(shù)據(jù)  
  7. * M-可以識(shí)別已損失15%的數(shù)據(jù)  
  8. * Q-可以識(shí)別已損失25%的數(shù)據(jù)  
  9. * H-可以識(shí)別已損失30%的數(shù)據(jù)  
  10. * @param int $margin 生成的二維碼離圖片邊框的距離  
  11. */ 
  12. function generateQRfromGoogle($data,$widhtHeight='150',$EC_level='L',$margin='0'){  
  13. $url=urlencode($data);  
  14. 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.'"/>';  
  15. }  

使用方法:

 

 
  1. $data='版權(quán)所有:http://m.survivalescaperooms.com';  
  2. generateQRfromGoogle($data); 

post方法實(shí)現(xiàn)請(qǐng)求google api 生成二維碼的方式:

 

  1. function qrcode($width,$height,$string){  
  2. $post_data=array();  
  3. $post_data['cht']='qr';  
  4. $post_data['chs']=$width."x".$height;  
  5. $post_data['chl']=$string;  
  6. $post_data['choe']="UTF-8";  
  7. $url="http://chart.apis.google.com/chart";  
  8. $data_Array=array();  
  9. foreach($post_data as $key=>$value){  
  10. $data_Array[]=$key.'='.$value;  
  11. }  
  12. $data=implode("&",$data_Array);  
  13. $ch=curl_init();  
  14. curl_setopt($ch, CURLOPT_POST, 1);  
  15. curl_setopt($ch, CURLOPT_HEADER, 0);  
  16. curl_setopt($ch, CURLOPT_URL, $url);  
  17. curl_setopt($ch, CURLOPT_POSTFIELDS,$data);  
  18. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  19. $result=curl_exec($ch);  
  20. //echo "<img src =/"data:image/png;base64,".base64_encode($result)."/" >"; 注意,不寫header的寫法  
  21. return $result;  

使用方法:

 

 
  1. header("Content-type:image/png");  
  2. $width=300;  
  3. $height=300;  
  4. $data='版權(quán)所有:http://m.survivalescaperooms.com'
  5. echo qrcode($width,$height,$data);  

當(dāng)然生成的圖片同上面是一樣的。

(2)使用php QR Code類庫生成二維碼

注意使用該類庫必須首先下載類庫包,下載地址:

地址:http://phpqrcode.sourceforge.net/

下載下來的壓縮包里面有很多示例,可以自行研究,下面給出一個(gè)簡單的使用案例(具體參數(shù)的意思和上面大同小異):

 

 
  1. <?php  
  2. include "./phpqrcode.php";  
  3. $data='版權(quán)所有:<a href="http://m.survivalescaperooms.com'">http://m.survivalescaperooms.com'</a>;  
  4. $errorCorrectionLevel="L";  
  5. $matrixPointSize="4";  
  6. QRcode::png($data,false,$errorCorrectionLevel,$matrixPointSize); 

以上所述就是本文的全部內(nèi)容了,希望對(duì)大家熟練掌握php生產(chǎn)二維碼能夠有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 原阳县| 邯郸市| 呼玛县| 广安市| 德清县| 莒南县| 日照市| 阳朔县| 安仁县| 江都市| 沙河市| 天门市| 崇礼县| 和平县| 沧源| 淄博市| 东方市| 黔西| 临江市| 灵川县| 安陆市| 六盘水市| 九龙城区| 西乌| 报价| 隆德县| 马边| 赤壁市| 闵行区| 台湾省| 习水县| 商水县| 奈曼旗| 九寨沟县| 合江县| 科尔| 达州市| 铜梁县| 兴文县| 永泰县| 阿城市|