|
...
|
...
|
@@ -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 = '';//存储默认配置
|
|
...
|
...
|
@@ -79,34 +68,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'];
|
|
|
|
$content = $this->readImageContent($info,$w,$h);
|
|
|
|
$header['Content-Length'] = strlen($content);
|
|
|
|
}
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
$header['Content-Type'] = 'image/'.$info['type'];
|
|
|
|
return response($content,200,$header);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name :(图片上传)upload
|
|
|
|
* @author :lyh
|
|
...
|
...
|
@@ -130,7 +122,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 +135,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 +155,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 +166,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 +174,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 +213,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 +234,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);
|
...
|
...
|
|