1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Services; |
4
|
|
|
|
5
|
|
|
use App\Image; |
6
|
|
|
use Exception; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Http\UploadedFile; |
9
|
|
|
|
10
|
|
|
class ImageProcessor |
11
|
|
|
{ |
12
|
|
|
const MIME_JPEG = 'image/jpeg'; |
13
|
|
|
const MIME_JPG = 'image/jpg'; |
14
|
|
|
const MIME_PNG = 'image/png'; |
15
|
|
|
const MIME_GIF = 'image/gif'; |
16
|
|
|
|
17
|
|
|
protected $location = 'upload/images'; |
18
|
|
|
|
19
|
|
|
protected $filename; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return Image |
23
|
|
|
*/ |
24
|
|
|
public function getModel() |
25
|
|
|
{ |
26
|
|
|
return new Image(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Upload and create image for imageable instance. |
31
|
|
|
* |
32
|
|
|
* @param $image |
33
|
|
|
* @param $imageable |
34
|
|
|
* @param null $data |
35
|
|
|
* @param null $location |
36
|
|
|
* @return static |
37
|
|
|
* @throws Exception |
38
|
|
|
*/ |
39
|
|
|
public function uploadAndCreate($image, $imageable, $data = null, $location = null) |
40
|
|
|
{ |
41
|
|
|
if($this->validateImage($image)) { |
42
|
|
|
$original = $image->getClientOriginalName(); |
43
|
|
|
if (isset($location)) |
44
|
|
|
$this->setLocation($location); |
45
|
|
|
|
46
|
|
|
$data['original'] = $original; |
47
|
|
|
|
48
|
|
|
if ($imageInfo = $this->upload($image)) |
49
|
|
|
{ |
50
|
|
|
return $this->create($imageInfo->getPathname(), $imageable, $data); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Download image to server and get as UploadedFile object. |
57
|
|
|
* |
58
|
|
|
* @param $url |
59
|
|
|
* @param $imageable |
60
|
|
|
* @param array|null $data |
61
|
|
|
* @param string $location |
62
|
|
|
* @return static |
63
|
|
|
*/ |
64
|
|
|
public function downloadAndUpload($url, $imageable, array $data = null, $location = 'upload/images/') |
65
|
|
|
{ |
66
|
|
|
$file = file_get_contents($url); // download the image from url. |
67
|
|
|
$extension = $this->parseExtension(getimagesize($url)['mime']); |
68
|
|
|
|
69
|
|
|
if (isset($location)) |
70
|
|
|
$this->setLocation($location); |
71
|
|
|
$filename = time() .'_'. str_random(15); |
72
|
|
|
|
73
|
|
|
$path_to_image = $this->pattern($filename, $extension, $location); |
74
|
|
|
file_put_contents($path_to_image, $file); |
75
|
|
|
|
76
|
|
|
$imageInfo = new UploadedFile($path_to_image, str_random(10)); |
77
|
|
|
$data = array_merge(['original' => $filename], $data); |
78
|
|
|
|
79
|
|
|
return $this->create($imageInfo->getRealPath(), $imageable, $data); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Validate incoming image. |
84
|
|
|
* |
85
|
|
|
* @param $image |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
private function validateImage($image) |
89
|
|
|
{ |
90
|
|
|
return (bool) ($image instanceof UploadedFile); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Render image name. |
95
|
|
|
* |
96
|
|
|
* @param $image |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
private function getNewImageName($image) |
100
|
|
|
{ |
101
|
|
|
$hash = md5_file($image->getRealPath()); |
102
|
|
|
$ext = $image->getClientOriginalExtension(); |
103
|
|
|
$time = time(); |
104
|
|
|
return "{$time}_{$hash}.{$ext}"; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
public function getLocation() |
111
|
|
|
{ |
112
|
|
|
return $this->location; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param Image $image |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
public function destroy($image) |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
if($image) { |
121
|
|
|
$location = ltrim($image->image, '/'); |
|
|
|
|
122
|
|
|
@unlink(base_path("public/{$location}")); |
|
|
|
|
123
|
|
|
|
124
|
|
|
$image->delete(); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Delete only image form folder. |
130
|
|
|
* |
131
|
|
|
* @param $image |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
View Code Duplication |
public function destroyImageOnly($image) |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
if($image) { |
137
|
|
|
$location = ltrim($image->image, '/'); |
138
|
|
|
return @unlink(base_path("public/{$location}")); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set location. |
146
|
|
|
* |
147
|
|
|
* @param $location |
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
|
|
public function setLocation($location) |
151
|
|
|
{ |
152
|
|
|
$this->location = trim($location, '/'); |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param $location |
159
|
|
|
* @throws Exception |
160
|
|
|
*/ |
161
|
|
|
protected function validateLocation($location) |
162
|
|
|
{ |
163
|
|
|
$fileExists = file_exists($location); |
164
|
|
|
|
165
|
|
|
if ((! $fileExists && ! mkdir($location, 0777, true)) || ! is_writeable($location)) { |
166
|
|
|
throw new Exception("Location [$location] not exists or not writable"); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
protected function upload($image) |
171
|
|
|
{ |
172
|
|
|
$location = base_path("public/" . $this->location); |
173
|
|
|
|
174
|
|
|
$this->validateLocation($location); |
175
|
|
|
|
176
|
|
|
if ($image) { |
177
|
|
|
if ($image->isValid()) { |
178
|
|
|
$filename = rtrim($location, '/') . '/' . ltrim($this->getNewImageName($image), '/'); |
179
|
|
|
|
180
|
|
|
$this->filename = $filename; |
181
|
|
|
|
182
|
|
|
return $image->move($location, $filename); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return false; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Change avatar. |
191
|
|
|
* |
192
|
|
|
* @param $avatar |
193
|
|
|
* @param string $location |
194
|
|
|
* @return $this |
195
|
|
|
*/ |
196
|
|
|
public function changeAvatar($avatar, $location = 'upload/images/user_avatars/') |
197
|
|
|
{ |
198
|
|
|
if ($old_avatar = \Auth::user()->images()->avatar()->first()) |
199
|
|
|
$this->destroy($old_avatar); |
200
|
|
|
|
201
|
|
|
if(is_string($avatar)) { // check if url. |
202
|
|
|
$this->downloadAndUpload($avatar, \Auth::user(), ['type' => 'avatar'], $location); |
203
|
|
|
|
204
|
|
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
$this->uploadAndCreate($avatar, \Auth::user(), ['type' => 'avatar'], $location); |
208
|
|
|
|
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Standart pattern. |
214
|
|
|
* |
215
|
|
|
* @param $location |
216
|
|
|
* @param $filename |
217
|
|
|
* @param $extension |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
|
|
private function pattern($filename, $extension, $location = null) |
221
|
|
|
{ |
222
|
|
|
if(! $location) |
223
|
|
|
return sprintf('%s.%s', $filename, $extension); |
224
|
|
|
|
225
|
|
|
return sprintf('%s%s.%s', $location, $filename, $extension); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Parse extension. |
230
|
|
|
* |
231
|
|
|
* @param $mime |
232
|
|
|
* @return string |
233
|
|
|
*/ |
234
|
|
|
private function parseExtension($mime) |
235
|
|
|
{ |
236
|
|
|
switch ($mime) |
237
|
|
|
{ |
238
|
|
|
case self::MIME_JPEG: |
239
|
|
|
return 'jpeg'; |
240
|
|
|
break; |
|
|
|
|
241
|
|
|
case self::MIME_JPG: |
242
|
|
|
return 'jpg'; |
243
|
|
|
break; |
|
|
|
|
244
|
|
|
case self::MIME_PNG: |
245
|
|
|
return 'png'; |
246
|
|
|
break; |
|
|
|
|
247
|
|
|
case self::MIME_GIF: |
248
|
|
|
return 'gif'; |
249
|
|
|
break; |
|
|
|
|
250
|
|
|
default: |
251
|
|
|
return 'jpeg'; |
252
|
|
|
break; |
|
|
|
|
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
private function create($path, $imageable, array $data = null) |
257
|
|
|
{ |
258
|
|
|
return $this->getModel()->create([ |
259
|
|
|
'imageable_id' => $imageable->id, |
260
|
|
|
'imageable_type' => get_class($imageable), |
261
|
|
|
'type' => isset($data['type']) ? $data['type'] : 'cover', |
262
|
|
|
'original' => isset($data['original']) ? $data['original'] : str_random(13), |
263
|
|
|
'image' => str_replace(base_path('public'), '', $path), |
264
|
|
|
]); |
265
|
|
|
} |
266
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.