|
...
|
...
|
@@ -16,17 +16,6 @@ use Intervention\Image\Facades\Image; |
|
|
|
|
|
|
|
class ImageController extends Controller
|
|
|
|
{
|
|
|
|
public $upload_img = [
|
|
|
|
//设置静态缓存参数(304)
|
|
|
|
'header' => [
|
|
|
|
'Cache-Control' => 'max-age=2592000',
|
|
|
|
'Pragma' => 'cache',
|
|
|
|
'Expires' => "%Expires%", // cache 1 month
|
|
|
|
'etag' => "%etag%",
|
|
|
|
'Last-Modified' => "%Last-Modified%",
|
|
|
|
'Content-Description' => 'File Transfer',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
public $path = '';//路径
|
|
|
|
|
|
|
|
public $config = '';//存储默认配置
|
|
...
|
...
|
@@ -53,8 +42,6 @@ class ImageController extends Controller |
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->request = request();
|
|
|
|
$this->token = $this->request->header('token');
|
|
|
|
$this->cache = Cache::get($this->token);
|
|
|
|
$this->param = $this->request->all();
|
|
|
|
$this->config = config('filesystems.disks.upload');
|
|
|
|
$this->uploads = config('upload.default_image');
|
|
...
|
...
|
@@ -79,34 +66,37 @@ class ImageController extends Controller |
|
|
|
//查看缩略图是否存在
|
|
|
|
$filename = $this->config['root'] . '/' .$info['path'] . '_' . $w . '_' . $h;
|
|
|
|
if(is_file($filename)){
|
|
|
|
$last_modified_time = gmdate(time() + ((30 * 60 * 60 * 24))) . " GMT";
|
|
|
|
$header = str_replace(['%Expires%', "%etag%", '%Last-Modified%'],
|
|
|
|
[$last_modified_time, $hash . ':' . $w . '_' . $h . '_' . 1, $last_modified_time], $this->upload_img['header']);
|
|
|
|
$content = file_get_contents($filename);
|
|
|
|
$header['Content-Length'] = strlen($content);
|
|
|
|
}else{
|
|
|
|
$path = $this->config['root'].'/'.$info['path'];
|
|
|
|
if (!is_file($path)) {
|
|
|
|
$this->response('指定图片已被系统删除!', Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
$content = '';
|
|
|
|
$last_modified_time = gmdate(time() + ((30 * 60 * 60 * 24))) . " GMT";
|
|
|
|
$header = str_replace(['%Expires%', "%etag%", '%Last-Modified%'],
|
|
|
|
[$last_modified_time, $hash . ':' . $w . '_' . $h . '_' . 1, $last_modified_time], $this->upload_img['header']);
|
|
|
|
if ($w > 0 && $h > 0) {
|
|
|
|
$path = $this->cacheImage($info, $w, $h);
|
|
|
|
$content = file_get_contents($path);
|
|
|
|
$header['Content-Length'] = strlen($content);
|
|
|
|
} else {
|
|
|
|
$content = file_get_contents($path);
|
|
|
|
$header['Content-Length'] = strlen($content);
|
|
|
|
}
|
|
|
|
$content = $this->readImageContent($info,$w,$h);
|
|
|
|
$header['Content-Length'] = strlen($content);
|
|
|
|
}
|
|
|
|
$header['Content-Type'] = 'image/'.$info['type'];
|
|
|
|
$header['Content-Type'] = $info['mime'];
|
|
|
|
return response($content,200,$header);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @remark :缩略图不存在时获取图片
|
|
|
|
* @name :readImageContent
|
|
|
|
* @author :lyh
|
|
|
|
* @method :post
|
|
|
|
* @time :2023/7/27 9:26
|
|
|
|
*/
|
|
|
|
public function readImageContent($info,$w,$h)
|
|
|
|
{
|
|
|
|
$path = $this->config['root'] . '/' . $info['path'];
|
|
|
|
if (!is_file($path)) {
|
|
|
|
$this->response('指定图片已被系统删除!', Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
if ($w > 0 && $h > 0) {
|
|
|
|
$path = $this->cacheImage($info, $w, $h);
|
|
|
|
$content = file_get_contents($path);
|
|
|
|
} else {
|
|
|
|
$content = file_get_contents($path);
|
|
|
|
}
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @name :(图片上传)upload
|
|
|
|
* @author :lyh
|
|
...
|
...
|
@@ -130,7 +120,8 @@ class ImageController extends Controller |
|
|
|
}else{
|
|
|
|
$size = $files->getSize();
|
|
|
|
$image_type = $files->getClientOriginalExtension();
|
|
|
|
return $this->single($files,$size,$image_type);
|
|
|
|
$mime = $files->getMimeType();
|
|
|
|
return $this->single($files,$size,$image_type,$mime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
...
|
...
|
@@ -142,7 +133,7 @@ class ImageController extends Controller |
|
|
|
* @method :post
|
|
|
|
* @time :2023/6/17 16:30
|
|
|
|
*/
|
|
|
|
public function single(&$files,$size,$image_type){
|
|
|
|
public function single(&$files,$size,$image_type,$mime){
|
|
|
|
$hash = hash_file('md5', $files->getPathname());
|
|
|
|
//查看文件是否存在
|
|
|
|
$imageModel = new ImageModel();
|
|
...
|
...
|
@@ -162,7 +153,7 @@ class ImageController extends Controller |
|
|
|
return $this->response($files->getError(), Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location);
|
|
|
|
$this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location,$mime);
|
|
|
|
return $this->response('图片资源',Code::SUCCESS,$this->responseData($hash));
|
|
|
|
}
|
|
|
|
|
|
...
|
...
|
@@ -173,7 +164,7 @@ class ImageController extends Controller |
|
|
|
* @method :post
|
|
|
|
* @time :2023/7/19 16:38
|
|
|
|
*/
|
|
|
|
public function saveMysql(&$imageModel,$size,$image_type,$fileName,$hash,$is_cos = 0){
|
|
|
|
public function saveMysql(&$imageModel,$size,$image_type,$fileName,$hash,$is_cos = 0,$mime = ''){
|
|
|
|
$data = [
|
|
|
|
'path' => $this->path.'/'.$fileName,
|
|
|
|
'created_at' => date('Y-m-d H:i:s',time()),
|
|
...
|
...
|
@@ -181,7 +172,8 @@ class ImageController extends Controller |
|
|
|
'hash' => $hash,
|
|
|
|
'type'=>$image_type,
|
|
|
|
'refer'=>$this->param['refer'] ?? 1,
|
|
|
|
'is_cos'=>$is_cos
|
|
|
|
'is_cos'=>$is_cos,
|
|
|
|
'mime'=>$mime
|
|
|
|
];
|
|
|
|
$rs = $imageModel->add($data);
|
|
|
|
if ($rs === false) {
|
|
...
|
...
|
@@ -219,6 +211,7 @@ class ImageController extends Controller |
|
|
|
foreach ($files as $file) {
|
|
|
|
$size = $file->getSize();
|
|
|
|
$image_type = $file->getClientOriginalExtension();
|
|
|
|
$mime = $file->getMimeType();
|
|
|
|
$imageModel = new ImageModel();
|
|
|
|
$hash = hash_file('md5', $file->getPathname());
|
|
|
|
$image_hash = $imageModel->read(['hash'=>$hash]);
|
|
...
|
...
|
@@ -239,7 +232,7 @@ class ImageController extends Controller |
|
|
|
}
|
|
|
|
}
|
|
|
|
//批量存储
|
|
|
|
$this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location);
|
|
|
|
$this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location,$mime);
|
|
|
|
$data[] = $this->responseData($hash);
|
|
|
|
}
|
|
|
|
$this->response('图片资源',Code::SUCCESS,$data);
|
|
...
|
...
|
@@ -309,7 +302,7 @@ class ImageController extends Controller |
|
|
|
$imageModel = new ImageModel();
|
|
|
|
$lists = $imageModel->list($this->param,$order = 'id');
|
|
|
|
foreach ($lists as $k => $v){
|
|
|
|
$v['image_link'] = $this->getImageUrl($v['hash']);
|
|
|
|
$v['image_link'] = getImageUrl($v['hash']);
|
|
|
|
$lists[$k] = $v;
|
|
|
|
}
|
|
|
|
$this->response('success',Code::SUCCESS,$lists);
|
|
...
|
...
|
@@ -328,39 +321,21 @@ class ImageController extends Controller |
|
|
|
$this->path = $this->uploads['path_a'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m');
|
|
|
|
}else{
|
|
|
|
//B端上传
|
|
|
|
if(isset($this->param['upload_method']) && $this->param['upload_method'] == 1){
|
|
|
|
//强制上传本地配置
|
|
|
|
$this->upload_location = 0;
|
|
|
|
}else{
|
|
|
|
$this->token = $this->request->header('token');
|
|
|
|
$this->cache = Cache::get($this->token);
|
|
|
|
if(!isset($this->param['upload_method'])){
|
|
|
|
//根据项目上传标识区分上传到cos/本地
|
|
|
|
$projectModel = new Project();
|
|
|
|
$project_info = $projectModel->read(['id'=>$this->cache['project_id']],['upload_location']);
|
|
|
|
$this->upload_location = $project_info['upload_location'];
|
|
|
|
if ($project_info['project_location'] == 0) {//不为普通项目时 上传到本地服务器
|
|
|
|
$this->upload_location = 1;//上传到cos
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @remark :获取图片链接
|
|
|
|
* @name :getImageUrl
|
|
|
|
* @author :lyh
|
|
|
|
* @method :post
|
|
|
|
* @time :2023/7/20 16:46
|
|
|
|
*/
|
|
|
|
public function getImageUrl($hash){
|
|
|
|
$imageModel = new ImageModel();
|
|
|
|
$info = $imageModel->read(['hash'=>$hash]);
|
|
|
|
if($info['is_cos'] == 1){
|
|
|
|
$cos = new CosService();
|
|
|
|
$url = $cos->getImageUrl($info['path']);
|
|
|
|
}else{
|
|
|
|
$url = url('a/image/'.$info['hash']);
|
|
|
|
}
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @remark :统一返回参数处理
|
|
|
|
* @name :responseData
|
|
|
|
* @author :lyh
|
|
...
|
...
|
@@ -370,7 +345,7 @@ class ImageController extends Controller |
|
|
|
public function responseData($hash){
|
|
|
|
$data = [
|
|
|
|
'image'=>$hash,
|
|
|
|
'image_link'=>$this->getImageUrl($hash),
|
|
|
|
'image_link'=>getImageUrl($hash),
|
|
|
|
'image_download'=>url('a/downLoad/images?hash='.$hash),
|
|
|
|
];
|
|
|
|
return $data;
|
...
|
...
|
|