当前位置:网站首页>Laravel simply realizes Alibaba cloud storage + Baidu AI Cloud image review
Laravel simply realizes Alibaba cloud storage + Baidu AI Cloud image review
2022-07-04 16:06:00 【Shy_ shit_ man】
Find the file from the front end Get file pathname
$img = $request->file('name')->getPathname();Call image audit method
$res = $this->checkImg($img);
The return value is an array Can pass conclusion This subscript determines whether it is compliant
/**
* Image review
*/
public function checkImg($img){
$app_key = " Baidu intelligent cloud key";
$secretkey = " Baidu AI Cloud service meter has ";
$token = $this->getAccessToken($app_key,$secretkey);
$url = 'https://aip.baidubce.com/rest/2.0/solution/v1/img_censor/v2/user_defined?access_token=' . $token;
$img = file_get_contents($img);
$img = base64_encode($img);
$bodys = array(
'image' => $img
);
$res = $this->curlPost($url, $bodys);
$res = json_decode($res,true);
return $res;
}
/**
* Get the bills of Baidu open platform
* Reference link :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);
// Convert the returned result into an array
$res = json_decode($res, true);
if (isset($res['error'])) {
exit('API Key perhaps Secret Key Incorrect ');
}
$accessToken = $res['access_token'];
return $accessToken;
}After passing the audit, you can directly call the method of uploading cloud storage
$data['icon'] = $this->aliyunOss($img);
/**
* Ali cloud storage
* @param $cover
* @return mixed
* @throws \OSS\Core\OssException
*/
public function aliyunOss($cover){
$key = " Alibaba cloud secret key ";
$secret = " Alibaba cloud service key ";
$http = " Bucket address ";
$bucket = " Barrel name ";
// Create a oss
$oss = new OssClient($key,$secret,$http);
$fileName = md5(date("Y-m-d H:i:s",time())).'.png';
// Store pictures in oss In barrel And will url return
$res = $oss->uploadFile($bucket,$fileName,$cover);
return $res['oss-request-url'];
}curl Of post Request method
/**
* CURL Of Post Request method
* @param string $url
* @param string $param
* @return bool|string
*/
function curlPost($url = '', $param = '')
{
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
// initialization curl
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $postUrl);
curl_setopt($curl, CURLOPT_HEADER, 0);
// Require the result to be a string and output to the screen
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// post submission
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
// function curl
$data = curl_exec($curl);
curl_close($curl);
return $data;
}边栏推荐
- The per capita savings of major cities in China have been released. Have you reached the standard?
- Unity动画Animation Day05
- Dry goods | fMRI standard reporting guidelines are fresh, come and increase your knowledge
- MySQL learning notes - data type (numeric type)
- unity update 协程_Unity 协程的原理
- 一篇文章搞懂Go语言中的Context
- The new generation of domestic ORM framework sagacity sqltoy-5.1.25 release
- lnx 高效搜索引擎、FastDeploy 推理部署工具箱、AI前沿论文 | ShowMeAI资讯日报 #07.04
- Interface test - knowledge points and common interview questions
- MySQL - MySQL adds self incrementing IDs to existing data tables
猜你喜欢
![[tutorial] yolov5_ DeepSort_ The whole process of pytoch target tracking and detection](/img/a7/92d670776e3fd3d5add3aa144617c7.jpg)
[tutorial] yolov5_ DeepSort_ The whole process of pytoch target tracking and detection

A trap used by combinelatest and a debouncetime based solution

Book of night sky 53 "stone soup" of Apache open source community

Huawei cloud database DDS products are deeply enabled

AutoCAD - set color

AI system content recommendation issue 24

干货 | fMRI标准报告指南新鲜出炉啦,快来涨知识吧

函数式接口,方法引用,Lambda实现的List集合排序小工具

MySQL learning notes - data type (numeric type)

Stress, anxiety or depression? Correct diagnosis and retreatment
随机推荐
Unity脚本API—GameObject游戏对象、Object 对象
MySQL learning notes - data type (2)
C implementation defines a set of intermediate SQL statements that can be executed across libraries
165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version
Enter the width!
[native JS] optimized text rotation effect
文本挖掘工具的介绍[通俗易懂]
Unity prefab day04
Understand the context in go language in an article
LeetCode 35. Search the insertion position - vector traversal (O (logn) and O (n) - binary search)
Essential basic knowledge of digital image processing
Width accuracy
Unity update process_ Principle of unity synergy
一篇文章学会GO语言中的变量
函数式接口,方法引用,Lambda实现的List集合排序小工具
An article learns variables in go language
Variable cannot have type 'void'
After the eruption of Tonga volcano, we analyzed the global volcanic distribution and found that the area with the most volcanoes is here!
Redis' optimistic lock and pessimistic lock for solving transaction conflicts
[flask] ORM one to many relationship