1--下载类库
composer require aferrandini/phpqrcode -vvv
2-common 的方法
//$text 文本的内容
//$logo logo图片
function code($text,$logo){
//二维码图片保存路径
$pathname = APP_PATH . '/../Public/upload/';
if(!is_dir($pathname)) { //若目录不存在则创建之
mkdir($pathname);
}
vendor("phpqrcode.phpqrcode");
//二维码图片保存路径(若不生成文件则设置为false)
$filename = $pathname . "/qrcode_" . time() . ".png";
//二维码容错率,默认L
$level = "L";
//二维码图片每个黑点的像素,默认4
$size = '10';
//二维码边框的间距,默认2
$padding = 2;
//保存二维码图片并显示出来,$filename必须传递文件路径
$saveandprint = true;
//生成二维码图片
\PHPQRCode\QRcode::png($text,$filename,$level,$size,$padding,$saveandprint);
//二维码logo
$QR = imagecreatefromstring(file_get_contents($filename));
$logo = imagecreatefromstring(file_get_contents($logo));
$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);
return $filename;
}
3--控制器的 方法
public function test(){
//$savePath 图片存储路径
$savePath = APP_PATH . '/../Public/qrcode/';
//路径
$webPath = '/public/qrcode/';
//$qrData 手机扫描后要跳转的网址
$qrData = 'http://www.baidu.com';
// $qrLevel 默认纠错比例 分为L、M、Q、H四个等级,H代表最高纠错能力
$qrLevel = 'H';
//$qrSize 二维码图大小,1-10可选,数字越大图片尺寸越大
$qrSize = '8';
// $savePrefix 图片名称前缀
$savePrefix = 'NickBai';
if($filename = createQRcode($savePath, $qrData, $qrLevel, $qrSize, $savePrefix)){
$pic = $webPath . $filename;
}
echo "<img src='$pic'>";
}
如整合期间有遇到什么问题 可以加群 858507220 一起讨论哦。