二維碼(QR Code)以黑白幾何圖案記錄資訊,具備容錯機制(遮擋部分仍可識讀),廣泛用於網址、名片、支付、防偽等場景。
include 'phpqrcode.php';
$value = 'https://www.example.com';
$errorCorrectionLevel = 'L'; // L/M/Q/H 容錯率遞增
$matrixPointSize = 4; // 每個模組像素大小
QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize); // 直接輸出圖片流
QRcode::png($data, $filename, $level, $size, $margin)| 參數 | 說明 |
|---|---|
| $data | 編碼內容,UTF-8 中文最多 984 字元 |
| $filename | 儲存檔名,false 直接輸出瀏覽器 |
| $level | 容錯等級:L(7%)、M(15%)、Q(25%)、H(30%) |
| $size | 模組像素(建議 3-10) |
| $margin | 外邊距模組數(預設 4) |
支援格式:png、jpg、svg、text(對應 QRcode::svg() 等)。
https://... 或 {URLTO:...}mailto:user@domain.comtel:+8613800000000MECARD:N:姓名;TEL:電話;EMAIL:信箱;URL:網址;;smsto:號碼:內容geo:緯度,經度,海拔WIFI:T:WPA;S:SSID;P:密碼;;include 'phpqrcode.php';
$value = $_GET['url'] ?? 'https://default.com';
QRcode::png($value, 'qrcode.png', 'L', 6, 2);
$logo = 'logo.png';
$qr = imagecreatefromstring(file_get_contents('qrcode.png'));
$logo = imagecreatefromstring(file_get_contents($logo));
$qr_w = imagesx($qr); $qr_h = imagesy($qr);
$logo_w = imagesx($logo); $logo_h = imagesy($logo);
$logo_qr_w = $qr_w / 5;
$scale = $logo_w / $logo_qr_w;
$logo_qr_h = $logo_h / $scale;
$from_x = ($qr_w - $logo_qr_w) / 2;
$from_y = ($qr_h - $logo_qr_h) / 2;
imagecopyresampled($qr, $logo, $from_x, $from_y, 0, 0, $logo_qr_w, $logo_qr_h, $logo_w, $logo_h);
header('Content-Type: image/png');
imagepng($qr);
imagedestroy($qr);
關鍵點:Logo 佔二維碼 1/5 面積,置中合成,容錯等級建議 H 以確保 Logo 遮擋下仍可掃描。