当前位置:网站首页>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;
}
边栏推荐
- Functional interface, method reference, list collection sorting gadget implemented by lambda
- 2022年九大CIO趋势和优先事项
- Unity script API - time class
- The new generation of domestic ORM framework sagacity sqltoy-5.1.25 release
- The four most common errors when using pytorch
- Rearrange array
- 在芯片高度集成的今天,绝大多数都是CMOS器件
- LNX efficient search engine, fastdeploy reasoning deployment toolbox, AI frontier paper | showmeai information daily # 07.04
- Feature extraction and detection 15-akaze local matching
- Actual combat | use composite material 3 in application
猜你喜欢
科研漫画 | 联系到被试后还需要做什么?
这几年爆火的智能物联网(AIoT),到底前景如何?
A trap used by combinelatest and a debouncetime based solution
AI做题水平已超过CS博士?
Interface test - knowledge points and common interview questions
Scientific research cartoon | what else to do after connecting with the subjects?
.Net 应用考虑x64生成
2022年九大CIO趨勢和優先事項
error: ‘connect‘ was not declared in this scope connect(timer, SIGNAL(timeout()), this, SLOT(up
Common API day03 of unity script
随机推荐
Unity script introduction day01
Find numbers
【读书会第十三期】视频文件的编码格式
Unity animation day05
2022年九大CIO趋势和优先事项
Shell programming basics
. Net delay queue
[flask] ORM one to many relationship
科普达人丨一文看懂阿里云的秘密武器“神龙架构”
怎么判断外盘期货平台正规,资金安全?
Implementation of web chat room
【读书会第十三期】 音频文件的封装格式和编码格式
hexadecimal
165 webmaster online toolbox website source code / hare online tool system v2.2.7 Chinese version
這幾年爆火的智能物聯網(AIoT),到底前景如何?
AutoCAD - set color
Talking about Net core how to use efcore to inject multiple instances of a context annotation type for connecting to the master-slave database
Width accuracy
Unity脚本常用API Day03
CentOS 6.3 下 PHP编译安装JSON模块报错解决