作者 lyh

gx

@@ -108,7 +108,8 @@ class FileController @@ -108,7 +108,8 @@ class FileController
108 } else { 108 } else {
109 $size = $files->getSize(); 109 $size = $files->getSize();
110 $file_type = $files->getClientOriginalExtension(); 110 $file_type = $files->getClientOriginalExtension();
111 - return $this->single($files,$size,$file_type); 111 + $mime = $files->getMimeType();
  112 + return $this->single($files,$size,$file_type,$mime);
112 } 113 }
113 } 114 }
114 115
@@ -119,7 +120,7 @@ class FileController @@ -119,7 +120,7 @@ class FileController
119 * @method :post 120 * @method :post
120 * @time :2023/6/17 16:32 121 * @time :2023/6/17 16:32
121 */ 122 */
122 - public function single(&$files,$size,$file_type){ 123 + public function single(&$files,$size,$file_type,$mime = ''){
123 $hash = hash_file('md5', $files->getPathname()); 124 $hash = hash_file('md5', $files->getPathname());
124 //查看文件是否存在 125 //查看文件是否存在
125 $fileModel = new File(); 126 $fileModel = new File();
@@ -139,7 +140,7 @@ class FileController @@ -139,7 +140,7 @@ class FileController
139 return $this->response($files->getError(), Code::USER_ERROR); 140 return $this->response($files->getError(), Code::USER_ERROR);
140 } 141 }
141 } 142 }
142 - $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$this->upload_location); 143 + $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$this->upload_location,$mime);
143 return $this->response('资源',Code::SUCCESS,$this->responseData($fileModel,$hash)); 144 return $this->response('资源',Code::SUCCESS,$this->responseData($fileModel,$hash));
144 } 145 }
145 146
@@ -150,7 +151,7 @@ class FileController @@ -150,7 +151,7 @@ class FileController
150 * @method :post 151 * @method :post
151 * @time :2023/7/19 16:38 152 * @time :2023/7/19 16:38
152 */ 153 */
153 - public function saveMysql(&$fileModel,$size,$image_type,$fileName,$hash,$is_cos = 0){ 154 + public function saveMysql(&$fileModel,$size,$image_type,$fileName,$hash,$is_cos = 0,$mime = ''){
154 $data = [ 155 $data = [
155 'path' => $this->path.'/'.$fileName, 156 'path' => $this->path.'/'.$fileName,
156 'created_at' => date('Y-m-d H:i:s',time()), 157 'created_at' => date('Y-m-d H:i:s',time()),
@@ -158,7 +159,8 @@ class FileController @@ -158,7 +159,8 @@ class FileController
158 'hash' => $hash, 159 'hash' => $hash,
159 'type'=>$image_type, 160 'type'=>$image_type,
160 'refer'=>$this->param['refer'] ?? 1, 161 'refer'=>$this->param['refer'] ?? 1,
161 - 'is_cos'=>$is_cos 162 + 'is_cos'=>$is_cos,
  163 + 'mime'=>$mime,
162 ]; 164 ];
163 $rs = $fileModel->add($data); 165 $rs = $fileModel->add($data);
164 if ($rs === false) { 166 if ($rs === false) {
@@ -198,7 +200,8 @@ class FileController @@ -198,7 +200,8 @@ class FileController
198 } 200 }
199 } 201 }
200 $size = $file->getSize(); 202 $size = $file->getSize();
201 - $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$this->upload_location); 203 + $mime = $file->getMimeType();
  204 + $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$this->upload_location,$mime);
