


Teach you to use PHP to implement the face recognition and facial login function of the WeChat applet
Teach you how to use PHP to implement the facial recognition login function of the WeChat applet
First of all, we must confirm that our personal information photos have been uploaded to our Baidu Cloud face library;
Then we write the face-scanning login interface in the background to log in. We need to store the photos obtained by taking photos to the server;
public function login(){ // 上传文件路径 $dir = "./Uploads/temp/"; if(!file_exists($dir)){ mkdir($dir,0777,true); } $upload = new \Think\Upload(); $upload->maxSize = 2048000 ;// 设置附件上传大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 $upload->savepath = ''; $upload->autoSub = false; $upload->rootPath = $dir; // 设置附件上传根目录 // 上传单个文件 $info = $upload->uploadOne($_FILES['file']); if(!$info) {// 上传错误提示错误信息 echo json_encode(array('error'=>true,'msg'=>$upload->getError()),JSON_UNESCAPED_UNICODE); }else{// 上传成功 获取上传文件信息 $file = $dir . $info['savepath'].$info['savename']; $image = base64_encode(file_get_contents($file)); $client = $this->init_face(); $options['liveness_control'] = 'NORMAL'; $options['max_user_num'] = '1'; $ret = $client->search($image,'BASE64','student',$options); // echo json_encode($ret,JSON_UNESCAPED_UNICODE); // exit; if($ret['error_code']==0){ $user = $ret['result']['user_list'][0]; $no = $user['user_id']; $score = $user['score']; if($score>=95){ $data = M('student')->where("no = '{$no}'")->find(); $data['score'] = $score; // $data['name'] = json_decode($data['name'],true); // $data['sex'] = json_decode($data['sex'],true); echo '识别成功' . json_encode($data,JSON_UNESCAPED_UNICODE); }else{ echo '识别失败' . $data['score']; } } } }
Then we design the front desk;
<camera device-position="{{device?'back':'front'}}" flash="off" binderror="error" style="width: 100%; height: 300px;"></camera> <view class="weui-cells__title" >开关</view> <view class="weui-cells weui-cells_after-title"> <view class="weui-cell weui-cell_switch"> <view class="weui-cell__bd">切换摄像头</view> <view class="weui-cell__ft" > <switch bindtap="devicePosition" /> </view> </view> </view> <button type="primary" bindtap="takePhoto">刷脸登录</button>
We can also control the front and rear lenses of the camera;
devicePosition() { this.setData({ device: !this.data.device, }) console.log("当前相机摄像头为:", this.data.device ? "后置" : "前置"); camera() { let { ctx, type, startRecord } = this.data; }, data: { src: null, },
Call the interface in js;
takePhoto() { const ctx = wx.createCameraContext() ctx.takePhoto({ quality: 'high', success: (res) => { this.setData({ src: res.tempImagePath }) console.log(res) wx.uploadFile({ url: '', //仅为示例,非真实的接口地址 filePath: this.data.src, name: 'file', formData: { }, success: function (res) { // var data = res.data // var json = JSON.parse(data) console.log(res) wx.showModal({ title: "提示", content: res.data, showCancel: false, confirmText: "确定" }) } }) } }) },
Swipe your face to log in successfully.
The above is the detailed content of Teach you to use PHP to implement the face recognition and facial login function of the WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
