当前位置:网站首页>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;
}边栏推荐
- C generic
- Sixtool- source code of multi-functional and all in one generation hanging assistant
- Analysis report on the development trend and operation status of China's environmental monitoring instrument industry from 2022 to 2028
- Laravel framework Alipay payment fails to receive asynchronous callback request [original]
- Read / write lock for thread synchronization
- I like you!
- Laravel access error could not be opened
- Redis cache message queue
- redis集群的方式
- 小程序中实现视频通话及互动直播功能
猜你喜欢

Minecraft 1.16.5 biochemical 8 module 1.9 version 1.18 version synchronization

Nailing open platform - applet development practice (nailing applet client)

PIP batch complete uninstall package
![[Qunhui] this suite requires you to start PgSQL adapter service](/img/fb/1aea7eb833afc1a24531b612a98400.jpg)
[Qunhui] this suite requires you to start PgSQL adapter service
![PHP design function getmaxstr to find the longest symmetric string in a string - [original]](/img/45/d8dae9e605a2f411683db7a2d40d0b.jpg)
PHP design function getmaxstr to find the longest symmetric string in a string - [original]

Knowledge of SQL - database design, backup and restore
![[QT] resource file import](/img/0f/6eff57a09edda284b833947dab16af.png)
[QT] resource file import
![[geek challenge 2019] rce me](/img/92/978c54fb42391198300c76ae92893d.jpg)
[geek challenge 2019] rce me

Zeromq from getting started to mastering

Read / write lock for thread synchronization
随机推荐
PHP small factory moves bricks for three years - interview series - my programming life
What if the serial port fails to open when the SCM uses stc-isp to download software?
Create alicloud test instances
Simple personal summary of tp6 multi application deployment -- Part I [original]
Mutex of thread synchronization (mutex)
[QT] resource file import
Nailing open platform - applet development practice (nailing applet client)
Tp6 is easy to tread [original]
6、 Project practice --- identifying cats and dogs
Clickhouse stand alone installation
Install cenos in the virtual machine
Oracle 數據泵導錶
SQL related knowledge - constraints
Syntax error of go language generic in IDE
VHDL design
Unity mobile game performance optimization spectrum CPU time-consuming optimization divided by engine modules
asp. Net web page, ASP connects to the database, and uses asp:panel and asp:dropdownlist controls
2020-12-18
The open software of win10 system is too small. How to make it larger (effective through personal test)
[Qunhui] command line acme SH automatically apply for domain name certificate