laravel7 百度智能云检测内容及图片
1:百度智能云,获取AppID,API Key,Secret Key
https://console.bce.baidu.com/ai/?_=1642339692640&exraInfo=aipe#/ai/antiporn/app/detail~appId=3036325
2:下载phpSDK,并解压,引至项目中,修改命名空间
https://ai.baidu.com/sdk#anti
3:新建baiduserver.php 有控制器进行调用
<?php namespace App\Service; use App\Service\Img\AipContentCensor; use App\Service\img\AipImageCensor; use App\Service\Img\Libs\AipHttpClient; class BaiduService { const APP_ID = ''; const API_KEY = ''; const SECRET_KEY = ''; /** * 百度敏感词校验 * @param $content * @return bool */ public static function textCheck($content) { // APP_ID,API_KEY,SECRET_KEY 在官网注册获取 $client = new AipContentCensor(self::APP_ID, self::API_KEY, self::SECRET_KEY); $result = $client->textCensorUserDefined($content); print_r($result); if($result['conclusionType'] == 1) { return true; } return false; } /** * 百度图片校验 * @param $imgPath * @return bool */ public static function imageCheck($imgPath) { // APP_ID,API_KEY,SECRET_KEY 在官网注册获取 $client = new AipContentCensor(self::APP_ID, self::API_KEY, self::SECRET_KEY); $content = file_get_contents($imgPath); $result = $client->imageCensorUserDefined($content); //return $result; if($result['conclusionType'] == 1) { return '合规'; } return '不合规'; } }
4:控制器进行调用
路由:
//测试图像 Route::get('tp','exam8\ExamController@tp');
public function tp(){ $img='img/123.jpg'; $res=BaiduService::imageCheck($img); // 测试结果不合规 }
5:效果图: