作者 lyh

gx

... ... @@ -165,7 +165,7 @@ class ImageController extends Controller
if ($rs === false) {
return $this->response('添加失败', Code::USER_ERROR);
}
return $this->uploadCos($this->path,$fileName);
return $this->uploadCos($this->path,$fileName,$data['type']);
return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
}
... ... @@ -327,8 +327,8 @@ class ImageController extends Controller
* @method :post
* @time :2023/7/18 17:38
*/
public function uploadCos($path,$fileName){
public function uploadCos($path,$fileName,$image_type){
$txCos = new TencentCosService();
return $txCos->upload_image($path,$fileName);
return $txCos->upload_image($path,$fileName,$image_type);
}
}
... ...
... ... @@ -40,22 +40,21 @@ class TencentCosService extends BaseService
* @method :post
* @time :2023/7/18 16:56
*/
public function upload_image($path,$fileName){
public function upload_image($path,$fileName,$image_type){
// 构建请求URL
$pathname = '/'.$this->config['bucket'].$path.'/'.$fileName;
$url = 'https://'.$this->config['bucket'].'.cos.'.$this->config['cosRegion'].'.myqcloud.com'.$pathname;
$date = gmdate('D, d M Y H:i:s T');
$signKey = hash_hmac('sha1', $date, $this->config['secretKey']);
$method = 'PUT';
$signature = base64_encode(hash_hmac('sha1', "{$method}\n\n\n\n", $signKey, true));
$headers = array(
'Authorization: QCloud ' . $this->config['secretId'] . ':' . $signature,
'Date: ' . $date,
'Host: ' . $this->config['bucket'] . '.cos.' . $this->config['cosRegion'] . '.myqcloud.com'
);
$httpVerb = 'PUT';
$contentMd5 = '';
$contentType = 'image/'.$image_type;
$date = gmdate('D, d M Y H:i:s \G\M\T');
$filePath = $path.'/'.$fileName;
$expiredTime = time() + 3600;
$stringToSign = "{$httpVerb}\n{$contentMd5}\n{$contentType}\n{$date}\n/{$this->config['bucket']}/{$filePath}";
$sign = base64_encode(hash_hmac('sha1', $stringToSign, $this->config['secretKey'], true));
$authorization = "q-sign-algorithm=sha1&q-ak={$this->config['secretId']}&q-sign-time={$expiredTime}&q-key-time={$expiredTime}&q-header-list=&q-url-param-list=&q-signature={$sign}";
$url = "https://{$this->config['bucket']}.cos.{$this->config['cosRegion']}.myqcloud.com{$filePath}";
// 打开文件流
$filePath = config('filesystems.disks.upload')['root'].$path.'/'.$fileName;
return $this->http_put($url,$filePath,$headers);
$filePathUrl = config('filesystems.disks.upload')['root'].$path.'/'.$fileName;
return $this->http_put($url,$contentType,$authorization,$date,$filePathUrl);
}
/**
... ... @@ -65,24 +64,28 @@ class TencentCosService extends BaseService
* @method :post
* @time :2023/7/18 17:06
*/
public function http_put($url,$filePath,$headers){
public function http_put($url,$contentType,$authorization,$date,$filePath){
// 执行请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_INFILE, fopen($filePath, 'rb'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: ' . $contentType,
'Authorization: ' . $authorization,
'Date: ' . $date
));
curl_setopt($ch, CURLOPT_INFILE, fopen($filePath, 'r'));
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filePath));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
// 检查响应结果
if ($response) {
var_dump($response);
echo '图片上传成功';
die();
return 1;
} else {
echo '图片上传失败';
return 0;
}
}
... ...