1.安装包
composer require simplesoftwareio/simple-qrcode
2.直接使用
use SimpleSoftwareIO\QrCode\Facades\QrCode;
use Storage;
.....
/**
* 生成二维码
* @param Request $request [description]
* @return [type] [description]
*/
public function generateQrcode(Request $request)
{
$validator = Validator::make($request->all(), [
'url' => 'nullable|url',
'size' => 'nullable|integer|min:100',
'type' => 'nullable|integer|in:1,2',
]);
if ($validator->fails()) {
$first_error = $validator->errors()->first();
return $this->outPutJson('', 201, $first_error);
}
$size = request('size', 200);
$type = request('type', 1); #1.生成base64,直接在页面使用不保存图片到文件服务器;2.保存图片到文件服务器,返回可访问的网络路径
$url = request('url', url('form/apply'));
$logo = public_path('assets/img/logo_qrcode.png');
switch ($type) {
case 1:
$gen = QrCode::format('png')->size($size)->merge($logo, .3, true)->generate($url);
$data = 'data:image/png;base64,' . base64_encode($gen);
break;
case 2:
$qrcode_name = 'qrcodes/' . date('YmdHis_') . str_random(8) . '.png';
QrCode::format('png')->size($size)->merge($logo, .3, true)->generate($url, Storage::disk('public')->path($qrcode_name));
$data = Storage::disk('public')->url($qrcode_name);
break;
default:
$data = '';
break;
}
return $this->outPutJson($data);
// return '<img src=' . $data . '>';type=1的时候使用图片
}
官方文档:
https://www.simplesoftware.io/docs/simple-qrcode
在页面使用:
{!! QrCode::size(250)->margin(0)->generate(Request::url()); !!}
margin控制二维码留白像素,size控制二维码大小
相关文章:
php生成二维码 https://blog.csdn.net/zhezhebie/article/details/71440888