<?php
namespace app\bshy_web\controller;
use think\Db;
use think\Controller;
use Green\Request\V20180509 as Green;
use Green\Request\Extension\ClientUploader;
class Aliyun extends Controller
{
//设置
protected $beforeActionList = [
\'first\'
];
private static $accessKeyId=\'*******************\';
private static $accessKeySecret=\'***********************************\';
/**
* 检查当前用户是否登录 判断 token 是否 正确 过期
* @param uid 必传
* @param token 可选
*/
protected function first()
{
header(\'Access-Control-Allow-Origin:*\');
$response = request();
$data = $response->param();
if (!$response->has(\'uid\')) {
//返回基本信息
if (!$data) {
$this->status = 0;
$this->msg = \'传入的参数不能为空\';
} else {
$this->status = 200;
$this->msg = \'\';
$this->data = $data;
}
} else {
//判断处理当前信息
if ($response->has(\'token\')) {
$token = $response->param(\'token\');
$user = new User();
$res = $user->checkLogin($data[\'uid\'], $data[\'token\']);
} else {
$user = new User();
$res = $user->checkLogin($data[\'uid\']);
}
$status = $res[\'status\'];
$msg = $res[\'msg\'];
if ($status == 200) {
$info = $data;
} else {
if ($status != 0) {
$status = 2;
} else {
$status = 0;
}
$info = \'\';
}
$this->status = $status;
$this->msg = $msg;
$this->data = $info;
}
}
//阿里云图片检测
public function aliyun_img_green(){
$file = $_FILES[\'file\'][\'tmp_name\'];
import(\'aliyuncs.aliyun-oss-php-sdk.autoload\', EXTEND_PATH,\'.php\');
import(\'aliyuncs.aliyun_php_sdk_core.Config\', EXTEND_PATH,\'.php\');
date_default_timezone_set("PRC");
//请替换成您的accessKeyId、accessKeySecret
$iClientProfile = \DefaultProfile::getProfile("cn-beijing", self::$accessKeyId, self::$accessKeySecret);
\DefaultProfile::addEndpoint("cn-beijing", "cn-beijing", "Green", "green.cn-beijing.aliyuncs.com");
$client = new \DefaultAcsClient($iClientProfile);
$request = new Green\ImageSyncScanRequest();
$request->setMethod("POST");
$request->setAcceptFormat("JSON");
//本地文件先进行上传,然后进行检测
$uploader = ClientUploader::getImageClientUploader($client);
$url = $uploader->uploadFile("$file");
$task1 = array(\'dataId\' => uniqid(),
\'url\' => $url
);
// 设置待检测图片, 一张图片一个task,
// 多张图片同时检测时,处理的时间由最后一个处理完的图片决定。
// 通常情况下批量检测的平均rt比单张检测的要长, 一次批量提交的图片数越多,rt被拉长的概率越高
// 这里以单张图片检测作为示例, 如果是批量图片检测,请自行构建多个task
// 计费按照该处传递的场景进行
// 一次请求中可以同时检测多张图片,每张图片可以同时检测多个风险场景,计费按照场景计算
// 例如:检测2张图片,场景传递porn,terrorism,计费会按照2张图片鉴黄,2张图片暴恐检测计算
$request->setContent(json_encode(array("tasks" => array($task1),
"scenes" => array("porn","terrorism"))));
try {
$response = $client->getAcsResponse($request);
$result=array();
if(200 == $response->code){
$taskResults = $response->data;
foreach ($taskResults as $taskResult) {
if(200 == $taskResult->code){
$sceneResults = $taskResult->results;
foreach ($sceneResults as $sceneResult) {
$scene = $sceneResult->scene;
$suggestion = $sceneResult->suggestion;
$result=$taskResult;
}
}else{
print_r("task process fail:" + $response->code);
}
}
}else{
print_r("detect not success. code:" + $response->code);
}
$result=$result->results[1];
$data=array();
if($result->suggestion == \'pass\'){
$data[\'code\']=true;
$data[\'label\']=$this->getimglabel($result->label,$result->scene);
}else{
$data[\'code\']=false;
$data[\'label\']=$this->getimglabel($result->label,$result->scene);
}
return json($data);
} catch (Exception $e) {
print_r($e);
}
}
//图片检测反馈描述
public function getimglabel($label,$scene){
if($scene==\'porn\'){
if($label==\'normal\'){
return \'正常图片,无色情内容\';
}elseif($label==\'sexy\'){
return \'性感图片\';
}elseif($label==\'porn\'){
return \'色情图片\';
}
}elseif($scene==\'terrorism\'){
if($label==\'normal\'){
return \'正常图片\';
}elseif($label==\'bloody\'){
return \'血腥\';
}elseif($label==\'explosion\'){
return \'爆炸烟光\';
}elseif($label==\'outfit\'){
return \'特殊装束\';
}elseif($label==\'logo\'){
return \'特殊标识\';
}elseif($label==\'weapon\'){
return \'武器\';
}elseif($label==\'politics\'){
return \'涉政\';
}elseif($label==\'violence\'){
return \'打斗\';
}elseif($label==\'crowd\'){
return \'聚众\';
}elseif($label==\'parade\'){
return \'游行\';
}elseif($label==\'carcrash\'){
return \'车祸现场\';
}elseif($label==\'flag\'){
return \'旗帜\';
}elseif($label==\'location\'){
return \'地标\';
}elseif($label==\'others\'){
return \'其他\';
}
}elseif($scene==\'ad\'){
if($label==\'normal\'){
return \'正常图片\';
}elseif($label==\'ad\'){
return \'其他广告\';
}
}elseif($scene==\'qrcode\'){
if($label==\'normal\'){
return \'正常图片\';
}elseif($label==\'qrcode\'){
return \'含二维码的图片\';
}
}elseif($scene==\'live\'){
if($label==\'normal\'){
return \'正常图片\';
}elseif($label==\'meaningless\'){
return \'无意义图片\';
}elseif($label==\'PIP\'){
return \'画中画\';
}elseif($label==\'smoking\'){
return \'吸烟\';
}elseif($label==\'drivelive\'){
return \'车内直播\';
}
}elseif($scene==\'qrcode\'){
if($label==\'normal\'){
return \'正常图片\';
}elseif($label==\'TV\'){
return \'带有管控logo的图片\';
}elseif($label==\'trademark\'){
return \'商标\';
}
}
}
//阿里云文本检测
public function aliyun_green(){
$response=request();
$content=$response->param(\'content\');
import(\'aliyuncs.aliyun_php_sdk_core.Config\', EXTEND_PATH,\'.php\');
date_default_timezone_set("PRC");
$iClientProfile = \DefaultProfile::getProfile("cn-beijing", self::$accessKeyId, self::$accessKeySecret);
\DefaultProfile::addEndpoint("cn-beijing", "cn-beijing", "Green", "green.cn-beijing.aliyuncs.com");
$client = new \DefaultAcsClient($iClientProfile);
$request = new Green\TextScanRequest();
$request->setMethod("POST");
$request->setAcceptFormat("JSON");
$task1 = array(\'dataId\' => uniqid(),
\'content\' => $content
);
/**
* 文本垃圾检测: antispam
* 关键词检测: keyword
**/
$request->setContent(json_encode(array("tasks" => array($task1),
"scenes" => array("antispam"))));
try {
$response = $client->getAcsResponse($request);
$result=array();
if(200 == $response->code){
$taskResults = $response->data;
foreach ($taskResults as $taskResult) {
if(200 == $taskResult->code){
$sceneResults = $taskResult->results;
foreach ($sceneResults as $sceneResult) {
$scene = $sceneResult->scene;
$suggestion = $sceneResult->suggestion;
$result=$taskResult;
}
}else{
print_r("task process fail:" + $response->code);
}
}
}else{
print_r("detect not success. code:" + $response->code);
}
$result=$result->results[0];
$data=array();
if($result->label == \'normal\'){
$data[\'code\']=true;
$data[\'label\']=$this->getlabel($result->label);
}else{
$data[\'code\']=false;
$data[\'label\']=$this->getlabel($result->label);
}
return json($data);
} catch (Exception $e) {
print_r($e);
}
}
//文本检测反馈描述
public function getlabel($label){
if($label==\'normal\'){
return \'正常文本\';
}elseif($label==\'spam\'){
return \'输入的内容含垃圾信息\';
}elseif($label==\'ad\'){
return \'输入的内容含广告\';
}elseif($label==\'politics\'){
return \'输入的内容含渉政\';
}elseif($label==\'terrorism\'){
return \'输入的内容含暴恐\';
}elseif($label==\'abuse\'){
return \'输入的内容含辱骂\';
}elseif($label==\'porn\'){
return \'输入的内容含色情\';
}elseif($label==\'flood\'){
return \'输入的内容含灌水\';
}elseif($label==\'contraband\'){
return \'输入的内容含垃违禁\';
}elseif($label==\'customized\'){
return \'输入的内容包含敏感词\';
}
}
}