当前位置:网站首页>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;
}边栏推荐
- Compiling and installing phpredis extension on MAC
- Laravel access error could not be opened
- Zhubo Huangyu: all the precious metals you want to know are here
- Sixtool- source code of multi-functional and all in one generation hanging assistant
- Knowledge about SQL - DML
- Create alicloud test instances
- Ueeditor automatically appends P tags to rich text.br tags always wrap.br tag solutions
- College C language final exam · multiple choice questions · summary notes of mistakes and difficulties
- mysql自帶的性能測試工具mysqlslap執行壓力測試
- 08_ Spingboot integrated redis
猜你喜欢

Detailed explanation of globalkey of flutter

線程同步之讀寫鎖

Force buckle 515 Find the maximum value in each tree row

Ubuntu installs PostgreSQL and uses omnidb to view
![Notes on enterprise wechat development [original]](/img/66/cd83f4f86b7c42921db45f07957c15.jpg)
Notes on enterprise wechat development [original]

Performance test comparison between PHP framework jsnpp and thinkphp6

08_ Spingboot integrated redis

NPM installation tutorial
![[Qunhui] Internet access + custom port](/img/7d/c00caeade209a48c8f44a4fecb59d5.jpg)
[Qunhui] Internet access + custom port

2021-02-07
随机推荐
小程序中实现视频通话及互动直播功能
Nailing open platform - applet development practice (nailing applet server side)
Add, delete, modify and query curd in PHP native SQL
SQL related knowledge - DDL
Conditional variables for thread synchronization
Navicat connects the pit of shardingsphere sub table and sub library plug-ins
Understand CGI and fastcgi
Zhubo Huangyu: all the precious metals you want to know are here
6、 Project practice --- identifying cats and dogs
win10 系统打开的软件太小,如何变大(亲测有效)
go语言泛型在IDE中语法报错
[从零开始学习FPGA编程-45]:视野篇 - 集成电路助力数字化时代高质量发展-2-市场预测
SQL related knowledge - DQL
Spark - understand parquet
What should I do if I don't understand the precious metal indicators
2021/11/6-burpsuit packet capturing and web page source code modification
Report on the "fourteenth five year plan" and future development direction of global and Chinese indoor vertical farms from 2022 to 2028
Tencent Interviewer: How did binder get its system services?
Principle and implementation of syn cookie
08_ Spingboot integrated redis