public function imgAdd(Request $request)
{
$pathName = $request->file('phone')->getPathname();
$token =$this->getAccessToken('mr5GSx453SUGD64OId9zojDE', 'tcAQfPdjo4v1VwoHLBEwtMBVzrebG3GS');
$url = 'https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined?access_token=' . $token;
$img = file_get_contents($pathName);
$img = base64_encode($img);
$bodys = array(
'image' => $img
);
$res = $this->curlPost($url, $bodys);
$res = json_decode($res,true);
if($res['conclusionType']!=1){
die('异常');
}
// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
$accessKeyId = config('ali.accessKeyId');
$accessKeySecret = config('ali.accessKeySecret');
// Endpoint以杭州为例,其它Region请按实际情况填写。
$endpoint = config('ali.endpoint');
// 存储空间名称
$bucket= config('ali.bucket');
$fileName = md5(time() . $pathName . rand(1111,999999));
// <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg
$object = date('Y-m-d',time()) . '/' .$fileName . '.png';
$content = file_get_contents($pathName);
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$ossClient->putObject($bucket, $object, $content);
} catch (OssException $e) {
print $e->getMessage();
}
$arr=$request->input();
$path=$request->file('phone')->store('phone');
$arr['phone']=$path;
$ak['name'] = $arr['name'];
$ak['phone'] = $arr['phone'];
$addRes = Img::create($ak);
if($addRes){
return redirect('imgList');
}else{
return '添加失败';
}
}
aliyun
最新推荐文章于 2024-07-12 16:16:27 发布