当前位置:网站首页>Thinkphp6 parsing QR code
Thinkphp6 parsing QR code
2022-06-26 04:26:00 【Shrimp stuff】
preparation
PHP edition 7.3
QrReader class , Download address , Put the extracted folder in the root directory of the website “vendor” In the folder
Start
1. Image upload
First, receive the uploaded pictures , And store the pictures locally
PHP Code
/**
* Image upload (ajax)
* @return \think\Response|void
* @throws \Exception
*/
public function upload()
{
// Gets the form upload file For example, I uploaded 001.jpg
$file = request()->file('file');
// verification
validate(['image' => [
'fileSize' => 1024 * 5,
'fileExt' => 'jpg,jpeg,png,bmp,gif',
'fileMime' => 'image/jpeg,image/png,image', // This must be added with , Very important I think !
]])->check(['imgFile' => $file]);
// Upload the picture to the local server
$saveName = \think\facade\Filesystem::putFile('QRcode', $file);
if ($saveName) {
return json_encode(['code' => 200, 'address' => $saveName]);
} else {
return json_encode(['code' => 400, 'msg' => ' Failed to upload file ']);
}
}html The code is as follows , This code has not been carefully considered , There may be errors
<!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> Submit </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> To upload pictures
</button>
</div>
</div>
</div>
</div>
</body>
<script>
layui.use('upload', function(){
var upload = layui.upload;
// Execution examples
var uploadInst = upload.render({
elem: '#QRcode' // Binding elements
,url: '/api/QrReader/upload/' // Upload interface
,accept:'images' // Only upload pictures
,method: 'post'
,acceptMime: 'image/*'
,done: function(res){
if (res.code == 200){
$('#address').val(res.address);
layer.msg(' Upload successful ', {icon: 1});
}else{
layer.msg(res.msg, {icon: 5});
}
// After uploading, call back
}
,error: function(){
// Request exception callback
}
});
});
</script>
</html>2. The compressed image
After testing ,QrReader Can't parse a picture that is too large , We can use thinkPHP The image processing class library makes the image smaller
The code is as follows
/*
* $img_path The path of the compressed picture
* $thumb_w Compressed width
* $save_path The storage path of the compressed picture
* $is_del Delete the original file or not , Delete by default
*/
public function thumb_img($img_path, $thumb_w, $save_path, $is_del = true)
{
$image = \think\Image::open($img_path);
$width = $image->width(); // Returns the width of the image
if ($width > $thumb_w) {
$width = $width / $thumb_w; // Get the aspect ratio of the picture
$height = $image->height();
$thumb_h = ceil($height / $width);
}else{
$thumb_w = $image->width();
$thumb_h = $image->height();
}
// If the file path does not exist, create
$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); // Delete source file
}After testing ,$thumb_w Give me one 800 That's enough
3. decode
You can call QrReader To decode , Be careful , The address of the picture to be transferred by the QR code , I don't know if it supports base64, Didn't study the configuration carefully
The decoding code is as follows
//php decode , The local path of the incoming picture
public function QrReader($url)
{
// Introduce the required classes
require '../vendor/php_QrReader-master/lib/QrReader.php';
// Put in the two-dimensional code we want to identify
$qrcode = new \QrReader($url); // Picture path
// Return the recognized text
$text = $qrcode->text();
return $text;
}边栏推荐
- Composer version rollback version switching
- 小程序中实现视频通话及互动直播功能
- PHP small factory moves bricks for three years - interview series - my programming life
- What preparation should I make before learning SCM?
- Go SQL parsing time Time type
- Construction of art NFT trading platform | NFT mall
- [Qunhui] command line acme SH automatically apply for domain name certificate
- Performance test comparison between PHP framework jsnpp and thinkphp6
- Minecraft 1.16.5 生化8 模组 1.9版本 1.18版本同步
- Oracle 數據泵導錶
猜你喜欢

What preparation should I make before learning SCM?
![[Qunhui] no port access (reverse proxy + intranet penetration)](/img/bc/b1e0c5c382e30fbcea28fbc68c1151.jpg)
[Qunhui] no port access (reverse proxy + intranet penetration)
![[Qunhui] this suite requires you to start PgSQL adapter service](/img/fb/1aea7eb833afc1a24531b612a98400.jpg)
[Qunhui] this suite requires you to start PgSQL adapter service

08_ Spingboot integrated redis

Threejs专用天空盒素材,五种天空盒素材免费下载

六、项目实战---识别猫和狗

The open software of win10 system is too small. How to make it larger (effective through personal test)
![Simple personal summary of tp6 multi application deployment -- Part I [original]](/img/7b/65fab1973423081483dacc9bed9594.jpg)
Simple personal summary of tp6 multi application deployment -- Part I [original]

2021-02-07

Clickhouse stand alone installation
随机推荐
Laravel framework Alipay payment fails to receive asynchronous callback request [original]
go语言泛型在IDE中语法报错
Which is the best embedded visual programming software? (introduction, evaluation and selection of visual programming platform) [scratch, mind+, mixly]
Nightmare
Redis cache data consistency solution analysis
Threejs special sky box materials, five kinds of sky box materials are downloaded for free
Wechat applet is bound to a dynamic array to implement a custom radio box (after clicking the button, disable the button and enable other buttons)
Swagger
解析JSON接口并批量插入到数据库中
Video label forbids downloading. The test is valid. Hide button. The test is valid at three points
Oracle 数据泵导表
使用Jsoup提取接口中的图片
修改Oracle连接数
08_SpingBoot 集成Redis
Report on operation mode and future development trend of China's refining and chemical market 2022-2028
Install cenos in the virtual machine
BSC 及HT 等链的NFT 创造及绑定图片教程
Minecraft 1.16.5 生化8 模组 1.9版本 1.18版本同步
Simple use of redis in laravel
Composer version rollback version switching