202 $data[] = $this->responseData($fileModel,$hash); 205 $data[] = $this->responseData($fileModel,$hash);
203 } 206 }
204 $this->response('资源',Code::SUCCESS,$data); 207 $this->response('资源',Code::SUCCESS,$data);
@@ -16,17 +16,6 @@ use Intervention\Image\Facades\Image; @@ -16,17 +16,6 @@ use Intervention\Image\Facades\Image;
16 16
17 class ImageController extends Controller 17 class ImageController extends Controller
18 { 18 {
19 - public $upload_img = [  
20 - //设置静态缓存参数(304)  
21 - 'header' => [  
22 - 'Cache-Control' => 'max-age=2592000',  
23 - 'Pragma' => 'cache',  
24 - 'Expires' => "%Expires%", // cache 1 month  
25 - 'etag' => "%etag%",  
26 - 'Last-Modified' => "%Last-Modified%",  
27 - 'Content-Description' => 'File Transfer',  
28 - ],  
29 - ];  
30 public $path = '';//路径 19 public $path = '';//路径
31 20
32 public $config = '';//存储默认配置 21 public $config = '';//存储默认配置
@@ -79,34 +68,37 @@ class ImageController extends Controller @@ -79,34 +68,37 @@ class ImageController extends Controller
79 //查看缩略图是否存在 68 //查看缩略图是否存在
80 $filename = $this->config['root'] . '/' .$info['path'] . '_' . $w . '_' . $h; 69 $filename = $this->config['root'] . '/' .$info['path'] . '_' . $w . '_' . $h;
81 if(is_file($filename)){ 70 if(is_file($filename)){
82 - $last_modified_time = gmdate(time() + ((30 * 60 * 60 * 24))) . " GMT";  
83 - $header = str_replace(['%Expires%', "%etag%", '%Last-Modified%'],  
84 - [$last_modified_time, $hash . ':' . $w . '_' . $h . '_' . 1, $last_modified_time], $this->upload_img['header']);  
85 $content = file_get_contents($filename); 71 $content = file_get_contents($filename);
86 $header['Content-Length'] = strlen($content); 72 $header['Content-Length'] = strlen($content);
87 }else{ 73 }else{
88 - $path = $this->config['root'].'/'.$info['path'];  
89 - if (!is_file($path)) {  
90 - $this->response('指定图片已被系统删除!', Code::USER_ERROR);  
91 - }  
92 - $content = '';  
93 - $last_modified_time = gmdate(time() + ((30 * 60 * 60 * 24))) . " GMT";  
94 - $header = str_replace(['%Expires%', "%etag%", '%Last-Modified%'],  
95 - [$last_modified_time, $hash . ':' . $w . '_' . $h . '_' . 1, $last_modified_time], $this->upload_img['header']);  
96 - if ($w > 0 && $h > 0) {  
97 - $path = $this->cacheImage($info, $w, $h);  
98 - $content = file_get_contents($path);  
99 - $header['Content-Length'] = strlen($content);  
100 - } else {  
101 - $content = file_get_contents($path);  
102 - $header['Content-Length'] = strlen($content);  
103 - } 74 + $content = $this->readImageContent($info,$w,$h);
  75 + $header['Content-Length'] = strlen($content);
104 } 76 }
105 - $header['Content-Type'] = 'image/'.$info['type']; 77 + $header['Content-Type'] = $info['mime'];
106 return response($content,200,$header); 78 return response($content,200,$header);
107 } 79 }
108 80
109 - 81 + /**
  82 + * @remark :缩略图不存在时获取图片
  83 + * @name :readImageContent
  84 + * @author :lyh
  85 + * @method :post
  86 + * @time :2023/7/27 9:26
  87 + */
  88 + public function readImageContent($info,$w,$h)
  89 + {
  90 + $path = $this->config['root'] . '/' . $info['path'];
  91 + if (!is_file($path)) {
  92 + $this->response('指定图片已被系统删除!', Code::USER_ERROR);
  93 + }
  94 + if ($w > 0 && $h > 0) {
  95 + $path = $this->cacheImage($info, $w, $h);
  96 + $content = file_get_contents($path);
  97 + } else {
  98 + $content = file_get_contents($path);
  99 + }
  100 + return $content;
  101 + }
