使用PHP QR Code库生成二维码和带logo的二维码
使用PHP QR Code库生成二维码
function createQRcode($savePath, $qrData = 'PHP QR Code :)', $qrLevel = 'L', $qrSize = 8, $savePrefix = 'qrcode')
{
Loader::import('phpqrcode.phpqrcode');
if (!isset($savePath)) return '';
//设置生成png图片的路径
$PNG_TEMP_DIR = $savePath;
//检测并创建生成文件夹
if (!file_exists($PNG_TEMP_DIR)) {
mkdir($PNG_TEMP_DIR);
}
$filename = $PNG_TEMP_DIR . 'test.png';
$errorCorrectionLevel = 'L';
if (isset($qrLevel) && in_array($qrLevel, ['L', 'M', 'Q', 'H'])) {
$errorCorrectionLevel = $qrLevel;
}
$matrixPointSize = 4;
if (isset($qrSize)) {
$matrixPointSize = min(max((int)$qrSize, 1), 10);
}
$qrCode=new \QrCode();
if (isset($qrData)) {
if (trim($qrData) == '') {
die('data cannot be empty!');
}
//生成文件名 文件路径+图片名字前缀+md5(名称)+.png
$filename = $PNG_TEMP_DIR . $savePrefix . md5($qrData . '|' . $errorCorrectionLevel . '|' . $matrixPointSize) . '.png';
//开始生成
$qrCode::png($qrData, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
} else {
//默认生成
$qrCode::png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2);
}
if (file_exists($PNG_TEMP_DIR . basename($filename)))
return basename($filename);
else
return FALSE;
}
使用PHP QR Code库生成带logo的二维码
function createQRcodeWithLogo($savePath, $qrData = 'PHP QR Code :)', $qrLevel = 'L', $qrSize = 8, $savePrefix = 'qrcode', $logoPath = '')
{
Loader::import('phpqrcode.phpqrcode');
if (!isset($savePath)) return '';
//设置生成png图片的路径
$PNG_TEMP_DIR = $savePath;
//检测并创建生成文件夹
if (!file_exists($PNG_TEMP_DIR)) {
mkdir($PNG_TEMP_DIR);
}
$filename = $PNG_TEMP_DIR . 'test.png';
$errorCorrectionLevel = 'L';
if (isset($qrLevel) && in_array($qrLevel, ['L', 'M', 'Q', 'H'])) {
$errorCorrectionLevel = $qrLevel;
}
$matrixPointSize = 4;
if (isset($qrSize)) {
$matrixPointSize = min(max((int)$qrSize, 1), 10);
}
$qrCode=new \QrCode();
if (isset($qrData)) {
if (trim($qrData) == '') {
die('data cannot be empty!');
}
//生成文件名 文件路径+图片名字前缀+md5(名称)+.png
$filename = $PNG_TEMP_DIR . $savePrefix . md5($qrData . '|' . $errorCorrectionLevel . '|' . $matrixPointSize) . '.png';
//开始生成
$qrCode::png($qrData, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
//添加logo
if ($logoPath) {
$QR = imagecreatefrompng($filename);
$logo = imagecreatefromstring(file_get_contents($logoPath));
$QR_width = imagesx($QR);
$QR_height = imagesy($QR);
$logo_width = imagesx($logo);
$logo_height = imagesy($logo);
$logo_qr_width = $QR_width / 5;
$scale = $logo_width / $logo_qr_width;
$logo_qr_height = $logo_height / $scale;
$from_width = ($QR_width - $logo_qr_width) / 2;
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
imagepng($QR, $filename);
}
} else {
//默认生成
$qrCode::png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2);
}
if (file_exists($PNG_TEMP_DIR . basename($filename)))
return basename($filename);
else
return FALSE;
}
参数说明:
$savePath: 保存二维码图片的路径$qrData: 二维码内容$qrLevel: 错误纠正等级,可选值:'L', 'M', 'Q', 'H'$qrSize: 二维码尺寸,取值范围:1-10$savePrefix: 保存的图片文件名前缀$logoPath: logo图片路径
使用示例:
// 生成默认二维码
createQRcode('path/to/save/', 'www.example.com');
// 生成带logo的二维码
createQRcodeWithLogo('path/to/save/', 'www.example.com', 'L', 8, 'qrcode', 'path/to/logo.png');
原文地址: https://www.cveoy.top/t/topic/o0g1 著作权归作者所有。请勿转载和采集!