thinkphp5 生成二维码
时间: 2023-11-16 16:54:05 浏览: 439
轻量级PHP二维码生成API源码 本地生成二维码图片 无需三方接口
thinkphp5可以使用dh2y/think-qrcode这个扩展包来生成二维码。你可以通过composer安装,具体的安装方法可以参考官方文档。安装完成后,你可以在控制器中使用以下代码生成二维码:
```php
use dh2y\thinkqrcode\QRcode;
public function createQrcode(){
$code = new QRcode();
$register_url = 'https://baidu.com';
$code = $code->png($register_url)
->logo(ROOT_PATH.'/public/static/image/qrcode.png') //生成logo二维码
->getPath(); //获取二维码生成的地址:二维码路径默认在项目下 uploads/qrcode/xxx.png
echo "<img src='$code'>";
}
```
另外,你也可以将QRcode类文件放在框架extend目录下的PhpQrcode文件夹中,然后在控制器中使用以下代码生成二维码:
```php
namespace app\index\controller;
use think\Controller;
use think\Loader;
Loader::import('PhpQrcode.phpqrcode',EXTEND_PATH,'.php');
class Index extends Controller {
//通过链接生成二维码
public function code($url = "http://www.baidu.com") {
$qrcode = new \QRcode();
$value = $url; //二维码内容
$errorCorrectionLevel = 'H'; //容错级别
$matrixPointSize = 6; //生成图片大小
ob_start();
$qrcode::png($value,false , $errorCorrectionLevel, $matrixPointSize, 2);
$imageString = base64_encode(ob_get_contents());
ob_end_clean();
$data = array(
'code'=>200,
'data'=>$imageString
);
return json($data);
}
}
```
以上是生成二维码的两种不同方法,你可以选择适合你的需求的方法来使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [thinkPHP5 - 二维码的生成](https://blog.csdn.net/qq_41408081/article/details/117559893)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [thinkphp5框架生成二维码](https://blog.csdn.net/weixin_30776545/article/details/95507392)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文