1. 程式人生 > >Thinkphp3.2版本結合phpqrcode生成二維碼並提供下載

Thinkphp3.2版本結合phpqrcode生成二維碼並提供下載

com value inf PQ png 容錯 AI mini eve

說明:這篇文章主要為大家詳細介紹了Thinkphp3.2.3整合phpqrcode生成帶二維碼的實現方法並提供圖像下載,感興趣的小夥伴們可以參考一下

緣由:Thinkphp中沒有二維碼相關的庫,因此我們可以通過整合phpqrcode來完成生成二維碼的功能。

一、phpqrcode下載地址:http://phpqrcode.sourceforge.net/

放置位置:Thinkphp/Vendor/目錄下,如下圖

技術分享圖片

二、寫代碼

HTML代碼

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=2.0, user-scalable=no, width=device-width" name="viewport">
	<title>生成二維碼_sl95-申霖個人博客</title>
	<style>
		body{background: #b5b5b5;}
		.box{width: 350px;height: 300px;border: solid 2px #b5b5b5; border-radius: 5px; margin: 0 auto;margin-top: 50px;background-color: #999;}
		h3{color: #FFF;font-size: 24px;text-align: center;}
		span{color: #FFF;display: block;width:300px;padding-left: 45px;line-height: 30px;}
		.input-url{display:block;width: 250px;height: 30px;margin:0 auto;border-radius: 5px;border: solid 1px #FFF;padding-left: 10px;}
		.input-code{width: 50px;height: 30px;float:left;margin-left:70px;border-radius: 5px;border: solid 1px #FFF;padding-left: 10px;}
		img{width: 80px;height: 30px;float: left;margin-left: 20px;}
		.input-submit{display:block;width: 265px;height: 40px;margin:0 auto;margin-top:20px;background-color: #3385ff;color: #FFF; border-radius: 5px;border: solid 1px #b5b5b5;}		
		.footer{width:300px;height:30px;margin:0 auto;text-align: center;padding: 3px 3px;color: #ddd;}
		.error{display: none;}
		.img{display: none;width: 350px;height: 300px;border: solid 2px #b5b5b5; border-radius: 5px; margin: 0 auto;margin-top: 50px;background-color: #999;}
		.img img{
			width: 350px;
			height: 350px;
			margin: 0 auto;
			background-color: #999;
		}
	</style>
</head>
<body>
	<div class="box">
		<h3>生成二維碼</h3>
		<span>網  址</span>
		<input type="text" name="url" required="" placeholder="請輸入網址,例:www.baidu.com" class="input-url">		
		<input type="submit" value="提交" class="input-submit">
		<div class="footer">
			<p>? 申霖</p>
		</div>		
		<div class="error">
			<button class="input-submit error-pic">生成失敗</button>
		</div>
	</div>
	<div class="img">
		<img src="" alt="二維碼圖片" class="ewm-pic">
		<p align="center">手機長按下載,電腦右鍵另存為下載</p>
	</div>
	<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
	<script>
		$(".input-submit").click(function(){
			var url = $(".input-url").val();
			if(!url){
				alert("請先填寫網址!");
			}else{
				$.ajax({					
					url:"http://www.sl95.cn/index.php/Test/Ewm/index.html",
					type:"get",
					dataType:"json",
					contentType:"application/json",
					async:true,
					data:{"url":url},
					success:function(data){
						if(data.status==1){
							$(".box").css("display","none");
							$(".img").css("display","block");
							var picurl = "http://" + window.location.host + "/" + data.url;
							$(".ewm-pic").attr("src",picurl);
						}else{
							$(".error").css("display","block");						
						}
					}
				});
			}		
		});
	</script>
</body>
</html>

PHP代碼

<?php
/*
Time:2018-01-08 23:10
UpdateTime:2018-05-09 User:shenlin Purpose:二維碼 */ namespace Test\Controller; use Think\Controller; class EwmController extends Controller { public function index(){ if(IS_AJAX){ //接收url $url = I(‘get.url‘); $UrlInfo = $this->qrcode($url); if($UrlInfo){ $data[‘info‘] = "生成成功"; $data[‘status‘] = 1; $data[‘url‘] = $UrlInfo; }else{ $data[‘info‘] = "生成失敗!"; $data[‘status‘] = 0; } $this->ajaxReturn($data); }else{ $this->display(); } } public function qrcode($url){ ob_clean(); Vendor(‘phpqrcode.phpqrcode‘); $level = 3; $size = 4; $errorCorrectionLevel = intval($level) ;//容錯級別 $matrixPointSize = intval($size);//生成圖片大小 //保存位置 $path = "Public/qrcode/"; // 生成的文件名 $fileName = $path.date(‘YmdHis‘,time()).‘.png‘; //生成二維碼圖片 $object = new \QRcode(); $object->png($url, $fileName, $errorCorrectionLevel, $matrixPointSize, 2); return $fileName; } }

  三、完美結束,如果需要生成帶logo的二維碼,請參照文章“Thinkphp3.2版本結合phpqrcode生成帶logo的二維碼並提供下載”

Thinkphp3.2版本結合phpqrcode生成二維碼並提供下載