推薦 phpqrcode(基於 libqrencode 的 PHP 移植版),純 PHP 實現、無擴展依賴、支持 QR Code 所有版本與糾錯等級。
# Composer (推薦)
composer require endroid/qrcode
# 或下載 phpqrcode 單文件版:https://sourceforge.net/projects/phpqrcode/
<?php
// 基礎配置
ini_set('display_errors', 'on');
// 溫目錄設置(需可寫)
$PNG_TEMP_DIR = __DIR__ . DIRECTORY_SEPARATOR . 'temp' . DIRECTORY_SEPARATOR;
$PNG_WEB_DIR = 'temp/';
if (!is_dir($PNG_TEMP_DIR)) mkdir($PNG_TEMP_DIR, 0755, true);
require_once 'qrlib.php'; // phpqrcode 入口
// 參數配置
$data = 'https://example.com/product/12345'; // 內容
$ecc = 'H'; // 糾錯等級:L(7%) M(15%) Q(25%) H(30%)
$size = 10; // 模塊大小:1-10(像素/模塊)
$margin = 2; // 靜區邊距:模塊數
// 生成文件名
$filename = $PNG_TEMP_DIR . 'qrcode_' . time() . '.png';
// 生成 QR Code
QRcode::png($data, $filename, $ecc, $size, $margin);
// 輸出到瀏覽器
chmod($filename, 0644);
echo '<img src="' . $PNG_WEB_DIR . basename($filename) . '" alt="QR Code" />';
?>

| 等級 | 恢復能力 | 數據密度 | 適用場景 |
|---|---|---|---|
| L (Low) | ~7% | 最高 | 乾淨環境、大數據量、掃碼距離近 |
| M (Medium) | ~15% | 高 | 默認推薦,平衡密度與容錯 |
| Q (Quartile) | ~25% | 中 | 可能有污損、戶外廣告 |
| H (High) | ~30% | 最低 | 嚴重污損、Logo 嵌入、工業環境 |
size (1-10):每個模塊像素數,最終圖片邊長 = (版本*4+17) * sizemargin (0-10):靜區寬度(模塊數),標準要求 ≥4,掃碼器識別需要庫會根據數據長度和糾錯等級自動選擇 Version 1-40(21×21 到 177×177 模塊)。強制指定版本需修改庫源碼。
<?php
// 直接輸出到瀏覽器
header('Content-Type: image/png');
QRcode::png($data, false, $ecc, $size, $margin); // 第2參數 false 直接輸出
?>
<?php
// 獲取二維矩陣 (0/1)
$matrix = QRcode::getMatrix($data, $ecc);
// $matrix = [[0,1,1,0...], [1,0,0,1...], ...]
// 自定義渲染:SVG、ASCII、HTML Table、Canvas 等
function renderSvg($matrix, $size=10) {
$n = count($matrix);
$wh = $n * $size;
$svg = '<svg xmlns="https://www.w3.org/2000/svg" width="'.$wh.'" height="'.$wh.'">';
$svg .= '<rect width="100%" height="100%" fill="white"/>';
foreach ($matrix as $y => $row) {
foreach ($row as $x => $v) {
if ($v) $svg .= '<rect x="'.($x*$size).'" y="'.($y*$size).'" width="'.$size.'" height="'.$size.'" fill="black"/>';
}
}
$svg .= '</svg>';
return $svg;
}
echo renderSvg($matrix, 5);
?>
<?php
function addLogo($qrPath, $logoPath, $outputPath, $logoWidthPercent = 0.2) {
$qr = imagecreatefrompng($qrPath);
$logo = imagecreatefromstring(file_get_contents($logoPath));
$qrW = imagesx($qr); $qrH = imagesy($qr);
$logoW = imagesx($logo); $logoH = imagesy($logo);
$targetW = $qrW * $logoWidthPercent;
$targetH = $logoH * ($targetW / $logoW);
$dstX = ($qrW - $targetW) / 2;
$dstY = ($qrH - $targetH) / 2;
imagecopyresampled($qr, $logo, $dstX, $dstY, 0, 0, $targetW, $targetH, $logoW, $logoH);
imagepng($qr, $outputPath);
imagedestroy($qr); imagedestroy($logo);
}
?>
<?php
function getCachedQr($data, $ecc='M', $size=5, $margin=4) {
$cacheKey = 'qr_' . md5($data . $ecc . $size . $margin);
$cacheFile = sys_get_temp_dir() . '/' . $cacheKey . '.png';
if (file_exists($cacheFile) && (time() - filemtime($cacheFile) < 86400)) {
return $cacheFile;
}
QRcode::png($data, $cacheFile, $ecc, $size, $margin);
return $cacheFile;
}
?>
<?php
// 文件鎖防止併發生成同一文件
$lockFile = $cacheFile . '.lock';
$fp = fopen($lockFile, 'w');
if (flock($fp, LOCK_EX)) {
if (!file_exists($cacheFile)) QRcode::png(...);
flock($fp, LOCK_UN);
}
fclose($fp);
?>
超長數據(>2KB)生成大版本 QR 會消耗大量內存,建議:
ini_set('memory_limit', '256M')endroid/qrcode 庫(更現代、內存效率更高)<?php
// composer require endroid/qrcode
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;
$qrCode = new QrCode('https://example.com');
$qrCode->setSize(300);
$qrCode->setMargin(10);
$qrCode->setErrorCorrectionLevel('high');
$writer = new PngWriter();
$result = $writer->write($qrCode);
// 直接輸出
header('Content-Type: ' . $result->getMimeType());
echo $result->getString();
// 或保存
$result->saveToFile(__DIR__ . '/qrcode.png');
?>
phpqrcode 適合輕量級部署,Endroid/QrCode 提供更現代的 API、類型安全、擴展性,推薦新項目使用。