laravel 简单实现 阿里云存储+百度智能云 图片审核

找到前端传过来的文件 获取文件路径名

$img = $request->file('name')->getPathname();

调用图片审核方法 

$res = $this->checkImg($img);

返回值是一个数组 可以通过 conclusion 这个下标进行判断是否合规

/**
     * 图片审核
     */
    public function checkImg($img){
        $app_key = "百度智能云key";
        $secretkey = "百度智能云服务米有";
        $token = $this->getAccessToken($app_key,$secretkey);
        $url = 'https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined?access_token=' . $token;
        $img = file_get_contents($img);
        $img = base64_encode($img);
        $bodys = array(
            'image' => $img
        );
        $res = $this->curlPost($url, $bodys);
        $res = json_decode($res,true);
        return $res;
    }
    /**
     * 获取百度开放平台的票据
     * 参考链接:https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu
     */
    public function getAccessToken($ApiKey = '', $SecretKey = '', $grantType = 'client_credentials')
    {

        $url = 'https://aip.baidubce.com/oauth/2.0/token';
        $post_data['grant_type'] = $grantType;
        $post_data['client_id'] = $ApiKey;
        $post_data['client_secret'] = $SecretKey;
        $o = "";
        foreach ($post_data as $k => $v) {
            $o .= "$k=" . urlencode($v) . "&";
        }
        $post_data = substr($o, 0, -1);

        $res = $this->curlPost($url, $post_data);
        //进行把返回结果转成数组
        $res = json_decode($res, true);
        if (isset($res['error'])) {
            exit('API Key或者Secret Key不正确');
        }
        $accessToken = $res['access_token'];
        return $accessToken;
    }

审核通过后直接调用上传云存储的方法即可

$data['icon'] = $this->aliyunOss($img);
/**
     * 阿里云存储
     * @param $cover
     * @return mixed
     * @throws \OSS\Core\OssException
     */
    public function aliyunOss($cover){
        $key = "阿里云秘钥";
        $secret = "阿里云服务秘钥";
        $http = "桶地址";
        $bucket = "桶名";
        //创建一个oss
        $oss = new OssClient($key,$secret,$http);
        $fileName = md5(date("Y-m-d H:i:s",time())).'.png';
        //将图片存储到oss桶内 并将url返回
        $res = $oss->uploadFile($bucket,$fileName,$cover);
        return $res['oss-request-url'];
    }

curl的post请求方法

    /**
     * CURL的Post请求方法
     * @param string $url
     * @param string $param
     * @return bool|string
     */
    function curlPost($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;
    }

你可能感兴趣的:(日进一步,laravel,阿里云,百度)