laravel生成二維碼

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

相關文章:
php生成二維碼 https://blog.csdn.net/zhezhebie/article/details/71440888

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章