You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP生成带员工照片的vCard二维码时换行符失效如何解决

问题原因

  • 代码混用了PHP编辑时的硬换行(写代码时敲击的回车)和\n转义字符,实际输出时硬换行、多余缩进会被带入$text变量,打乱vCard的格式规范
  • vCard 2.1 标准要求换行符为\r\n(CRLF),而非单纯的\n,绝大多数二维码识别工具仅支持标准CRLF换行
  • 照片字段拼接时额外添加了多余空格,和错误换行混写后导致识别失败

解决方案

统一使用显式的\r\n做换行拼接,删除所有代码字符串内的硬换行,修正照片字段的格式,修正后完整可运行代码如下:

if (file_exists($employeeBCpicture)) {
    $imagedata = file_get_contents($employeeBCpicture);
    $photo64 = base64_encode($imagedata);
} else {
    $photo64 = '';
}
// 引入QR码生成库
include_once('/usr/share/phpqrcode/qrlib.php');

// 统一用\r\n拼接vCard内容,避免硬换行干扰
$text = 'BEGIN:VCARD' . "\r\n";
$text .= 'VERSION:2.1' . "\r\n"; // 已修正原代码VERSION末尾多余的小数点,符合vCard规范
$text .= 'N:' . ucwords($rowe['lname']) . ';' . ucwords($rowe['fname']) . ';;;' . "\r\n";
$text .= 'FN:' . $fullname . "\r\n";
$text .= 'TEL;WORK:+'.$phoneline . "\r\n";
$text .= 'TEL;CELL:+'.$mobile . "\r\n";
$text .= 'EMAIL;WORK:'.$email . "\r\n";
$text .= 'URL;WORK:'.$website . "\r\n";
$text .= 'ORG:'.ucwords($rowc['companyname']) . "\r\n";
$text .= 'TITLE:'.$title . "\r\n";
$text .= 'ADR;WORK:;;'.$address.';;' . "\r\n";

// 修正照片字段格式,3个标准换行,无多余空格
if ($photo64 != '') {
    $text .= 'PHOTO;TYPE=PNG;ENCODING=BASE64:' . "\r\n";
    $text .= $photo64 . "\r\n\r\n\r\n";
}

$text .= 'END:VCARD';

// 生成二维码部分逻辑不变
$rfilename = '../../../signatures/'.uniqid().".png";
$e_correction = 'L';
$pixel_size = 2;
$frame_size = 2;
QRcode::png($text, $rfilename, $e_correction, $pixel_size, $frame_size);

额外优化建议

如果带照片的二维码识别率较低,可以把错误纠正等级从L调整为M或H,带照片的vCard内容更长,更高的纠错等级可以大幅提升识别成功率。

内容的提问来源于stack exchange,提问作者Mohamed Ayman Ragab

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 19:48:02