首先去官網下載PHPqrCode庫文件,只需要里面的phpqrcode.php文件,下載地址:http://phpqrcode.sourceforge.net
/** * phpqrcode php生成二維碼 * $frame string 二維碼內容 * $filename string|false 默認為否,不生成文件,只將二維碼圖片返回,否則需要給出存放生成二維碼圖片的路徑 * $level 默認為L,這個參數可傳遞的值分別是L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)。這個參數控制二維碼容錯率,不同的參數表示二維碼可被覆蓋的區域百分比。 * $size int 生成二維碼的區域大小點的大小:1到10 * $margin int 圖片留白大小 * $saveandprint string 保存二維碼圖片並顯示出來,$outfile必須傳遞圖片路徑 */ function qrcode($frame, $filename = false, $level = 'L', $size = 5, $margin = 2, $saveandprint=false){ header('Content-Type: image/png'); Vendor('phpqrcode.phpqrcode'); $qrcode = new \QRcode(); ob_clean(); $png = $qrcode->png($frame, $filename , $level , $size , $margin , $saveandprint); return $png; } /** * 生成二維碼以base64輸出, * $frame 二維碼內容 * 參數同qrcode……………… */ function qrcode64($frame, $level = 'L', $size = 5, $margin = 2){ Vendor('phpqrcode.phpqrcode'); $QRcode = new \QRcode(); ob_start(); // 在服務器打開一個緩沖區來保存所有的輸出 $QRcode->png($frame,false,$level,$size,$margin); $imageString = base64_encode(ob_get_contents()); ob_end_clean(); //清除緩沖區的內容,並將緩沖區關閉,但不會輸出內容 return "data:image/jpg;base64,".$imageString; }
解釋
qrcode();常規生成二維碼 qrcode64以base64輸出圖片流使用時 $img = qrcode64('hello');echo "<img src='{$img}'>";