//先把七牛云下载下来
composer require qiniu/php-sdk
//引入必须要的类
use Qiniu\Auth;
use Qiniu\Http\Client;
//在common.php 中定义一个公共方法
function imageCheck($image_url)
{
// 用于签名的公钥和私钥
$ak ="";//这里填写你的ak
$sk ="";//这里填写你的sk
// $image_url='https://img-home.csdnimg.cn/images/20211129024917.jpg';//这里是图片地址
$host = 'ai.qiniuapi.com';
$uri = 'http://ai.qiniuapi.com/v3/image/censor';//请求接口
$contentType = 'application/json';
$method='POST';
//审核类型scenes与相应的label/group(值)
$scenes = [
'censor' => ['pulp', 'terror', 'politician', 'ads'],
'pulp' => ['pulp'],
'terror' => ['terror'],
'politician' => ['politician'],
'ads' => ['ads']
];
$body = [
'data' => ['uri' => $image_url],
'params' => ['scenes' => $scenes['censor']]//图片审核类型
];
$auth = new Auth($ak,$sk);
$headers = $auth->authorizationV2($uri, $method, json_encode($body), $contentType);
$headers['Content-Type'] = $contentType;
$headers['Host'] = $host;
$res = Client::post($uri, json_encode($body), $headers);//需要引入命名空间
$res = $res->body;
$res = json_decode($res,true);
$res = $res['result']['scenes']['pulp']['suggestion'];
return $res;
// if($res=='pass')//鉴定结果通过 ,无色请
// {
//// 可以添加入库,这里自由发挥即可
//
//
// }
}
public function newsAdd(){
$file = $_FILES;
//本地的文件路径
$localFilePath = $file['file']['tmp_name'];
//截取文件后缀名
//$suffix = '.jpg';
$suffix = strtolower(substr($file['file']['name'],strpos($file['file']['name'],'.')));
//生成一个唯一的文件名称,重命名 (md5加密原文件名+秒+随机数)
$fileName = md5($file['file']['name']) . date('s',time()) . rand(1,9999999);
$fileName .= $suffix;
//上传七牛云业务逻辑
$accessKey = ''; //去控制台的秘钥管理拿AK
$secretKey = '';//去控制台的秘钥管理拿SK
//七牛云桶名,根据自己实际进行填写
$bucket = '';
//图片违规验证
$url = 'http://ai.qiniuapi.com/v3/image/censor';
$method="POST";
$host="ai.qiniuapi.com";
$contentType = 'application/json';
//图片地址
$image_url='https://www.gaopinimages.com/img/newLogo_back_white.bd2c2e64.png';
$scenes = [
'censor' => ['pulp', 'terror', 'politician', 'ads'],
'pulp' => ['pulp'],
'terror' => ['terror'],
'politician' => ['politician'],
'ads' => ['ads']
];
$body = [
'data' => ['uri' => $image_url],
'params' => ['scenes' => $scenes['censor']]
];
$auth = new Auth($accessKey, $secretKey);
$headers=$auth->authorizationV2($url,$method,json_encode($body),$contentType);
$headers['Content-type']=$contentType;
$headers['Host']=$host;
$response=Client::post($url,json_encode($body),$headers);
// 生成上传Token
$token = $auth->uploadToken($bucket);
// 构建 UploadManager 对象
$uploadMgr = new UploadManager();
// 调用 UploadManager 的 putFile 方法进行文件的上传。
list($ret, $err) = $uploadMgr->putFile($token, $fileName, $localFilePath);
//错误信息提示
if ($err != null) {
//可调整为错误页面
$this->error('上传文件失败');
}
//添加自己的数据库
////把七牛云图片路径存储到我们自己的数据库 七牛云图片路径
// $imageUrl = 'http://qtpud69oi.hn-bkt.clouddn.com/' . $fileName;
////入库业务逻辑 create save 只有这两个模型方法才能自动写入时间戳
// $result =BannerModel::create([
// 'image_url'=>$imageUrl,
// ]);
echo 211;die;
// $this->success('上传文件成功');
}