您的位置:首页 > 移动开发 > 微信开发

php微信上传图片之人脸识别

2017-04-08 11:27 387 查看

人脸识别是调用http://apicn.faceplusplus.com中的接口

info.html中的js代码

//引入
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: '<?php echo $jssdk["appId"];?>', // 必填,公众号的唯一标识
timestamp: '<?php echo $jssdk["timestamp"];?>', // 必填,生成签名的时间戳
nonceStr: '<?php echo $jssdk["nonceStr"];?>', // 必填,生成签名的随机串
signature: '<?php echo $jssdk["signature"];?>', // 必填,签名,见附录1
jsApiList: ['checkJsApi', 'chooseImage', 'uploadImage', 'downloadImage', 'previewImage'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
var images = {
localId: [],
serverId: []
};
var cb=1;
wx.ready(function() {
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
$('#selectImage').click(function () {
var sid=images.serverId.length;
var ca=0;
if(sid >= 1) {
alert('目前仅支持1张图片上传');
return;
}else if(sid>0){
ca=cb-sid;
}else{
ca=cb;
}
wx.chooseImage({
count: ca,
sizeType: ['compressed'],
success:function (res) {
images.localId = res.localIds;
$.each(res.localIds,function(i,n){
$("#myimg").attr('src',n);
$("#myimg").attr('sta','1');
//$("#img").append('<li><img src="'+n+'" /></li>');// 往ID为img的html标签中插入图片预览
});
syncUpload(res.localIds);
}
});
});

var syncUpload = function(localIds){
var localId = localIds.pop();
wx.uploadImage({
localId: localId,
isShowProgressTips: 1,
success: function (res) {
images.serverId.push(res.serverId);
//其他对serverId做处理的代码
if(localIds.length > 0){
syncUpload(localIds);
}
}
});
};
});

wx.checkJsApi({
jsApiList: [
'checkJsApi',
'chooseImage',
'uploadImage',
'downloadImage',
'previewImage'
],
success: function(res) {
if (res.checkResult.getLocation == false) {
alert('你的微信版本太低,不支持微信JS接口,请升级到最新的微信版本!');
return;
}
}
});

function save_img() {
var lengthb = images.serverId.length;
if(lengthb>0){
$("#cli").attr('data-am-modal',"{target: '#my-alert1'}");
$("#cli").click();
var myobjb = eval(images.serverId);
var hlc='';
for(var i=0;i<myobjb.length;i++){
hlc = hlc + myobjb[i] + ',';
}
$("#cli").val(hlc);
$('#selectImage').attr("id",'');
var outtime=2;
var interval = setInterval(function(){
var time = --outtime;
if(time <= 0) {
document.getElementById('form').submit();
clearInterval(interval);

4000
}
}, 1000);
return true;
} else {
alert("请上传您的个人照片!");
return false;
}
var sta=$("#myimg").attr('sta');
if(sta==1){
$("#cli").attr('data-am-modal',"{target: '#my-alert1'}");
$("#cli").click();
var outtime=2;
var interval = setInterval(function(){
var time = --outtime;
if(time <= 0) {
document.getElementById('form').submit();
clearInterval(interval);
}
}, 1000);
}else{
alert('请选择照片');
}
}

</script>


php中代码

<?php
class shualianMod extends comMod {

public function __construct()
{
parent::__construct();
}

public function index() {
$status=module('wx_win')->ckpower();
//    $status=1;
// 绑定微信JSSDK接口参数
$jssdk = $this->wxjs($this->config['WX_appid'], $this->config['WX_appsecret']);
$this->jssdk =$jssdk;
if($status <> 1){
$this->redirect('/index.php');
return false;
}else{
$this->sta=intval(in($_GET['sta']));
$this->display('info.html');
}
}

public function post() {
$status=module('wx_win')->ckpower();
if($status <> 1){
echo "<script>alert('您今天已经刷过了');window.location.href='/index.php'</script>";
//         $this->redirect('/index.php');
return false;
}
// ============================== 微信上传图片 ==============================
$img = !empty($_POST['img']) ? array_filter(explode(',', trim(in($_POST['img']), ','))) : '';
$list=array();

if (!empty($img)) {
$accessToken = getAccessToken($this->config['WX_appid'], $this->config['WX_appsecret']);
include_once(__ROOTDIR__ . '/system/ext/SimpleImage.php');
$image = new cls_image();
$nimg = '';
for ($i = 0; $i < count($img); $i++) {
$url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" . $accessToken . "&media_id=" . $img[$i];
$fileInfo = $this->downloadWeixinFile($url);
$filename = $img[$i] . ".jpg";
$du = $this->saveWeixinFile($filename, $fileInfo["body"], $image);
if (!empty($du)) {
$list[$i]['url']=$du;
}
}
}
$url=$list[0]['url'];
// ============================== 微信上传图片 end==============================
// ============================== 人脸识别 ==============================

if($_SERVER['HTTPS'] != 'on'){
$ht='http://';
}else{
$ht='https://';
}
$server_name=$_SERVER['SERVER_NAME'];
$imgurl=$ht.$server_name.$url;
//    echo $imgurl;exit();
$faceArray=module('face_recognition')->face(urlencode($imgurl));
//         echo "reshu:".count($faceArray);
//         var_dump($faceArray);exit;
if(count($faceArray) <> 1){
$this->redirect('/index.php/shualian/index?sta=2');
return false;
}
$tempFace = $faceArray[0];
$tempAttr = $tempFace->{'attribute'};
$tempSmiling = $tempAttr->{'smiling'}->{'value'};
$rand=rand(1,100);
$fen=$tempSmiling*0.2+$rand*0.8;
//    echo $tempSmiling.'<br/>'.$rand.'<br/>'.$fen;
//    exit();
$info=model('shualian')->getInfo();
$hs=array();
if($fen >= 91){
$hs1=$info[0]['hs'];
$hs=explode('#',$hs1);
$num=count($hs);
if($num > 0){
$i=rand(0,$num-1);
$str=$hs[$i];
}
$zk=2;
}else if($fen >= 71 && $fen<=90){
$hs1=$info[1]['hs'];
$hs=explode('#',$hs1);
$num=count($hs);
if($num > 0){
$i=rand(0,$num-1);
$str=$hs[$i];
}
$zk=4;
}else if($fen >= 50 && $fen<=70){
$hs1=$info[2]['hs'];
$hs=explode('#',$hs1);
$num=count($hs);
if($num > 0){
$i=rand(0,$num-1);
$str=$hs[$i];
}
$zk=6;
}else{
$hs1=$info[3]['hs'];
$hs=explode('#',$hs1);
$num=count($hs);
if($num > 0){
$i=rand(0,$num-1);
$str=$hs[$i];
}
$zk=6;
}
$userid=$_SESSION['userid'];
$zkinfo=model('shualian')->getInfoByzk($zk);
$discount_id=$zkinfo['id'];
$status=1;
$gettime=time();
$data=array();
$data['member_id']=$userid;
$data['discount_id']=$discount_id;
$data['status']=$status;
$data['gettime']=$gettime;
$id=model('wx_win')->add($data);
if($id){
$this->
b72e
zk=$zk;
$this->hs=$str;
$this->fen=intval($fen);
$this->gettime=$gettime;
// ============================== 删除图片 ==============================
@unlink(__ROOTDIR__.$url);
$this->display('jiandingjieguo.html');
}else{
echo "<script>alert('系统出错,请重新获取');window.history.go(-1);</script>";
}

// ============================== 人脸识别end ==============================

}
public function discount(){
// 绑定微信JSSDK接口参数
$jssdk = $this->wxjs($this->config['WX_appid'], $this->config['WX_appsecret']);
$this->signPackage =$jssdk;
$this->zk=in($_GET['zk']);
$gettime=in($_GET['gettime']);
$userid=$_SESSION['userid'];
$this->member_info=model('member')->member_getinfo($userid);

$this->outtime=strtotime('+1 days',strtotime(date('Y-m-d',$gettime)));
$this->display('get_discount.html');
}

}


调用识别接口

<?php
//测试函数
class face_recognitionMod extends comMod {

public function __construct()
{
parent::__construct();
}

// 调用人脸识别的API返回识别结果
public function face($imgUrl)
{
// face++ 链接
$jsonStr =
file_get_contents("http://apicn.faceplusplus.com/v2/detection/detect?api_key=*******&api_secret=******&url=".$imgUrl."%3Fv%3D2&attribute=age%2Cgender%2Crace%2Csmiling%2Cpose%2Cglass");
$replyDic = json_decode($jsonStr);
//         $resultStr = "";
$faceArray = $replyDic->{'face'};

return $faceArray;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息