先到插件里面安装 免费的 《二维码生成》
然后就是直接调用了
/** * 获取我的分享二维码 * * @return void */ public function get_my_share_qr() { $qr = new \addons\qrcode\controller\Index; if(empty($this->auth->qr)){ $qr_path = $qr->get_qr(json_encode(['uid'=>$this->auth->id])); Db::name('user')->where('id',$this->auth->id)->update(['qr'=>$qr_path]); $this->auth->qr = $qr_path; } $this->success('qr',$this->auth->qr); }
当然
addons\qrcode\controller\Index.php
在里面加入
public function get_qr($content='内容') { $config = get_addon_config('qrcode'); $params = $this->request->get(); $params = array_intersect_key($params, array_flip(['text', 'size', 'padding', 'errorlevel', 'foreground', 'background', 'logo', 'logosize', 'logopath', 'label', 'labelfontsize', 'labelalignment'])); $params['text'] = $this->request->get('text', $content, 'trim'); $params['label'] = $this->request->get('label', $config['label'], 'trim'); $qrCode = \addons\qrcode\library\Service::qrcode($params); $mimetype = $config['format'] == 'png' ? 'image/png' : 'image/svg+xml'; $response = Response::create()->header("Content-Type", $mimetype); // 直接显示二维码 // header('Content-Type: ' . $qrCode->getContentType()); // $response->content($qrCode->writeString()); // 写入到文件 if ($config['writefile']) { $qrcodePath = ROOT_PATH . 'public/uploads/qrcode/user/'; $file_path = '/uploads/qrcode/user/'; if (!is_dir($qrcodePath)) { @mkdir($qrcodePath); } if (is_really_writable($qrcodePath)) { $name = md5(implode('', $params)) . '.' . $config['format']; $filePath = $qrcodePath . $name; $qrCode->writeFile($filePath); }else{ $this->error('没有写入权限'.$file_path); } return $file_path.$name; } }