准备工作
PHP版本7.3
QrReader类,下载地址,将解压出来的文件夹放入网站根目录的“vendor”文件夹中
开始
1.图片上传
首先接收上传过来的图片,并将图片存放在本地
PHP代码
/**
* 图片上传(ajax)
* @return \think\Response|void
* @throws \Exception
*/
public function upload()
{
// 获取表单上传文件 例如上传了001.jpg
$file = request()->file('file');
//验证
validate(['image' => [
'fileSize' => 1024 * 5,
'fileExt' => 'jpg,jpeg,png,bmp,gif',
'fileMime' => 'image/jpeg,image/png,image', //这个一定要加上,很重要我认为!
]])->check(['imgFile' => $file]);
// 上传图片到本地服务器
$saveName = \think\facade\Filesystem::putFile('QRcode', $file);
if ($saveName) {
return json_encode(['code' => 200, 'address' => $saveName]);
} else {
return json_encode(['code' => 400, 'msg' => '上传文件失败']);
}
}
html代码如下,这段代码没有仔细斟酌,可能会出错
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>提交</title>
<link rel="stylesheet" href="/static/api/layui/css/layui.css">
<script src="/static/api/js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<style>
.body{
width: 350px;
margin: 100px auto;
}
.layui-form-label {
padding: 13px 15px;
font-size: 25px;
width: 300px;
text-align: left;
}
.layui-input-block {
margin-left: 10px;
}
</style>
<script src="/static/api/layui/layui.js"></script>
</head>
<body>
<div class="body">
<div class="layui-form">
<div class="layui-form-item">
<div class="layui-input-block">
<input type="hidden" name="images" class="image">
<button type="button" class="layui-btn" id="QRcode" name="image">
<i class="layui-icon"></i>上传图片
</button>
</div>
</div>
</div>
</div>
</body>
<script>
layui.use('upload', function(){
var upload = layui.upload;
//执行实例
var uploadInst = upload.render({
elem: '#QRcode' //绑定元素
,url: '/api/QrReader/upload/' //上传接口
,accept:'images' //只允许上传图片
,method: 'post'
,acceptMime: 'image/*'
,done: function(res){
if (res.code == 200){
$('#address').val(res.address);
layer.msg('上传成功', {icon: 1});
}else{
layer.msg(res.msg, {icon: 5});
}
//上传完毕回调
}
,error: function(){
//请求异常回调
}
});
});
</script>
</html>
2.压缩图片
经过测试,QrReader无法解析过大的图片,我们可以利用thinkPHP的图像处理类库将图片变小
代码如下
/*
* $img_path 被压缩的图片的路径
* $thumb_w 压缩的宽
* $save_path 压缩后图片的存储路径
* $is_del 是否删除原文件,默认删除
*/
public function thumb_img($img_path, $thumb_w, $save_path, $is_del = true)
{
$image = \think\Image::open($img_path);
$width = $image->width(); // 返回图片的宽度
if ($width > $thumb_w) {
$width = $width / $thumb_w; //取得图片的长宽比
$height = $image->height();
$thumb_h = ceil($height / $width);
}else{
$thumb_w = $image->width();
$thumb_h = $image->height();
}
//如果文件路径不存在则创建
$save_path_info = pathinfo($save_path);
if (!is_dir($save_path_info['dirname'])) mkdir($save_path_info['dirname'], 0777);
$image->thumb($thumb_w, $thumb_h)->save($save_path);
if ($is_del) @unlink($img_path); //删除源文件
}
经测试,$thumb_w给个800就够了
3.解码
此时可以调用QrReader去解码了,注意,二维码要传入图片的地址,不知道支不支持base64,没仔细研究里面的配置
解码代码如下
//php解码,传入图片的本地路径
public function QrReader($url)
{
//引入需要的类
require '../vendor/php_QrReader-master/lib/QrReader.php';
// 将我们要识别的二维码放进去
$qrcode = new \QrReader($url); //图片路径
//返回识别后的文本
$text = $qrcode->text();
return $text;
}