作者 lyh

gx

... ... @@ -77,6 +77,11 @@ class ImageController extends Controller
if ($info === false) {
$this->response('指定图片不存在!', Code::USER_ERROR);
}
//获取cos链接
if($info['cos'] == 1){
$cos = new CosService();
return $cos->getImageUrl($info['path']);
}
//查看缩略图是否存在
$filename = $this->config['root'] . '/' .$info['path'] . '_' . $w . '_' . $h;
if(is_file($filename)){
... ... @@ -107,6 +112,7 @@ class ImageController extends Controller
return response($content,200,$header);
}
/**
* @name :(图片上传)upload
* @author :lyh
... ... @@ -155,6 +161,7 @@ class ImageController extends Controller
//同步数据到cos
if($this->upload_location == 1){
$cosService = new CosService();
$is_cos = 1;//上传到cos
$cosService->uploadFile($files,$this->path,$fileName);
}else{
$res = $files->move($url,$fileName);
... ... @@ -162,7 +169,7 @@ class ImageController extends Controller
return $this->response($files->getError(), Code::USER_ERROR);
}
}
$this->saveMysql($imageModel,$size,$image_type,$fileName,$hash);
$this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$is_cos);
return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
}
... ... @@ -173,7 +180,7 @@ class ImageController extends Controller
* @method :post
* @time :2023/7/19 16:38
*/
public function saveMysql(&$imageModel,$size,$image_type,$fileName,$hash){
public function saveMysql(&$imageModel,$size,$image_type,$fileName,$hash,$is_cos = 0){
$data = [
'path' => $this->path.'/'.$fileName,
'created_at' => date('Y-m-d H:i:s',time()),
... ... @@ -181,6 +188,7 @@ class ImageController extends Controller
'hash' => $hash,
'type'=>$image_type,
'refer'=>$this->param['refer'] ?? 1,
'is_cos'=>$is_cos
];
$rs = $imageModel->add($data);
if ($rs === false) {
... ... @@ -231,6 +239,7 @@ class ImageController extends Controller
//同步数据到cos
if($this->upload_location == 1){
$cosService = new CosService();
$is_cos = 1;//上传到cos
$cosService->uploadFile($file,$this->path,$fileName);
}else{
$res = $file->move($url,$fileName);
... ... @@ -246,6 +255,7 @@ class ImageController extends Controller
'hash' => $hash,
'type'=>$image_type,
'refer'=>$this->param['refer'] ?? 0,
'is_cos'=>$is_cos ?? 0
];
$data[] = ['image'=>$hash];
}
... ... @@ -253,16 +263,30 @@ class ImageController extends Controller
return $this->response('图片资源',Code::SUCCESS,$data);
}
//下载
/**
* @param $filename
* @remark :下载
* @name :download
* @author :lyh
* @method :post
* @time :2023/7/19 17:59
*/
public function download($filename){
$path = Storage::path($filename);
return response()->download($path,time().rand(1,100000));
}
/**
* @name 统一返回参数
* @return JsonResponse
* @author :liyuhang
* @method
* @param $msg
* @param string $code
* @param $data
* @param $result_code
* @param $type
* @remark :统一返回
* @name :response
* @author :lyh
* @method :post
* @time :2023/7/19 17:59
*/
public function response($msg = null,string $code = Code::SUCCESS,$data = [],$result_code = 200,$type = 'application/json'): JsonResponse
{
... ...
... ... @@ -58,8 +58,6 @@ class CosService
],
]);
$imageUrl = $cosClient->getObjectUrl($cos['bucket'], basename($image_name));
return response()->json([
'image_url' => $imageUrl,
]);
return $imageUrl;
}
}
... ...