1.下载endroid扩展
#composer require endroid/qrcode
2.实现代码(生成二维码图片)
public function index(){
$qrCode = new QrCode('http://baidu.com/');
$qrCode->setSize(150);
// 边框宽度
$qrCode->setMargin(2);
// 图片格式
$qrCode->setWriterByName('png');
// 字符编码
$qrCode->setEncoding('UTF-8');
// 颜色设置,前景色,背景色(默认黑白)
$qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
$qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);
header('Content-Type: ' . $qrCode->getContentType());
// 容错等级,分为L、M、Q、H四级
$qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH());
$sha1 = sha1('yhq6584');
$path = substr($sha1, 0, 2) .'/'. substr($sha1, 2, 3) . '/';
$qrcode_dir = ROOT_PATH.'public/uploads/' . $path;
if (!file_exists($qrcode_dir)) mkdir($qrcode_dir, 0777, true);
$code_path = $qrcode_dir .$sha1 . '.png';
$code_logo = ROOT_PATH.'public\uploads\head\20200317\c15342e43d82a8c83604d84195c1d068.jpg';
$qrCode->setLabel('手机扫一扫', 12);
$qrCode->setLogoPath($code_logo);
$qrCode->setLogoWidth(64);
$qrCode->writeFile($code_path);
// return $this->redirect('Login/indexYhq');
// echo date('Y-m-d H:i:s',2437112213);
echo $path.$sha1 . '.png';
}