123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace app\expand\controller;
- use app\common\service\HelperService;
- use think\Log;
- use think\Validate;
- /**
- * 二维码
- * Class Alipay
- * @package app\expand\controller
- */
- class Qrcode extends BaseAuth
- {
- private $_Account = null;
- public function __construct(){
- parent::__construct();
- $this->_Account = $this->getKey($this->_apiCode);
- //验证是否具有访问这个接口的权限
- if(!isset($this->_Account['isQrcode'])){
- HelperService::returnJson(['code'=>400,'msg'=>'isQrcode interface unauthorized access','data'=>[]]);
- }
- }
- /**
- * 产生二维码的案例
- */
- public function QR(){
-
- header( "Content-Type:text/html;charset=UTF-8");
-
- $params = $this->_sysParams;
- $rule = [
- 'content|内容'=>'require',
- 'level|大小的level【LMQH】'=>'require',
- 'size|大小'=>'require|number',
- 'is_down|是否下载'=>'require|number',
- ];
-
- $validate = new Validate($rule);
- if(!$validate->check($params)){
- HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>[]]);
- }
-
- $content = $params['content'];
- $level = $params['level'];
- $size = $params['size'];
- vendor("phpqrcode.phpqrcode");
- $png = false;
- if($params['is_down']){
- $time = time().rand(1000,9999);
- $png = "./qrcode/{$time}.png";
- }
- \QRcode::png($content,$png,$level,min(max($size,4),10),2);
- if(isset($params['note']) && !empty($params['note'])){
- $fontSize = 18;
-
- $noteArr = explode(',',$params['note']);
- //以每行行高22px计算+20的空间
- $total_lineHeight = count($noteArr)*30-0;
- $imageSource = imagecreatefrompng($png);
- $width = imagesx($imageSource);
- $height = imagesy($imageSource);
- //创建画板
- $Palette = @imagecreate($width,$height+$total_lineHeight);
- //填充白色背景
- imagecolorallocate($Palette,255,255,255);
- imagecopyresized($Palette,$imageSource,0,0,0,0,$width,$height,$width,$height);
- //指定字体的颜色
- $color = imagecolorallocate($Palette,0,0,0);
- $i = 0;
- foreach($noteArr as $noteLine){
- $fontBox = imagettfbbox($fontSize, 0, './font/NotoSansHans-DemiLight.ttf', $noteLine);//水平居中实质
- imagettftext($Palette,14,0,ceil(($width - $fontBox[2]) / 2),$height+30*($i++)+15,$color,'./font/NotoSansHans-DemiLight.ttf',$noteLine);
- }
- imagepng($Palette, $png);
- }
- $note = isset($params['note'])?$params['note']:'';
- Log::record("old input:\n". json_encode(HelperService::$_sysParams)."\n note: \n".$note);
- if($params['is_down']) {
- HelperService::downloadFile($png);
- }
-
- exit;
- }
-
-
- }
|