《PHP生成二維碼的兩種方法(2)》要點:
本文介紹了PHP生成二維碼的兩種方法(2),希望對您有用。如果有疑問,可以聯系我們。
附:調用google生成二維碼的一個類示例:
QRcode是二維碼的一種。QRcode可以存儲最多4296個字母數字類型的任意文本。這些文本可以是任何內容,例如,網址、聯系信息、電話號碼(具體科查看二維碼數據格式)。QR code存儲的信息可以被安裝有適當軟件的光學設備讀取。這種設備既可以是專用的QR code讀取器也可以是手機。
通過調用 Google Chart Tools / Image Charts 的 API ,我們可以很方便的生成QRcode。
調用方式也很簡單,只要向 http://chart.apis.google.com/chart 傳入適合的參數就可以了,參數如下:
1. cht=qr
這個是必需的,告訴 API ,你需要生成的是二維碼。
2. chs=<width>x<height>
這個同樣是必需的,告訴 API ,你需要生成的二維碼的尺寸。
3. chl=<data>
這個還是必需的,用來告訴 API 二維碼所包含的信息。可以是數字、字符數字、字符、二進制信息、漢字。不能混合數據類型。數據必須經過UTF-8 URL-encoded。如果需要傳遞的信息超過2K個字節,請使用POST方式。
4. choe=<output_encoding>
終于來了個不是必須的,這個是用來聲明生成的二維碼所包含信息的編碼,默認是 UTF-8 ;其他可選編碼是 Shift_JIS 、 ISO-8859-1
5. chld=<error_correction_level>|<margin>
可選 糾錯等級。QR碼支持四個等級的糾錯,用來恢復丟失的、讀錯的、模糊的、數據。下面是可選的值:L-(默認)可以識別已損失7%的數據;M-可以識別已損失15%的數據;Q-可以識別已損失25%的數據;H-可以識別已損失30%的數據。margin 是指生成的二維碼離圖片邊框的距離。
QR碼是方形的,有相同的長和寬。QR碼的大小是固定的:從21到177的長/寬,每次遞增4個像素點。每個配置被稱為一個等級。長和寬越大,存儲的信息就越多。下面是版本摘要:
等級為1的QR碼長和寬分別為21個像素,最多可以存儲25個字母數字和字符。
等級為2的QR碼長和寬分別為25個像素,最多可以存儲47個字母數字和字符。
…以此類推 。
Chart API會根據你將存儲的信息的大小來決定使用哪個等級的QR碼。最棒的QR碼閱讀器可以讀取等級為40的QR碼中存儲的信息。然而通常來說移動設備最多可以讀取等級為4的QR碼中存儲的信息。
下面來介紹使用PHP調取Google Chart API 來生成二維碼
<?php class qrcode { private $data; //creating text qr code public function text($text){ $this->data = $text; } //creating code with link mtadata public function link($url){ if (preg_match('/^http:\/\//', $url) || preg_match('/^https:\/\//', $url)) { $this->data = $url; } else { $this->data = "<a href="http://".$url">http://".$url</a>; } } //creating code with bookmark metadata public function bookmark($title, $url){ $this->data = "MEBKM:TITLE:".$title.";URL:".$url.";;"; } //creating code with email address metadata public function email_address($email){ $this->data = "<a href="mailto:".$email">MAILTO:".$email</a>; } //creating code with email metadata public function email($email, $subject, $message){ $this->data = "MATMSG:TO:".$email.";SUB:".$subject.";BODY:".$message.";;"; } //creating code with phone public function phone_number($phone){ $this->data = "TEL:".$phone; } //creating code with sms metadata public function sms($phone, $text){ $this->data = "SMSTO:".$phone.":".$text; } //creating code with mms metadata public function mms($phone, $text){ $this->data = "MMSTO:".$phone.":".$text; } //creating code with mecard metadata public function contact_info($name, $address, $phone, $email){ $this->data = "MECARD:N:".$name.";ADR:".$address.";TEL:".$phone.";EMAIL:".$email.";;"; } //creating code with geo location metadata public function geo($lat, $lon, $height){ $this->data = "GEO:".$lat.",".$lon.",".$height; } //creating code with wifi configuration metadata public function wifi($type, $ssid, $pass){ $this->data = "WIFI:T:".$type.";S".$ssid.";".$pass.";;"; } //creating code with i-appli activating meta data public function iappli($adf, $cmd, $param){ $cur = current($param); $next = next($param); $param_str = ""; foreach($cur as $key => $val) { $param_str .= "PARAM:".$val.",".$next[$key].";"; } $this->data = "LAPL:ADFURL:".$adf.";CMD:".$cmd.";".$param_str.";"; } //creating code with gif or jpg image, or smf, MFi or ToruCa files as content public function content($type, $size, $content){ $this->data = "CNTS:TYPE:".$type.";LNG:".$size.";BODY:".$content.";;"; } //getting image public function get_image($size = 150, $EC_level = 'L', $margin = '0'){ $ch = curl_init(); $this->data = urlencode($this->data); curl_setopt($ch, CURLOPT_URL, 'http://chart.apis.google.com/chart'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, 'chs='.$size.'x'.$size.'&cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$this->data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $response = curl_exec($ch); curl_close($ch); return $response; } //getting link for image public function get_link($size = 150, $EC_level = 'L', $margin = '0'){ $this->data = urlencode($this->data); return 'http://chart.apis.google.com/chart?chs='.$size.'x'.$size.'&cht=qr&chld='.$EC_level.'|'.$margin.'&chl='.$this->data; } //forsing image download public function download_image($file){ header('Content-Description: File Transfer'); header('Content-Type: image/png'); header('Content-Disposition: attachment; filename=QRcode.png'); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); echo $file; } } ?>
使用上述二維碼PHP類的方法:
<?php
include("qrcode.php");
$qr = new qrcode();
//bookmark
$title = "標點符";
$url = "http://www.biaodianfu.com/";
$qr->bookmark($title,$url);
//獲取二維碼圖片URL
echo "<img src='".$qr->get_link()."'>";
//here is the way to output image
//header("Content-type:image/png");
//echo $qr->get_image();
//and here is the way to force image download
//$file = $qr->get_image();
//$qr->download_image($file)
?>
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/28_2.html