|
@@ -7,7 +7,7 @@ use App\Models\Image as ImageModel; |
|
@@ -7,7 +7,7 @@ use App\Models\Image as ImageModel; |
|
7
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
7
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
8
|
use Illuminate\Http\JsonResponse;
|
8
|
use Illuminate\Http\JsonResponse;
|
|
9
|
use Illuminate\Http\Request;
|
9
|
use Illuminate\Http\Request;
|
|
10
|
-use Illuminate\Support\Facades\Hash;
|
10
|
+use Illuminate\Support\Facades\DB;
|
|
11
|
use Intervention\Image\Facades\Image;
|
11
|
use Intervention\Image\Facades\Image;
|
|
12
|
|
12
|
|
|
13
|
class ImageController
|
13
|
class ImageController
|
|
@@ -109,27 +109,26 @@ class ImageController |
|
@@ -109,27 +109,26 @@ class ImageController |
|
109
|
* @method
|
109
|
* @method
|
|
110
|
*/
|
110
|
*/
|
|
111
|
public function single($files){
|
111
|
public function single($files){
|
|
112
|
- if (is_array($files)) {
|
|
|
|
113
|
- $file = current($files);
|
|
|
|
114
|
- }
|
112
|
+ $hash = hash_file('md5', $files->getPathname());
|
|
115
|
$url = './../uploads/images/';
|
113
|
$url = './../uploads/images/';
|
|
116
|
$filename = date('ymdHis').rand(10000,99999);
|
114
|
$filename = date('ymdHis').rand(10000,99999);
|
|
117
|
- $res = $files->move($url,$filename);
|
115
|
+ $res = $this->request->file('image')->move($url,$filename);
|
|
118
|
if ($res === false) {
|
116
|
if ($res === false) {
|
|
119
|
- return $this->fail($files->getError(), 400);
|
117
|
+ return $this->fail($files->getError(), Code::USER_ERROR);
|
|
120
|
}
|
118
|
}
|
|
121
|
-
|
119
|
+ $imageModel = new ImageModel();
|
|
122
|
$data = [
|
120
|
$data = [
|
|
123
|
'path' => $url.$filename,
|
121
|
'path' => $url.$filename,
|
|
124
|
'created_at' => date('Y-m-d H:i:s',time()),
|
122
|
'created_at' => date('Y-m-d H:i:s',time()),
|
|
125
|
'size' => $res->getSize(),
|
123
|
'size' => $res->getSize(),
|
|
126
|
- 'hash' => hash_file('md5', $res->getPathname()),
|
124
|
+ 'hash' => $hash.$filename,
|
|
127
|
'type'=>$files->getClientOriginalExtension(),
|
125
|
'type'=>$files->getClientOriginalExtension(),
|
|
128
|
-// 'mime'=>$files->getMimeType()
|
|
|
|
129
|
];
|
126
|
];
|
|
130
|
- $imageModel = new ImageModel();
|
|
|
|
131
|
- $imageModel->add($data);
|
|
|
|
132
|
- return $data['hash'];
|
127
|
+ $rs = $imageModel->add($data);
|
|
|
|
128
|
+ if ($rs === false) {
|
|
|
|
129
|
+ return $this->fail('添加失败', Code::USER_ERROR);
|
|
|
|
130
|
+ }
|
|
|
|
131
|
+ return $hash.$filename;
|
|
133
|
}
|
132
|
}
|
|
134
|
/**
|
133
|
/**
|
|
135
|
* 生成缩略图缓存
|
134
|
* 生成缩略图缓存
|
|
@@ -146,6 +145,38 @@ class ImageController |
|
@@ -146,6 +145,38 @@ class ImageController |
|
146
|
}
|
145
|
}
|
|
147
|
|
146
|
|
|
148
|
/**
|
147
|
/**
|
|
|
|
148
|
+ * 多图片上传
|
|
|
|
149
|
+ * @param type $files file对象集合
|
|
|
|
150
|
+ * @return type
|
|
|
|
151
|
+ */
|
|
|
|
152
|
+ private function multi($files) {
|
|
|
|
153
|
+ if (!is_array($files)) {
|
|
|
|
154
|
+ $files = [$files];
|
|
|
|
155
|
+ }
|
|
|
|
156
|
+ $save_data = [];
|
|
|
|
157
|
+ $data = [];
|
|
|
|
158
|
+ foreach ($files as $file) {
|
|
|
|
159
|
+ $hash = hash_file('md5', $file->getPathname());
|
|
|
|
160
|
+ $url = './../uploads/images/';
|
|
|
|
161
|
+ $filename = date('ymdHis').rand(10000,99999);
|
|
|
|
162
|
+ $res = $file->move($url,$filename);
|
|
|
|
163
|
+ if ($res === false) {
|
|
|
|
164
|
+ return $this->fail($file->getError(), 400);
|
|
|
|
165
|
+ }
|
|
|
|
166
|
+ $save_data[] = [
|
|
|
|
167
|
+ 'path' => $url.$filename,
|
|
|
|
168
|
+ 'created_at' => date('Y-m-d H:i:s',time()),
|
|
|
|
169
|
+ 'size' => $res->getSize(),
|
|
|
|
170
|
+ 'hash' => $hash.$filename,
|
|
|
|
171
|
+ 'type'=>$files->getClientOriginalExtension(),
|
|
|
|
172
|
+ ];
|
|
|
|
173
|
+ $data[] = $hash.$filename;
|
|
|
|
174
|
+ }
|
|
|
|
175
|
+ $imageModel = new ImageModel();
|
|
|
|
176
|
+ $imageModel->insertAll($data);
|
|
|
|
177
|
+ $this->response('上传成功!', 200, $data);
|
|
|
|
178
|
+ }
|
|
|
|
179
|
+ /**
|
|
149
|
* @name 统一返回参数
|
180
|
* @name 统一返回参数
|
|
150
|
* @return JsonResponse
|
181
|
* @return JsonResponse
|
|
151
|
* @author :liyuhang
|
182
|
* @author :liyuhang
|