php二維碼產生_php技巧

來源:互聯網
上載者:User

本文介紹兩種使用 php 產生二維碼的方法。
(1)利用google產生二維碼的開放介面,代碼如下:

/**  * google api 二維碼產生【QRcode可以儲存最多4296個字母數字類型的任意文本,具體可以查看二維碼資料格式】  * @param string $data 二維碼包含的資訊,可以是數字、字元、二進位資訊、漢字。不能混合資料類型,資料必須經過UTF-8 URL-encoded.如果需要傳遞的資訊超過2K個位元組,請使用POST方式  * @param int $widhtHeight 產生二維碼的尺寸設定  * @param string $EC_level 可選錯誤修正層級,QR碼支援四個等級錯誤修正,用來恢複丟失的、讀錯的、模糊的、資料。  *       L-預設:可以識別已損失的7%的資料  *       M-可以識別已損失15%的資料  *       Q-可以識別已損失25%的資料  *       H-可以識別已損失30%的資料  * @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='著作權:http://www.jb51.net'; generateQRfromGoogle($data); 

post方法實現請求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='著作權:http://www.jb51.net';echo qrcode($width,$height,$data); 

當然產生的圖片同上面是一樣的。

(2)使用php QR Code類庫產生二維碼
注意使用該類庫必須首先下載類庫包,下載地址:
地址:http://phpqrcode.sourceforge.net/
下載下來的壓縮包裡面有很多樣本,可以自行研究,下面給出一個簡單的使用案例(具體參數的意思和上面大同小異):

<?php include "./phpqrcode.php"; $data='著作權:http://www.jb51.net'; $errorCorrectionLevel="L"; $matrixPointSize="4"; QRcode::png($data,false,$errorCorrectionLevel,$matrixPointSize);

以上所述就是本文的全部內容了,希望對大家熟練掌握php生產二維碼能夠有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.