|
...
|
...
|
@@ -3,14 +3,16 @@ |
|
|
|
namespace App\Http\Controllers\File;
|
|
|
|
|
|
|
|
use App\Enums\Common\Code;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Http\Controllers\type;
|
|
|
|
use App\Models\File\Image as ImageModel;
|
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use Intervention\Image\Facades\Image;
|
|
|
|
|
|
|
|
class ImageController
|
|
|
|
class ImageController extends Controller
|
|
|
|
{
|
|
|
|
public $upload_img = [
|
|
|
|
//设置静态缓存参数(304)
|
|
...
|
...
|
@@ -25,20 +27,45 @@ class ImageController |
|
|
|
];
|
|
|
|
const TYPE = 1;
|
|
|
|
|
|
|
|
public $path = '';
|
|
|
|
public $path = '';//路径
|
|
|
|
|
|
|
|
public $config = '';
|
|
|
|
public $config = '';//存储默认配置
|
|
|
|
|
|
|
|
public $thr_path = '';
|
|
|
|
public $request = '';//request
|
|
|
|
|
|
|
|
public $request = '';
|
|
|
|
public $param = '';//参数
|
|
|
|
|
|
|
|
public $token = '';//token
|
|
|
|
|
|
|
|
public $cache = '';//缓存数据
|
|
|
|
|
|
|
|
public $image_type = [
|
|
|
|
1 => 'image_product',
|
|
|
|
2 => 'image_news',
|
|
|
|
3 => 'image_blog',
|
|
|
|
4 => 'image_other',
|
|
|
|
];
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->request = request();
|
|
|
|
$this->token = $this->request->header('token');
|
|
|
|
$this->cache = Cache::get($this->token);
|
|
|
|
if(!isset($this->token) || empty($this->cache)){
|
|
|
|
$this->response('请登录后上传',Code::USER_LOGIN_ERROE);
|
|
|
|
}
|
|
|
|
$this->param = $this->request->all();
|
|
|
|
$this->config = config('filesystems.disks.upload');
|
|
|
|
$this->uploads = config('upload.default_image');
|
|
|
|
$this->path = $this->config['root'].$this->uploads['path'].'/';
|
|
|
|
//上传路径设置
|
|
|
|
if(!isset($this->param['image_type'])){
|
|
|
|
$this->response('类型不能为空',Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
if(isset($this->param['refer_type']) && $this->param['refer_type'] == 1){
|
|
|
|
$this->path = $this->uploads['path_a'].'/'.$this->image_type[$this->param['image_type']].'/'.date('Y-m');
|
|
|
|
}else{
|
|
|
|
$this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['image_type']].'/'.date('Y-m');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
...
|
...
|
@@ -61,7 +88,7 @@ class ImageController |
|
|
|
$this->response('指定图片不存在!', Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
//查看缩略图是否存在
|
|
|
|
$filename = $this->path . $info['hash'] . $w . '_' . $h;
|
|
|
|
$filename = $this->config['url'].'/'.$this->path . '/' . $info['hash'] . $w . '_' . $h;
|
|
|
|
if(is_file($filename)){
|
|
|
|
$last_modified_time = gmdate(time() + ((30 * 60 * 60 * 24))) . " GMT";
|
|
|
|
$header = str_replace(['%Expires%', "%etag%", '%Last-Modified%'],
|
|
...
|
...
|
@@ -69,7 +96,7 @@ class ImageController |
|
|
|
$content = file_get_contents($filename);
|
|
|
|
$header['Content-Length'] = strlen($content);
|
|
|
|
}else{
|
|
|
|
$path = $this->path.$info['path'];
|
|
|
|
$path = $this->config['url'].'/'.$info['path'];
|
|
|
|
if (!is_file($path)) {
|
|
|
|
$this->response('指定图片已被系统删除!', Code::USER_ERROR);
|
|
|
|
}
|
|
...
|
...
|
@@ -106,7 +133,7 @@ class ImageController |
|
|
|
if (empty($files)) {
|
|
|
|
$this->response('没有上传的文件!', 400);
|
|
|
|
}
|
|
|
|
$type = $this->request->post('type', 'single');
|
|
|
|
$type = $this->param['type'];
|
|
|
|
if ($type == 'multi') {
|
|
|
|
return $this->multi($files);
|
|
|
|
} else {
|
|
...
|
...
|
@@ -130,19 +157,19 @@ class ImageController |
|
|
|
if($image_hash !== false){
|
|
|
|
return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
|
|
|
|
}
|
|
|
|
$url = $this->path;
|
|
|
|
$url = $this->config['url'].$this->path;
|
|
|
|
$fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
|
|
|
|
$res = $files->move($url,$fileName);
|
|
|
|
if ($res === false) {
|
|
|
|
return $this->response($files->getError(), Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
$data = [
|
|
|
|
'path' => $fileName,
|
|
|
|
'path' => $this->path.'/'.$fileName,
|
|
|
|
'created_at' => date('Y-m-d H:i:s',time()),
|
|
|
|
'size' => $res->getSize(),
|
|
|
|
'hash' => $hash,
|
|
|
|
'type'=>$files->getClientOriginalExtension(),
|
|
|
|
'refer'=>$this->request->post('refer') ?? '',
|
|
|
|
'refer'=>$this->param['refer'] ?? '',
|
|
|
|
];
|
|
|
|
$rs = $imageModel->add($data);
|
|
|
|
if ($rs === false) {
|
|
...
|
...
|
@@ -163,7 +190,7 @@ class ImageController |
|
|
|
*/
|
|
|
|
private function cacheImage($info, $w, $h) {
|
|
|
|
$path = $info['path'];
|
|
|
|
$filename = $this->path . $info['hash'] . $w . '_' . $h;
|
|
|
|
$filename = $this->config['url'] . $this->path . $info['hash'] . $w . '_' . $h;
|
|
|
|
Image::make($path)->resize($w, $h)->save($filename);
|
|
|
|
return $filename;
|
|
|
|
}
|
|
...
|
...
|
@@ -191,20 +218,20 @@ class ImageController |
|
|
|
$data[] = ['image'=>$hash];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$url = $this->path;
|
|
|
|
$url = $this->config['url'].$this->path;
|
|
|
|
$fileName = uniqid().rand(10000,99999).'.'.$file->getClientOriginalExtension();
|
|
|
|
$res = $file->move($url,$fileName);
|
|
|
|
if ($res === false) {
|
|
|
|
return $this->response($file->getError(), Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
$save_data[] = [
|
|
|
|
'path' => $fileName,
|
|
|
|
'path' => $this->path.'/'.$fileName,
|
|
|
|
'created_at' => date('Y-m-d H:i:s',time()),
|
|
|
|
'updated_at'=>date('Y-m-d H:i:s',time()),
|
|
|
|
'size' => $res->getSize(),
|
|
|
|
'hash' => $hash,
|
|
|
|
'type'=>$file->getClientOriginalExtension(),
|
|
|
|
'refer'=>$this->request->post('refer') ?? '',
|
|
|
|
'refer'=>$this->param['refer'] ?? '',
|
|
|
|
];
|
|
|
|
$data[] = ['image'=>$hash];
|
|
|
|
}
|
|
...
|
...
|
@@ -284,4 +311,5 @@ class ImageController |
|
|
|
$lists = $imageModel->list([],$order = 'id',['id','hash','type','path','created_at']);
|
|
|
|
$this->response('success',Code::SUCCESS,$lists);
|
|
|
|
}
|
|
|
|
|
|
|
|
} |
...
|
...
|
|