使用phpqrcode生成带logo的二维码
步骤【thinkPHP5】
- 下载phpqrcode类,地址:https://sourceforge.net/projects/phpqrcode/
- 下载完成后在vender下新建一个名为phpqrcode的文件夹,将phpqrcode.php放置该文件夹
- 在common.php里面写方法调用该类
生成二维码的方法:
function generateQRCode($url='')
{
vendor('phpqrcode.phpqrcode');
$value = $url;
$errorCorrectionLevel = 'H';
$matrixPointSize = 6;
$id = Session::get('u_id');
$filename = 'C:/wamp64/www/Web/mw_agency_system/public/upload/code/'. 'miwhd'. $id .'.png';
QRcode::png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2);
$logo = 'C:/wamp64/www/Web/mw_agency_system/public/upload/code/logo.png';
$QR = $filename;
if (file_exists($logo)) {
$QR = imagecreatefromstring(file_get_contents($QR));
$logo = imagecreatefromstring(file_get_contents($logo));
if (imageistruecolor($logo)) imagetruecolortopalette($logo, false, 65535);
$QR_width = imagesx($QR);
$QR_height = imagesy($QR);
$logo_width = imagesx($logo);
$logo_height = imagesy($logo);
$logo_qr_width = $QR_width / 4;
$scale = $logo_width/$logo_qr_width;
$logo_qr_height = $logo_height/$scale;
$from_width = ($QR_width - $logo_qr_width) / 2;
$res = imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);
}
imagepng($QR, $filename);
imagedestroy($QR);
$pos =strripos($filename, '/');
$name = substr($filename, $pos+1);
return $name;
}
调用方法
$filename = generateQRCode('对应的URL【可带参数】');
$this->assign('filename', $filename);
输出到页面
<img src="__UP__{$filename}" alt=" ">