Google Chart API (chart.apis.google.com) 曾是生成二維碼最簡單的方式:**無需安裝庫、一行 URL 搞定**。雖然官方已棄用(可能隨時停止),但理解其參數結構對遷移到本地庫有幫助。本文詳解 vCard 格式構建、API 參數、Logo 嵌入替代方案。
二維碼名片本質是 vCard 文本內容。標準字段:
BEGIN:VCARD
VERSION:3.0
FN:張三 ; 全名
N:張;三;;; ; 姓;名;中間名;前綴;後綴
ORG:公司名稱 ; 組織
TITLE:職位 ; 職銜
TEL;TYPE=WORK,VOICE:+86-10-88888888 ; 工作電話
TEL;TYPE=CELL,VOICE:+86-138-0000-0000 ; 手機
EMAIL;TYPE=WORK:zhangsan@company.com
ADR;TYPE=WORK:;;大街100號;北京;北京;100000;中國
URL:https://company.com
NOTE:備註信息
REV:20240115T103000Z ; 修改時間戳
END:VCARD
關鍵點: 每行以 `\n` 換行,特殊字符需轉義(逗號、分號、換行)。總長度建議 < 2KB,超過需用 POST 請求。
https://chart.googleapis.com/chart?
chs=300x300 // 尺寸:寬x高 (像素),最大 500x500
&cht=qr // 圖表類型:qr = 二維碼
&chl=ENCODED_DATA // 數據內容:必須 URL 編碼
&chld=H|2 // 容錯級別|邊距
&choe=UTF-8 // 輸出編碼 (可選)
| 參數 | 說明 | 示例 |
|---|---|---|
| chs | 尺寸,寬x高 | 150x150, 300x300 |
| cht | 圖表類型 | qr (二維碼) |
| chl | 數據內容 (URL encoded) | BEGIN:VCARD%0AFN%3A%E5%BC%B5%E4%B8%89... |
| chld | 容錯級別|邊距 | L|0, M|1, Q|2, H|2 (嵌 Logo 必用 H) |
| choe | 輸出編碼 | UTF-8, Shift_JIS, ISO-8859-1 |
<?php
function generateGoogleQR(string $data, int $size = 300, string $level = 'H', int $margin = 2): string
{
$params = http_build_query([
'chs' => "{$size}x{$size}",
'cht' => 'qr',
'chl' => $data,
'chld' => $level . '|' . $margin,
'choe' => 'UTF-8',
]);
return "https://chart.googleapis.com/chart?{$params}";
}
// 構建 vCard
function buildVCard(array $info): string
{
$fields = [];
$fields[] = 'BEGIN:VCARD';
$fields[] = 'VERSION:3.0';
$fields[] = 'FN:' . $info['name'];
if (!empty($info['org'])) $fields[] = 'ORG:' . $info['org'];
if (!empty($info['title'])) $fields[] = 'TITLE:' . $info['title'];
if (!empty($info['phone'])) $fields[] = 'TEL;TYPE=CELL,VOICE:' . $info['phone'];
if (!empty($info['email'])) $fields[] = 'EMAIL:' . $info['email'];
if (!empty($info['url'])) $fields[] = 'URL:' . $info['url'];
if (!empty($info['address'])) $fields[] = 'ADR;TYPE=WORK:;;' . $info['address'];
$fields[] = 'REV:' . gmdate('Ymd\THis\Z');
$fields[] = 'END:VCARD';
return implode('\n', $fields);
}
// 使用
$vcard = buildVCard([
'name' => '張三',
'org' => '科技有限公司',
'title' => '高級工程師',
'phone' => '+86-138-0000-0000',
'email' => 'zhangsan@company.com',
'url' => 'https://company.com',
'address' => '北京市朝陽區大街100號',
]);
$qrUrl = generateGoogleQR($vcard, 300, 'H', 2);
echo '<img src="' . $qrUrl . '" alt="QR Code" width="300" height="300">';
Google API **不支持**直接生成帶 Logo 的二維碼。方案:
endroid/qrcode 一步完成。<?php
// 方案:下載 API 圖片再合成 Logo
function generateQRWithLogoViaGoogle(string $data, string $logoPath, string $outputPath): bool
{
$qrUrl = generateGoogleQR($data, 400, 'H', 2);
$qrImage = @imagecreatefromstring(file_get_contents($qrUrl));
if (!$qrImage) return false;
$logo = @imagecreatefromstring(file_get_contents($logoPath));
if (!$logo) return false;
// ... 同前文 imagecopyresampled 合成邏輯 ...
imagepng($qrImage, $outputPath);
imagedestroy($qrImage);
imagedestroy($logo);
return true;
}
Google Chart API **已棄用**,生產環境**強烈不建議**依賴外部服務(單點故障、隱私、限流、隨時下線)。
Google Chart API 適合**快速原型、個人項目、內網工具**。生產環境請**遷移到本地庫**,掌握 vCard 格式構建、容錯級別選擇、Logo 合成邏輯,這些知識在任何二維碼庫中通用。