欢迎您光临本站
首页 > PHP > 正文
faddei头像
faddei

2021-07-10

评论者
PHP
0 1

配置信息

// +----------------------------------------------------------------------
// | 百度智能云相关配置
// +----------------------------------------------------------------------
'baiduyun' => [
    'appid'   => '17718529',
    'appkey'   => 'v2ceKczihwqbElbNyBQ8Vx1m',
    'secretkey' => 'kMmbbcFfDE974cbazkKQ0NOwgj73X43R'
],


相似图片入库

/* 百度相似图片入库
 * imgurl 图片地址  ./uploads/.....jpg
 * brief 摘要信息
 * tags 分类信息
*/
public function baiduaddimg($imgurl,$brief,$tags){
    $url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='.config('baiduyun.appkey').'&client_secret='.config('baiduyun.secretkey');
    $token = $this->getCurls($url);
    //相似图片入库
    $url   = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/similar/add?access_token='.$token['access_token'];
    $img   = file_get_contents($imgurl);
    $img   = base64_encode($img);
    $bodys = array(
        'brief' => $brief,
        'image' => $img,
        'tags'  => $tags
    );
    $res = $this->request_posts($url, $bodys);
    return $res;
}
//curl-get请求
public function getCurls($url){
    if(empty($url)){
        return  returnMsg('201','请求url不能为空');    
    }
    $info = file_get_contents($url); //发送HTTPs请求并获取返回的数据,推荐使用curl
    $json = json_decode($info); //对json数据解码
    $arr  = get_object_vars($json);
    return $arr;
}
/**
* 发起http post请求(REST API), 并获取REST请求的结果
* @param string $url
* @param string $param
* @return - http response body if succeeds, else false.
*/
public function request_posts($url = '', $param = ''){
    if (empty($url) || empty($param)) {
        return false;
    }
    $postUrl  = $url;
    $curlPost = $param;
    // 初始化curl
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $postUrl);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    // 要求结果为字符串且输出到屏幕上
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    // post提交方式
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
    // 运行curl
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
}


相似图片检索

/**
 * @title 百度智能云相似图片检索
 * @desc  {"0":"接口地址:api/imgupload","1":"请求方式:POST","2":"应用场景:百度智能云相似图片检索"}
 * @param {"name":"typename","type":"string","required":true,"default":"","desc":"图片存储目录名"}
 * @param {"name":"img_src","type":"file","required":true,"default":"","desc":"图片文件"}
 * @return {"name":"code","type":"int","required":true,"desc":"返回码:200上传成功 201上传失败 202未传递图片","level":1}
 * @return {"name":"msg","type":"string","required":true,"desc":"返回信息","level":1}
 * @return {"name":"data","type":"array","required":true,"desc":"数据","level":1}
 * @return {"name":"img_src","type":"string","required":true,"desc":"图片存储地址","level":2}
 * @showcode {"code":"200","msg":"上传成功","data":{"img_src":"/uploads/orgimg/2019/03/19/11004523414.jpg"}}
*/
public function baidusoso()
{
    $user_id = input('user_id');
    $imgurl  = input('imgurl');
    $url     = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='.config('baiduyun.appkey').'&client_secret='.config('baiduyun.secretkey');
    $token   = $this->getCurls($url);
    $url     = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/realtime_search/similar/search?access_token='.$token['access_token'];
    $img     = file_get_contents($imgurl);
    $img     = base64_encode($img);
    $bodys   = array('image'=>$img);
    $res     = $this->request_posts($url, $bodys);
    $arr     = json_decode($res,true);
    foreach ($arr['result'] as $k => $v) {
        if($v['score']>0.4){
            $brief = json_decode($v['brief'],true);
            $data['id'][] = $brief['id'];
            $data['cont_sign'][] = $v['cont_sign'];
        }
    }
    foreach ($data['cont_sign'] as $v2) {
        $info = Db::name('course_productimg')->where(['cont_sign'=>$v2])->select();
        foreach ($info as $key => $value) {
            $data['id'][] = $value['product_id'];
        }
    }
    $idsarr = array_unique($data['id']);
    $jieguo = array();
    foreach ($idsarr as $ks => $vs) {
        $where .= "product_id = {$vs} or ";
        $jieguo[] = $vs;
    }
    $where = substr($where, 0, -4);
    $list  = Db::name('course_product')->where($where)->select();
    foreach ($list as $key2 => $value2) {
        $list2[$key2] = $value2;
        $list2[$key2]['coverimg'] = config('webinfo.www').'/uploads/'.$value2['coverimg'];
        $list2[$key2]['addtime']  = date('Y-m-d H:i'); 
    }
    //入库
    $add['user_id'] = $user_id;
    $add['imgurl']  = $imgurl;
    $add['jieguo']  = json_encode($jieguo);
    $add['addtime'] = time();
    @Db::name('user_soso')->insertGetId($add);
    return returnMsg('200','success',$list2);     
}


版权声明:本站所提供的文章、图片等内容均为用户发布或互联网整理而来,仅供学习参考,如有侵犯您的版权,请联系我们客服人员删除。

140

精彩推荐

暂无评论

文明用语