thinkphp 如何使用phpqrcode生成二维码

1-下载类库

composer require aferrandini/phpqrcode -vvv

2-common的方法

//$text  文本的内容//$logo  logo图片function code($text,$logo){    //二维码图片保存路径    $pathname = APP_PATH . '/../Public/upload/';    if(!is_dir($pathname)) { //若目录不存在则创建之        mkdir($pathname);    }    vendor("phpqrcode.phpqrcode"); //二维码图片保存路径(若不生成文件则设置为false)    $filename = $pathname . "/qrcode_" . time() . ".png";//二维码容错率,默认L    $level = "L";//二维码图片每个黑点的像素,默认4    $size = '10';//二维码边框的间距,默认2    $padding = 2;//保存二维码图片并显示出来,$filename必须传递文件路径    $saveandprint = true; //生成二维码图片    \PHPQRCode\QRcode::png($text,$filename,$level,$size,$padding,$saveandprint); //二维码logo    $QR = imagecreatefromstring(file_get_contents($filename));    $logo = imagecreatefromstring(file_get_contents($logo));    $QR_width = imagesx($QR);    $QR_height = imagesy($QR);    $logo_width = imagesx($logo);    $logo_height = imagesy($logo);    $logo_qr_width = $QR_width / 5;    $scale = $logo_width / $logo_qr_width;    $logo_qr_height = $logo_height / $scale;    $from_width = ($QR_width - $logo_qr_width) / 2;    imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);    imagepng($QR,$filename);return $filename;}

3--控制器

    public function code(){        $logo=request()->file("file");        $pathImg="";        //移动文件到框架应用更目录的public/uploads/        $info = $logo->move(ROOT_PATH . 'public' . DS . 'upload' . DS . 'admin' . DS . date('Y') . DS . date('m-d'),md5(microtime(true)));        if ($info) {            $pathImg = "./public/upload/admin/" . date('Y') . '/' . date('m-d') . '/' . $info->getFilename();        } else {            //错误提示用户            return $this->error($logo->getError());        }        $logo=$pathImg;        //二维码URL参数        $text = "http://www.baidu.com";         $filename=code($text,$logo);         echo "<img src='$filename'>";           $image = \think\Image::open('./public/images/q.png');        // 给原图居中添加透明度为100的水印并保存到$a.png        $a="./public/upload/11.jpg";        $image->water('./public/upload/qrcode_1536730572.png',\think    \Image::WATER_CENTER,100)->save($a);     }

4-html 代码

<!doctype html><html lang="en"><head><meta charset="UTF-8" /><title>Document</title></head><body><form action="http://127.0.0.1:99/admin/admin/code"  method="post"  enctype="multipart/form-data"><div class="row cl"><label class="form-label col-xs-4 col-sm-3">照片:</label><div class="col-xs-8 col-sm-9"><input type="file" id="file" name="file" ></div></div><div class="row cl"><div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3"><input type="submit" value="提交"></div></div></form>    </body></html>

您可能还会对下面的文章感兴趣: