二維碼已成連接線上線下核心入口。PHP 生成二維碼主流方案有二:調用 Google Chart API(零依賴、簡單)與本地 PHP QR Code 庫(可控、高性能、支持帶 Logo)。本文系統對比兩者優劣,提供完整代碼、核心參數詳解、帶 Logo 合成原理、容錯率科普及生產級建議。
<?php
function generateQRfromGoogle($chl, $widthHeight = 150, $EC_level = 'L', $margin = 0) {
$chl = urlencode($chl);
echo '<img src="https://chart.googleapis.com/chart?chs=' . $widthHeight . 'x' . $widthHeight
. '&cht=qr&chld=' . $EC_level . '|' . $margin . '&chl=' . $chl
. '" alt="QR Code" width="' . $widthHeight . '" height="' . $widthHeight . '" />';
}
// 使用
generateQRfromGoogle('https://www.example.com', 200, 'M', 1);
?>
| 參數 | 說明 | 可選值/範例 |
|---|---|---|
| chs | 圖片尺寸 | 200x200 (寬x高,單位像素) |
| cht | 圖表類型 | qr (固定) |
| chl | 編碼內容 | URL、文本,**必須 URLEncode**,最大 4296 字符 |
| chld | 容錯級別|邊距 | L|0, M|1, Q|2, H|2 (邊距 0-4) |
| choe | 字符編碼 | UTF-8 (推薦顯式指定) |
# 方式一:Composer (推薦)
composer require chillerlan/php-qrcode
# 方式二:手動下載 phpqrcode.php (單文件版)
# 官網: https://phpqrcode.sourceforge.net/
require_once 'phpqrcode.php';
QRcode::png(
string $text, // 編碼內容
string $outfile = false, // 輸出文件路徑,false 直接輸出瀏覽器
int $level = QR_ECLEVEL_L, // 容錯級別: L(7%), M(15%), Q(25%), H(30%)
int $size = 3, // 模塊大小 (像素/模塊),圖片尺寸 ≈ size * (版本*4+17)
int $margin = 4, // 靜默區寬度 (模塊數)
bool $saveandprint = false // true: 保存並輸出
);
<?php
require_once 'phpqrcode.php';
// 1. 直接輸出瀏覽器
QRcode::png('https://www.example.com');
// 2. 保存文件
QRcode::png('https://www.example.com', 'qrcode.png', QR_ECLEVEL_M, 6, 2);
// 3. 獲取二進制數據(用於合成、上傳雲存儲)
ob_start();
QRcode::png('Data', false, QR_ECLEVEL_L, 4);
$pngData = ob_get_clean();
// $pngData 可直接存入 DB BLOB、上傳 S3、返回 API 響應
?>
<?php
require_once 'phpqrcode.php';
function generateQRWithLogo($data, $logoPath, $outputPath, $level = 'H', $size = 6, $margin = 2) {
// 1. 生成基礎二維碼(高容錯 H 級)
$tempQR = 'temp_qr_' . uniqid() . '.png';
QRcode::png($data, $tempQR, constant('QR_ECLEVEL_' . $level), $size, $margin);
// 2. 載入圖片資源
$qr = imagecreatefromstring(file_get_contents($tempQR));
$logo = imagecreatefromstring(file_get_contents($logoPath));
if (!$qr || !$logo) {
@unlink($tempQR);
throw new Exception('圖片載入失敗');
}
// 3. 計算 Logo 目標尺寸(二維碼寬度的 1/5)
$qrW = imagesx($qr);
$qrH = imagesy($qr);
$logoW = imagesx($logo);
$logoH = imagesy($logo);
$targetW = $qrW / 5;
$scale = $logoW / $targetW;
$targetH = $logoH / $scale;
// 4. 居中坐標
$dstX = ($qrW - $targetW) / 2;
$dstY = ($qrH - $targetH) / 2;
// 5. 白底圓角矩形背景(可選,提升識別率)
$bgColor = imagecolorallocate($qr, 255, 255, 255);
$radius = 10;
// 簡易畫圓角矩形...
// 6. 重採樣複製 Logo 到二維碼中心
imagecopyresampled($qr, $logo, $dstX, $dstY, 0, 0, $targetW, $targetH, $logoW, $logoH);
// 7. 輸出最終圖片
imagepng($qr, $outputPath);
// 8. 清理資源
imagedestroy($qr);
imagedestroy($logo);
@unlink($tempQR);
return $outputPath;
}
// 使用
generateQRWithLogo(
'https://www.example.com/product/12345',
'logo.png', // 需為正方形、背景透明/白色的 PNG
'final_qrcode.png',
'H', // 必須用 H 級容錯
8, // 較大 size 確保清晰度
2
);
echo '<img src="final_qrcode.png" alt="QR with Logo">';
?>
| 級別 | 恢復能力 | 適用場景 | 數據密度 |
|---|---|---|---|
| L (7%) | 低 | 乾淨環境、大尺寸打印、無 Logo | 最高 |
| M (15%) | 中 | 通用場景、標準打印 | 高 |
| Q (25%) | 較高 | 可能髒污/折疊、帶小 Logo | 中 |
| H (30%) | 最高 | **帶 Logo、戶外廣告、金屬蝕刻、低質打印** | 低 |
建議: 帶 Logo **強制使用 H 級**;普通場景 M 級平衡體積與魯棒性。
md5($data.$level.$size.$logo) 作為 Key。QRcode::svg() 矢量輸出,無限縮放不失真。Google API 適合原型、內網工具、非敏感、低頻場景;PHP QR Code (chillerlan/php-qrcode) 是生產環境標準配置:可控、高性能、支持 Logo/SVG/多種輸出。掌握 容錯率原理、Logo 合成算法、緩存驗證策略,即可構建穩健、專業的二維碼生成服務。