|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yaro\Jarboe\Table\Fields; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Illuminate\Http\UploadedFile; |
|
7
|
|
|
use Yaro\Jarboe\Table\Fields\Traits\Placeholder; |
|
8
|
|
|
use Yaro\Jarboe\Table\Fields\Traits\Storage; |
|
9
|
|
|
use Illuminate\Support\Facades\Storage as IlluminateStorage; |
|
10
|
|
|
use Intervention\Image\ImageManagerStatic as InterventionImage; |
|
11
|
|
|
|
|
12
|
|
|
class ImageDeprecated extends AbstractField |
|
13
|
|
|
{ |
|
14
|
|
|
use Storage; |
|
15
|
|
|
use Placeholder; |
|
16
|
|
|
|
|
17
|
|
|
protected $encode = false; |
|
18
|
|
|
protected $crop = false; |
|
19
|
|
|
protected $ratio = [ |
|
20
|
|
|
'width' => false, |
|
21
|
|
|
'height' => false, |
|
22
|
|
|
]; |
|
23
|
|
|
protected $disk; |
|
24
|
|
|
protected $path = ''; |
|
25
|
|
|
protected $multiple = false; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->disk = config('filesystems.default'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function shouldSkip(Request $request) |
|
33
|
|
|
{ |
|
34
|
|
|
$files = $request->file($this->name()); |
|
35
|
|
|
|
|
36
|
|
|
return !$files; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function isEncode() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->encode; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Encode image as data-url. |
|
46
|
|
|
* |
|
47
|
|
|
* @param bool $encode |
|
48
|
|
|
* @return $this |
|
49
|
|
|
*/ |
|
50
|
|
|
public function encode(bool $encode = true) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->encode = $encode; |
|
53
|
|
|
|
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function storeFile(UploadedFile $file, $filename, Request $request, $index = 0) |
|
58
|
|
|
{ |
|
59
|
|
|
if (!$this->isCrop()) { |
|
60
|
|
|
if ($this->isEncode()) { |
|
61
|
|
|
$image = InterventionImage::make($file->getRealPath()); |
|
62
|
|
|
return (string) $image->encode('data-url'); |
|
63
|
|
|
} |
|
64
|
|
|
return $file->storeAs($this->getPath(), $filename, $this->getDisk()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$image = InterventionImage::make($file->getRealPath()); |
|
68
|
|
|
$imageCropPropsField = sprintf('__%s_%s', $this->name(), 'cropvalues'); |
|
69
|
|
|
if ($this->isCrop() && $request->has($imageCropPropsField)) { |
|
70
|
|
|
$image->crop( |
|
71
|
|
|
round($request->input(sprintf('%s.%s.width', $imageCropPropsField, $index))), |
|
72
|
|
|
round($request->input(sprintf('%s.%s.height', $imageCropPropsField, $index))), |
|
73
|
|
|
round($request->input(sprintf('%s.%s.x', $imageCropPropsField, $index))), |
|
74
|
|
|
round($request->input(sprintf('%s.%s.y', $imageCropPropsField, $index))) |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if ($this->isEncode()) { |
|
79
|
|
|
return (string) $image->encode('data-url'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$path = trim($this->getPath() .'/'. $filename, '/'); |
|
83
|
|
|
IlluminateStorage::disk($this->getDisk())->put($path, (string) $image->encode()); |
|
84
|
|
|
|
|
85
|
|
|
return $path; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function value(Request $request) |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
$files = $request->file($this->name()); |
|
91
|
|
|
if (!$files) { |
|
92
|
|
|
return $this->isNullable() ? null : ''; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$files = is_array($files) ? $files : [$files]; |
|
96
|
|
|
$paths = []; |
|
97
|
|
|
foreach ($files as $index => $file) { |
|
98
|
|
|
$filename = $file->hashName(); |
|
99
|
|
|
$paths[] = $this->storeFile($file, $filename, $request, $index); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if (!$this->isMultiple()) { |
|
103
|
|
|
return array_pop($paths); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $paths; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function crop(bool $crop = true) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->crop = $crop; |
|
112
|
|
|
|
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function ratio(int $width, int $height) |
|
117
|
|
|
{ |
|
118
|
|
|
$this->ratio['width'] = $width; |
|
119
|
|
|
$this->ratio['height'] = $height; |
|
120
|
|
|
|
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public function isCrop() |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->crop; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
View Code Duplication |
public function getRatio(string $type) |
|
|
|
|
|
|
130
|
|
|
{ |
|
131
|
|
|
$type = strtolower($type); |
|
132
|
|
|
|
|
133
|
|
|
switch ($type) { |
|
134
|
|
|
case 'width': |
|
135
|
|
|
case 'height': |
|
136
|
|
|
return $this->ratio[$type]; |
|
137
|
|
|
default: |
|
138
|
|
|
return false; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function getListView($model) |
|
143
|
|
|
{ |
|
144
|
|
|
return view('jarboe::crud.fields.image_deprecated.list', [ |
|
145
|
|
|
'model' => $model, |
|
146
|
|
|
'field' => $this, |
|
147
|
|
|
]); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function getEditFormView($model) |
|
151
|
|
|
{ |
|
152
|
|
|
$template = $this->isReadonly() ? 'readonly' : 'edit'; |
|
153
|
|
|
|
|
154
|
|
|
return view('jarboe::crud.fields.image_deprecated.'. $template, [ |
|
155
|
|
|
'model' => $model, |
|
156
|
|
|
'field' => $this, |
|
157
|
|
|
]); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function getCreateFormView() |
|
161
|
|
|
{ |
|
162
|
|
|
return view('jarboe::crud.fields.image_deprecated.create', [ |
|
163
|
|
|
'model' => null, |
|
164
|
|
|
'field' => $this, |
|
165
|
|
|
]); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function multiple(bool $multiple = true) |
|
169
|
|
|
{ |
|
170
|
|
|
$this->multiple = $multiple; |
|
171
|
|
|
|
|
172
|
|
|
return $this; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function isMultiple() |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->multiple; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function disk(string $disk) |
|
181
|
|
|
{ |
|
182
|
|
|
$this->disk = $disk; |
|
183
|
|
|
|
|
184
|
|
|
return $this; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function getDisK() |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->disk; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function path(string $path) |
|
193
|
|
|
{ |
|
194
|
|
|
$this->path = $path; |
|
195
|
|
|
|
|
196
|
|
|
return $this; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function getPath() |
|
200
|
|
|
{ |
|
201
|
|
|
return $this->path; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
public function getUrl($model) |
|
|
|
|
|
|
205
|
|
|
{ |
|
206
|
|
|
if (is_null($model)) { |
|
207
|
|
|
return null; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
$filepath = $model->{$this->name()}; |
|
211
|
|
|
if (!$filepath) { |
|
212
|
|
|
return null; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
$disk = IlluminateStorage::disk($this->getDisk()); |
|
216
|
|
|
|
|
217
|
|
|
$paths = []; |
|
218
|
|
|
if (is_array($filepath)) { |
|
219
|
|
|
foreach ($filepath as $path) { |
|
220
|
|
|
$paths[] = $this->isEncode() ? $path : $disk->url($path); |
|
221
|
|
|
} |
|
222
|
|
|
return $paths; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return $this->isEncode() ? $filepath : $disk->url($filepath); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.