Index.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace addons\qrcode\controller;
  3. use think\addons\Controller;
  4. use think\Response;
  5. /**
  6. * 二维码生成
  7. *
  8. */
  9. class Index extends Controller
  10. {
  11. public function index()
  12. {
  13. return $this->view->fetch();
  14. }
  15. // 生成二维码
  16. public function build()
  17. {
  18. $config = get_addon_config('qrcode');
  19. $params = $this->request->get();
  20. $params = array_intersect_key($params, array_flip(['text', 'size', 'padding', 'errorlevel', 'foreground', 'background', 'logo', 'logosize', 'logopath', 'label', 'labelfontsize', 'labelalignment']));
  21. $params['text'] = $this->request->get('text', $config['text'], 'trim');
  22. $params['label'] = $this->request->get('label', $config['label'], 'trim');
  23. $qrCode = \addons\qrcode\library\Service::qrcode($params);
  24. $mimetype = $config['format'] == 'png' ? 'image/png' : 'image/svg+xml';
  25. $response = Response::create()->header("Content-Type", $mimetype);
  26. // 直接显示二维码
  27. header('Content-Type: ' . $qrCode->getContentType());
  28. $response->content($qrCode->writeString());
  29. // 写入到文件
  30. if ($config['writefile']) {
  31. $qrcodePath = ROOT_PATH . 'public/uploads/qrcode/';
  32. if (!is_dir($qrcodePath)) {
  33. @mkdir($qrcodePath);
  34. }
  35. if (is_really_writable($qrcodePath)) {
  36. $filePath = $qrcodePath . md5(implode('', $params)) . '.' . $config['format'];
  37. $qrCode->writeFile($filePath);
  38. }
  39. }
  40. return $response;
  41. }
  42. }