本篇文章为大家展示了怎么在微信小程序中实现刷脸登录,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
public function login(){
$dir="./***";//自己定义的文件名
if(!file_exists($dir)){
mkdir($dir,0777,true);
}
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 3145728 ;// 设置附件上传大小
$upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
$upload->rootPath = $dir; // 设置附件上传根目录
$upload->savepath='';
// 上传单个文件
$info = $upload->uploadOne($_FILES['file']);
if(!$info) {// 上传错误提示错误信息
return $this->ajaxReturn(array('error'=>true,'msg'=>$upload->getError()));
}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',$this->face_group(),$options);
if($ret['error_code']==0){
$user = $ret['result']['user_list'][0];
$no = $user['user_id'];
$score = $user['score'];
if(!empty($no)){
$data = M('student')->field('no,name,sex')->where("no='{$no}'")->find();
if($data){
//查到此学号
$data['score'] = $score;
echo json_encode($data,JSON_UNESCAPED_UNICODE);
}else{
//本地库不存在此学号
echo "本地数据库没有该学生,百度云库信息:个人信息:{$no},分值:{$score}";
}
}
}else{
echo "活体检测失败,".json_encode($ret,JSON_UNESCAPED_UNICODE);
}
}
}
里面的search方法是sdk的方法
public function search($image, $imageType, $groupIdList, $options=array()){
$data = array();
$data['image'] = $image;
$data['image_type'] = $imageType;
$data['group_id_list'] = $groupIdList;
$data = array_merge($data, $options);
return $this->request($this->searchUrl, json_encode($data), array(
'Content-Type' => 'application/json',
));
}
上述内容就是怎么在微信小程序中实现刷脸登录,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注天达云行业资讯频道。