php使用阿里云人体分割
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2021/2/25 0025
* Time: 17:09
*/
header('Access-Control-Allow-Origin:*'); // *代表允许任何网址请求
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, x-file-name");
header('Access-Control-Allow-Methods: GET, POST, PUT,DELETE');
date_default_timezone_set('GMT');
class Img
{
static private $instance;
static public function getInstance(){
if(!self::$instance instanceof self){
self::$instance = new self();
}
return self::$instance;
}
private function __clone(){}
//阿里云人像分割
public function Ali($imgurl){
$AccessKeyId = "xxxxxxx";
$secret = "xxxxx";
$SignatureMethod = "HMAC-SHA1";
$RegionId = "cn-shanghai";
$Format = "JSON";
$SignatureVersion ="1.0";
$Version = "2019-12-30";
$Action = "SegmentBody";
$time = date('Y-m-d\TH:i:s\Z', time() - date('Z'));
$rand = uniqid().rand(1,9999999);
$imgurl = $imgurl;
$params = [
'SignatureMethod'=>$SignatureMethod,
'SignatureNonce'=>$rand,
'AccessKeyId'=>$AccessKeyId,
'SignatureVersion'=>$SignatureVersion,
'Timestamp'=>$time,
'Format'=>$Format,
'RegionId'=>$RegionId,
'Version'=>$Version,
'Action'=>$Action,
'ImageURL'=>$imgurl,
];
$params['Signature'] = $this->AliSign($params, $secret);
$uri = http_build_query($params);
// var_dump($uri);die;
$url = 'http://imageseg.cn-shanghai.aliyuncs.com/?'.$uri;
$msg['code'] = 200;
$msg['msg'] = '成功';
$msg['data'] = $url;
echo $url;die;
$msg = array();
try{
$res = json_decode($this->juhecurl($url),true);
$img = isset($res['Data'])?$res['Data']['ImageURL']:'';
if($img){
$msg['code'] = 200;
$msg['msg'] = '成功';
$msg['data'] = $img;
}else{
$msg['code'] = 500;
$msg['msg'] = '失败';
$msg['data'] = $res['Code'];
}
}catch(\Exception $e){
$msg['code'] = 500;
$msg['msg'] = '失败';
}
echo json_encode($msg);
die;
if($res['ImageURL']){
}
return $url;
}
//阿里云签名
public function AliSign($params,$accessSecret){
ksort($params);
$tmp = "";
foreach ($params as $key => $val) {
$tmp .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($val);
}
$tmp = trim($tmp,'&');
//echo $tmp;die;
$stringToSign = 'GET&%2F&'.$this->percentEncode($tmp);
$hmac = hash_hmac("sha1", $stringToSign,$accessSecret.'&', true);
return base64_encode($hmac);
}
function percentEncode($value = null)
{
$en = urlencode($value);
$en = str_replace("+", "%20", $en);
$en = str_replace("*", "%2A", $en);
$en = str_replace("%7E", "~", $en);
return $en;
}
function juhecurl($url, $params = false, $ispost = 0)
{
$httpInfo = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
if ($ispost) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_URL, $url);
} else {
if ($params) {
curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
} else {
curl_setopt($ch, CURLOPT_URL, $url);
}
}
$response = curl_exec($ch);
if ($response === FALSE) {
//echo "cURL Error: " . curl_error($ch);
return false;
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$httpInfo = array_merge($httpInfo, curl_getinfo($ch));
curl_close($ch);
return $response;
}
}
$class = Img::getInstance();
$str = !empty($_GET['url'])?$_GET['url']:'';
if(!$str){
$msg = array();
$msg['code'] = 500;
$msg['msg'] = '请上传图片';
}
//.'?x-oss-process=image/resize,p_40'
echo ($class->Ali($str.'?x-oss-process=image/resize,p_90'));//压缩图片质量