Qrcode.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace app\expand\controller;
  3. use app\common\service\HelperService;
  4. use think\Log;
  5. use think\Validate;
  6. /**
  7. * 二维码
  8. * Class Alipay
  9. * @package app\expand\controller
  10. */
  11. class Qrcode extends BaseAuth
  12. {
  13. private $_Account = null;
  14. public function __construct(){
  15. parent::__construct();
  16. $this->_Account = $this->getKey($this->_apiCode);
  17. //验证是否具有访问这个接口的权限
  18. if(!isset($this->_Account['isQrcode'])){
  19. HelperService::returnJson(['code'=>400,'msg'=>'isQrcode interface unauthorized access','data'=>[]]);
  20. }
  21. }
  22. /**
  23. * 产生二维码的案例
  24. */
  25. public function QR(){
  26. header( "Content-Type:text/html;charset=UTF-8");
  27. $params = $this->_sysParams;
  28. $rule = [
  29. 'content|内容'=>'require',
  30. 'level|大小的level【LMQH】'=>'require',
  31. 'size|大小'=>'require|number',
  32. 'is_down|是否下载'=>'require|number',
  33. ];
  34. $validate = new Validate($rule);
  35. if(!$validate->check($params)){
  36. HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>[]]);
  37. }
  38. $content = $params['content'];
  39. $level = $params['level'];
  40. $size = $params['size'];
  41. vendor("phpqrcode.phpqrcode");
  42. $png = false;
  43. if($params['is_down']){
  44. $time = time().rand(1000,9999);
  45. $png = "./qrcode/{$time}.png";
  46. }
  47. \QRcode::png($content,$png,$level,min(max($size,4),10),2);
  48. if(isset($params['note']) && !empty($params['note'])){
  49. $fontSize = 18;
  50. $noteArr = explode(',',$params['note']);
  51. //以每行行高22px计算+20的空间
  52. $total_lineHeight = count($noteArr)*30-0;
  53. $imageSource = imagecreatefrompng($png);
  54. $width = imagesx($imageSource);
  55. $height = imagesy($imageSource);
  56. //创建画板
  57. $Palette = @imagecreate($width,$height+$total_lineHeight);
  58. //填充白色背景
  59. imagecolorallocate($Palette,255,255,255);
  60. imagecopyresized($Palette,$imageSource,0,0,0,0,$width,$height,$width,$height);
  61. //指定字体的颜色
  62. $color = imagecolorallocate($Palette,0,0,0);
  63. $i = 0;
  64. foreach($noteArr as $noteLine){
  65. $fontBox = imagettfbbox($fontSize, 0, './font/NotoSansHans-DemiLight.ttf', $noteLine);//水平居中实质
  66. imagettftext($Palette,14,0,ceil(($width - $fontBox[2]) / 2),$height+30*($i++)+15,$color,'./font/NotoSansHans-DemiLight.ttf',$noteLine);
  67. }
  68. imagepng($Palette, $png);
  69. }
  70. $note = isset($params['note'])?$params['note']:'';
  71. Log::record("old input:\n". json_encode(HelperService::$_sysParams)."\n note: \n".$note);
  72. if($params['is_down']) {
  73. HelperService::downloadFile($png);
  74. }
  75. exit;
  76. }
  77. }