php二維碼生成
阿新 • • 發佈:2019-01-10
php生成二維碼的幾種方式,用起來挺方便,實現的幾種方法都經過測試通過
1.利用google開放介面
get方法實現方式一:
- $urlToEncode="163.com";
- generateQRfromGoogle($urlToEncode);
- function generateQRfromGoogle($chl,$widhtHeight ='150',$EC_level='L',$margin='0') {
- $url = urlencode($url);
- return '<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.'"/>';
- }
post方法實現方式:
- $width = 300;
- $height = 300;
- $string = "163.com";
- 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);
- //echo $data;
- $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");
- echo qrcode($width,$height,$string);
2.利用php類庫PHP QR Code來實現
首先下載類庫包
地址:http://phpqrcode.sourceforge.net/
下載:http://sourceforge.net/projects/phpqrcode/
- <?
- include "./phpqrcode/phpqrcode.php";
- $value="http://www.weste.net";
- $errorCorrectionLevel = "L";
- $matrixPointSize = "4";
- QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);
- exit;
- ?>