110 /** 102 /**
111 * @name :(图片上传)upload 103 * @name :(图片上传)upload
112 * @author :lyh 104 * @author :lyh
@@ -130,7 +122,8 @@ class ImageController extends Controller @@ -130,7 +122,8 @@ class ImageController extends Controller
130 }else{ 122 }else{
131 $size = $files->getSize(); 123 $size = $files->getSize();
132 $image_type = $files->getClientOriginalExtension(); 124 $image_type = $files->getClientOriginalExtension();
133 - return $this->single($files,$size,$image_type); 125 + $mime = $files->getMimeType();
  126 + return $this->single($files,$size,$image_type,$mime);
134 } 127 }
135 } 128 }
136 129
@@ -142,7 +135,7 @@ class ImageController extends Controller @@ -142,7 +135,7 @@ class ImageController extends Controller
142 * @method :post 135 * @method :post
143 * @time :2023/6/17 16:30 136 * @time :2023/6/17 16:30
144 */ 137 */
145 - public function single(&$files,$size,$image_type){ 138 + public function single(&$files,$size,$image_type,$mime){
146 $hash = hash_file('md5', $files->getPathname()); 139 $hash = hash_file('md5', $files->getPathname());
147 //查看文件是否存在 140 //查看文件是否存在
148 $imageModel = new ImageModel(); 141 $imageModel = new ImageModel();
@@ -162,7 +155,7 @@ class ImageController extends Controller @@ -162,7 +155,7 @@ class ImageController extends Controller
162 return $this->response($files->getError(), Code::USER_ERROR); 155 return $this->response($files->getError(), Code::USER_ERROR);
163 } 156 }
164 } 157 }
165 - $this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location); 158 + $this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location,$mime);
166 return $this->response('图片资源',Code::SUCCESS,$this->responseData($hash)); 159 return $this->response('图片资源',Code::SUCCESS,$this->responseData($hash));
167 } 160 }
168 161
@@ -173,7 +166,7 @@ class ImageController extends Controller @@ -173,7 +166,7 @@ class ImageController extends Controller
173 * @method :post 166 * @method :post
174 * @time :2023/7/19 16:38 167 * @time :2023/7/19 16:38
175 */ 168 */
176 - public function saveMysql(&$imageModel,$size,$image_type,$fileName,$hash,$is_cos = 0){ 169 + public function saveMysql(&$imageModel,$size,$image_type,$fileName,$hash,$is_cos = 0,$mime = ''){
177 $data = [ 170 $data = [
178 'path' => $this->path.'/'.$fileName, 171 'path' => $this->path.'/'.$fileName,
179 'created_at' => date('Y-m-d H:i:s',time()), 172 'created_at' => date('Y-m-d H:i:s',time()),
@@ -181,7 +174,8 @@ class ImageController extends Controller @@ -181,7 +174,8 @@ class ImageController extends Controller
181 'hash' => $hash, 174 'hash' => $hash,
182 'type'=>$image_type, 175 'type'=>$image_type,
183 'refer'=>$this->param['refer'] ?? 1, 176 'refer'=>$this->param['refer'] ?? 1,
184 - 'is_cos'=>$is_cos 177 + 'is_cos'=>$is_cos,
  178 + 'mime'=>$mime
185 ]; 179 ];
186 $rs = $imageModel->add($data); 180 $rs = $imageModel->add($data);
187 if ($rs === false) { 181 if ($rs === false) {
@@ -219,6 +213,7 @@ class ImageController extends Controller @@ -219,6 +213,7 @@ class ImageController extends Controller
219 foreach ($files as $file) { 213 foreach ($files as $file) {
220 $size = $file->getSize(); 214 $size = $file->getSize();
221 $image_type = $file->getClientOriginalExtension(); 215 $image_type = $file->getClientOriginalExtension();
  216 + $mime = $file->getMimeType();
222 $imageModel = new ImageModel(); 217 $imageModel = new ImageModel();
223 $hash = hash_file('md5', $file->getPathname()); 218 $hash = hash_file('md5', $file->getPathname());
224 $image_hash = $imageModel->read(['hash'=>$hash]); 219 $image_hash = $imageModel->read(['hash'=>$hash]);
@@ -239,7 +234,7 @@ class ImageController extends Controller @@ -239,7 +234,7 @@ class ImageController extends Controller
239 } 234 }
240 } 235 }
241 //批量存储 236 //批量存储
242 - $this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location); 237 + $this->saveMysql($imageModel,$size,$image_type,$fileName,$hash,$this->upload_location,$mime);
243 $data[] = $this->responseData($hash); 238 $data[] = $this->responseData($hash);
244 } 239 }
245 $this->response('图片资源',Code::SUCCESS,$data); 240 $this->response('图片资源',Code::SUCCESS,$data);