|
1
|
-<?php
|
|
|
|
2
|
-
|
|
|
|
3
|
-namespace App\Http\Controllers;
|
|
|
|
4
|
-
|
|
|
|
5
|
-use App\Enums\Common\Code;
|
|
|
|
6
|
-use App\Models\Image as ImageModel;
|
|
|
|
7
|
-use Illuminate\Http\Exceptions\HttpResponseException;
|
|
|
|
8
|
-use Illuminate\Http\JsonResponse;
|
|
|
|
9
|
-use Illuminate\Http\Request;
|
|
|
|
10
|
-use Illuminate\Support\Facades\DB;
|
|
|
|
11
|
-use Illuminate\Support\Facades\Storage;
|
|
|
|
12
|
-use Intervention\Image\Facades\Image;
|
|
|
|
13
|
-
|
|
|
|
14
|
-class ImageLogic
|
|
|
|
15
|
-{
|
|
|
|
16
|
- public $path = '';
|
|
|
|
17
|
-
|
|
|
|
18
|
- public $request = '';
|
|
|
|
19
|
-
|
|
|
|
20
|
- public function __construct(Request $request)
|
|
|
|
21
|
- {
|
|
|
|
22
|
- $this->request = $request;
|
|
|
|
23
|
- $this->path = config('filesystems.disks')['upload']['root'].config('upload');
|
|
|
|
24
|
- }
|
|
|
|
25
|
-
|
|
|
|
26
|
- /**
|
|
|
|
27
|
- * 图片上传
|
|
|
|
28
|
- */
|
|
|
|
29
|
- public function upload() {
|
|
|
|
30
|
- $this->request->validate([
|
|
|
|
31
|
- 'image'=>['required'],
|
|
|
|
32
|
- ],[
|
|
|
|
33
|
- 'image.required'=>'图片必须填写',
|
|
|
|
34
|
- ]);
|
|
|
|
35
|
- $files = $this->request->file('image');
|
|
|
|
36
|
- if (empty($files)) {
|
|
|
|
37
|
- $this->response('没有上传的文件!', 400);
|
|
|
|
38
|
- }
|
|
|
|
39
|
- $type = $this->request->post('type', 'single');
|
|
|
|
40
|
- if ($type == 'multi') {
|
|
|
|
41
|
- return $this->multi($files);
|
|
|
|
42
|
- } else {
|
|
|
|
43
|
- return $this->single($files);
|
|
|
|
44
|
- }
|
|
|
|
45
|
- }
|
|
|
|
46
|
- /**
|
|
|
|
47
|
- * @name :上传图片
|
|
|
|
48
|
- * @return void
|
|
|
|
49
|
- * @author :liyuhang
|
|
|
|
50
|
- * @method
|
|
|
|
51
|
- */
|
|
|
|
52
|
- public function single($files){
|
|
|
|
53
|
- $hash = hash_file('md5', $files->getPathname());
|
|
|
|
54
|
- $url = $this->path;
|
|
|
|
55
|
- $filename = date('ymdHis').rand(10000,99999);
|
|
|
|
56
|
- $res = $this->request->file('image')->move($url,$filename);
|
|
|
|
57
|
- if ($res === false) {
|
|
|
|
58
|
- return $this->response($files->getError(), Code::USER_ERROR);
|
|
|
|
59
|
- }
|
|
|
|
60
|
- $imageModel = new ImageModel();
|
|
|
|
61
|
- $data = [
|
|
|
|
62
|
- 'path' => $url.$filename,
|
|
|
|
63
|
- 'created_at' => date('Y-m-d H:i:s',time()),
|
|
|
|
64
|
- 'size' => $res->getSize(),
|
|
|
|
65
|
- 'hash' => $hash.$filename,
|
|
|
|
66
|
- 'type'=>$files->getClientOriginalExtension(),
|
|
|
|
67
|
- ];
|
|
|
|
68
|
- $rs = $imageModel->add($data);
|
|
|
|
69
|
- if ($rs === false) {
|
|
|
|
70
|
- return $this->response('添加失败', Code::USER_ERROR);
|
|
|
|
71
|
- }
|
|
|
|
72
|
- return $hash.$filename;
|
|
|
|
73
|
- }
|
|
|
|
74
|
-
|
|
|
|
75
|
- /**
|
|
|
|
76
|
- * @name :(删除资源及对应表数据)del
|
|
|
|
77
|
- * @author :lyh
|
|
|
|
78
|
- * @method :post
|
|
|
|
79
|
- * @time :2023/5/4 14:57
|
|
|
|
80
|
- */
|
|
|
|
81
|
- public function del($hash){
|
|
|
|
82
|
- DB::beginTransaction();
|
|
|
|
83
|
- $imageModel = new ImageModel();
|
|
|
|
84
|
- try {
|
|
|
|
85
|
- if(is_array($hash)){
|
|
|
|
86
|
- foreach ($hash as $k => $v){
|
|
|
|
87
|
- $this->del($v);
|
|
|
|
88
|
- }
|
|
|
|
89
|
- }else{
|
|
|
|
90
|
- $info = $imageModel->read(['hash'=>$hash]);
|
|
|
|
91
|
- if($info !== false){
|
|
|
|
92
|
- shell_exec('sudo rm -rf '.$info['path']. ' '.$this->path . $info['hash'] .'*');
|
|
|
|
93
|
- }
|
|
|
|
94
|
- }
|
|
|
|
95
|
- $imageModel->del(['hash'=>['in',$hash]]);
|
|
|
|
96
|
- DB::commit();
|
|
|
|
97
|
- }catch (\Exception $e){
|
|
|
|
98
|
- DB::rollBack();
|
|
|
|
99
|
- $this->response('error',Code::USER_ERROR);
|
|
|
|
100
|
- }
|
|
|
|
101
|
- }
|
|
|
|
102
|
- /**
|
|
|
|
103
|
- * 生成缩略图缓存
|
|
|
|
104
|
- * @param type $info
|
|
|
|
105
|
- * @param type $w
|
|
|
|
106
|
- * @param type $h
|
|
|
|
107
|
- * @return string
|
|
|
|
108
|
- */
|
|
|
|
109
|
- private function cacheImage($info, $w, $h) {
|
|
|
|
110
|
- $path = $info['path'];
|
|
|
|
111
|
- $filename = $this->path . $info['hash'] . $w . '_' . $h;
|
|
|
|
112
|
- Image::make($path)->resize($w, $h)->save($filename);
|
|
|
|
113
|
- return $filename;
|
|
|
|
114
|
- }
|
|
|
|
115
|
-
|
|
|
|
116
|
- /**
|
|
|
|
117
|
- * 多图片上传
|
|
|
|
118
|
- * @param type $files file对象集合
|
|
|
|
119
|
- * @return type
|
|
|
|
120
|
- */
|
|
|
|
121
|
- private function multi($files) {
|
|
|
|
122
|
- if (!is_array($files)) {
|
|
|
|
123
|
- $files = [$files];
|
|
|
|
124
|
- }
|
|
|
|
125
|
- $save_data = [];
|
|
|
|
126
|
- $data = [];
|
|
|
|
127
|
- foreach ($files as $file) {
|
|
|
|
128
|
- $hash = hash_file('md5', $file->getPathname());
|
|
|
|
129
|
- $url = $this->path;
|
|
|
|
130
|
- $filename = date('ymdHis').rand(10000,99999);
|
|
|
|
131
|
- $res = $file->move($url,$filename);
|
|
|
|
132
|
- if ($res === false) {
|
|
|
|
133
|
- return $this->response($file->getError(), Code::USER_ERROR);
|
|
|
|
134
|
- }
|
|
|
|
135
|
- $save_data[] = [
|
|
|
|
136
|
- 'path' => $url.$filename,
|
|
|
|
137
|
- 'created_at' => date('Y-m-d H:i:s',time()),
|
|
|
|
138
|
- 'size' => $res->getSize(),
|
|
|
|
139
|
- 'hash' => $hash.$filename,
|
|
|
|
140
|
- 'type'=>$files->getClientOriginalExtension(),
|
|
|
|
141
|
- ];
|
|
|
|
142
|
- $data[] = $hash.$filename;
|
|
|
|
143
|
- }
|
|
|
|
144
|
- $imageModel = new ImageModel();
|
|
|
|
145
|
- $imageModel->insert($save_data);
|
|
|
|
146
|
- return $data;
|
|
|
|
147
|
- }
|
|
|
|
148
|
-
|
|
|
|
149
|
- //下载
|
|
|
|
150
|
- public function download($filename){
|
|
|
|
151
|
- $path = Storage::path($filename);
|
|
|
|
152
|
- return response()->download($path,time().rand(1,100000));
|
|
|
|
153
|
- }
|
|
|
|
154
|
- /**
|
|
|
|
155
|
- * @name 统一返回参数
|
|
|
|
156
|
- * @return JsonResponse
|
|
|
|
157
|
- * @author :liyuhang
|
|
|
|
158
|
- * @method
|
|
|
|
159
|
- */
|
|
|
|
160
|
- public function response($msg = null,string $code = Code::SUCCESS,$data = [],$result_code = 200,$type = 'application/json'): JsonResponse
|
|
|
|
161
|
- {
|
|
|
|
162
|
- $code = Code::fromValue($code);
|
|
|
|
163
|
- $result = [
|
|
|
|
164
|
- 'msg' => $msg == ' ' ? $code->description : $msg,
|
|
|
|
165
|
- 'code' => $code->value,
|
|
|
|
166
|
- 'data' => $data,
|
|
|
|
167
|
- ];
|
|
|
|
168
|
- $this->header['Content-Type'] = $type;
|
|
|
|
169
|
- $response = response($result,$result_code,$this->header);
|
|
|
|
170
|
- throw new HttpResponseException($response);
|
|
|
|
171
|
- }
|
|
|
|
172
|
-} |
|
|