1. composer 下载
composer require zgldh/qiniu-laravel-storage
2. 配置文件(在?config下的app.php?加上)
zgldh\QiniuStorage\QiniuFilesystemServiceProvider::class
接下来在config/filesystems.php 里的disks 中新增如下选项:
'disks' => [
... ,
'qiniu' => [
'driver' => 'qiniu',
'domains' => [
'default' => 'laravelacademy.com1.z0.glb.clouddn.com', //你的七牛域名
'https' => 'dn-laravelacademy.qbox.me', //你的HTTPS域名
'custom' => 'static.laravelacademy.org', //你的自定义域名
],
'access_key'=> '', //AccessKey
'secret_key'=> '', //SecretKey
'bucket' => '', //Bucket名字
'notify_url'=> '', //持久化处理回调地址
],
],
3.控制器实现
/*
* 图片上传 + 图片审核
* */
public function upImage(request $request){
//图片审核
$fileTmp = $request->file('image')->getPathname();
$result = $this->imageAudit($fileTmp);
if($result['conclusion'] != "合规") return json_encode(['code'=>400,',msg'=>"文件不合规"]);
var_dump($result);
//云存储
$disk = Storage::disk('qiniu'); //使用七牛云上传
$time = date('Y/m/d/H-m-s');
$file = $request->file('image');
$filePath = $file->getRealPath();
$ext = $file->getClientOriginalExtension();
$filename = $disk->put($time.'.'.$ext, file_get_contents($filePath));//上传
//图片地址
$url = 'http://liyan.shop'.$time.'.'.mt_rand(1000,9999).$ext;
if(!$filename) return json_encode(['code'=>400,'msg'=>"文件错误"]);
return json_encode(['code'=>200,'msg'=>"文件上传成功",'data'=>$url]);
}
public function contentAudit(Request $request)
{
//审核的内容
$content = $request['content'];
$token = $this->getAccessToken('apikey', 'secrekey');
$url = 'https://aip.baidubce.com/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=' . $token;
$bodys = array(
'text' => $content
);
$res = $this->curlPost($url, $bodys);
//结果转成数组
$res = json_decode($res, true);
//根据自己的业务逻辑进行处理
return $res;
}
/**
* 图片审核
*/
public function imageAudit($fileTmp)
{
$token = $this->getAccessToken('apikey', 'secrekey');
$url = 'https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined?access_token=' . $token;
$img = file_get_contents($fileTmp);//本地路径
$img = base64_encode($img);
$bodys = array(
'image' => $img
);
$res = $this->curlPost($url, $bodys);
//结果转成数组
$res = json_decode($res, true);
//根据自己的业务逻辑进行处理
return $res;
}
/**
* 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;
}
/**
* 获取百度开放平台的票据
* 参考链接: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;
}
|