1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\AssetLibrary; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Illuminate\Support\Facades\DB; |
7
|
|
|
use Spatie\MediaLibrary\Models\Media; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
use Spatie\MediaLibrary\HasMedia\HasMedia; |
10
|
|
|
use Spatie\MediaLibrary\HasMedia\HasMediaTrait; |
11
|
|
|
use Thinktomorrow\AssetLibrary\Exceptions\ConfigException; |
12
|
|
|
use Thinktomorrow\AssetLibrary\Exceptions\CorruptMediaException; |
13
|
|
|
|
14
|
|
|
class Asset extends Model implements HasMedia |
15
|
|
|
{ |
16
|
|
|
use HasMediaTrait; |
|
|
|
|
17
|
|
|
|
18
|
|
|
private $order; |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @return bool |
22
|
|
|
*/ |
23
|
1 |
|
public function hasFile(): bool |
24
|
|
|
{ |
25
|
1 |
|
return (bool) $this->url(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $size |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
17 |
|
public function filename($size = ''): string |
33
|
|
|
{ |
34
|
17 |
|
return basename($this->url($size)); |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
public function exists(): bool |
38
|
|
|
{ |
39
|
1 |
|
return true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $size |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
50 |
|
public function url($size = ''): string |
47
|
|
|
{ |
48
|
50 |
|
$media = $this->getFirstMedia(); |
49
|
|
|
|
50
|
50 |
|
if ($media == null) { |
51
|
1 |
|
return ''; |
52
|
|
|
} |
53
|
|
|
|
54
|
49 |
|
return $media->getUrl($size); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return bool|string |
59
|
|
|
*/ |
60
|
2 |
|
public function getExtensionForFilter() |
61
|
|
|
{ |
62
|
2 |
|
if ($extension = $this->getExtensionType()) { |
63
|
1 |
|
return $extension; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
return ''; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return null|string |
71
|
|
|
* @throws CorruptMediaException |
72
|
|
|
*/ |
73
|
2 |
|
public function getExtensionType(): ?string |
74
|
|
|
{ |
75
|
2 |
|
$media = $this->getMedia()->first(); |
76
|
|
|
|
77
|
2 |
|
if ($media == null) { |
78
|
1 |
|
throw CorruptMediaException::missingMediaRelation($this->id); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
$extension = explode('.', $media->file_name); |
82
|
1 |
|
$extension = end($extension); |
83
|
|
|
|
84
|
1 |
|
if ($extension) { |
85
|
1 |
|
if (in_array(strtolower($extension), ['xls', 'xlsx', 'numbers', 'sheets'])) { |
86
|
1 |
|
return 'xls'; |
87
|
|
|
} |
88
|
1 |
|
if (in_array(strtolower($extension), ['png', 'jpg', 'jpeg', 'gif', 'svg', 'webp'])) { |
89
|
1 |
|
return 'image'; |
90
|
|
|
} |
91
|
1 |
|
if (strtolower($extension) === 'pdf') { |
92
|
1 |
|
return 'pdf'; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
return null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
2 |
|
public function getMimeType(): string |
103
|
|
|
{ |
104
|
2 |
|
return $this->isMediaEmpty() ? '' : $this->getMedia()[0]->mime_type; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
7 |
|
public function isMediaEmpty(): bool |
111
|
|
|
{ |
112
|
7 |
|
return $this->getMedia()->isEmpty(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
2 |
|
public function getSize(): string |
119
|
|
|
{ |
120
|
2 |
|
return $this->isMediaEmpty() ? '' : $this->getMedia()[0]->human_readable_size; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string|null $size |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
3 |
|
public function getDimensions($size = null): string |
128
|
|
|
{ |
129
|
3 |
|
if ($this->isMediaEmpty()) { |
130
|
1 |
|
return ''; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
//TODO Check the other sizes as well |
134
|
2 |
|
if ($size === 'cropped') { |
135
|
1 |
|
$dimensions = explode(',', $this->getMedia()[0]->manipulations['cropped']['manualCrop']); |
136
|
|
|
|
137
|
1 |
|
return $dimensions[0].' x'.$dimensions[1]; |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
$dimensions = ''; |
141
|
1 |
|
if (self::isImage($this->getMedia()[0])) { |
142
|
1 |
|
$imagesize = getimagesize(public_path($this->url($size??''))); |
143
|
|
|
|
144
|
1 |
|
$dimensions = $imagesize[0].' x '.$imagesize[1]; |
145
|
|
|
} |
146
|
|
|
|
147
|
1 |
|
return $dimensions; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param string|null $size |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
3 |
|
public function getWidth($size = null): string |
155
|
|
|
{ |
156
|
3 |
|
if ($this->isMediaEmpty()) { |
157
|
1 |
|
return ''; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
//TODO Check the other sizes as well |
161
|
2 |
|
if ($size === 'cropped') { |
162
|
1 |
|
$width = explode(',', $this->getMedia()[0]->manipulations['cropped']['manualCrop']); |
163
|
|
|
|
164
|
1 |
|
return $width[0]; |
165
|
|
|
} |
166
|
|
|
|
167
|
1 |
|
$width = ''; |
168
|
1 |
|
if (self::isImage($this->getMedia()[0])) { |
169
|
1 |
|
$imagesize = getimagesize(public_path($this->url($size??''))); |
170
|
|
|
|
171
|
1 |
|
$width = $imagesize[0]; |
172
|
|
|
} |
173
|
|
|
|
174
|
1 |
|
return $width; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string|null $size |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
3 |
|
public function getHeight($size = null): string |
182
|
|
|
{ |
183
|
3 |
|
if ($this->isMediaEmpty()) { |
184
|
1 |
|
return ''; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
//TODO Check the other sizes as well |
188
|
2 |
|
if ($size === 'cropped') { |
189
|
1 |
|
$height = explode(',', $this->getMedia()[0]->manipulations['cropped']['manualCrop']); |
190
|
|
|
|
191
|
1 |
|
return trim($height[1]); |
192
|
|
|
} |
193
|
|
|
|
194
|
1 |
|
$height = ''; |
195
|
1 |
|
if (self::isImage($this->getMedia()[0])) { |
196
|
1 |
|
$imagesize = getimagesize(public_path($this->url($size??''))); |
197
|
|
|
|
198
|
1 |
|
$height = $imagesize[1]; |
199
|
|
|
} |
200
|
|
|
|
201
|
1 |
|
return $height; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param UploadedFile $file |
|
|
|
|
207
|
|
|
* @return bool |
208
|
|
|
*/ |
209
|
3 |
|
private static function isImage($file): bool |
210
|
|
|
{ |
211
|
3 |
|
return Str::before($file->mime_type, '/') === 'image'; |
212
|
|
|
} |
213
|
|
|
|
214
|
1 |
|
public function isUsed() |
215
|
|
|
{ |
216
|
1 |
|
$pivots = DB::table('asset_pivots')->where('asset_id', $this->id)->where('unused', false)->get(); |
217
|
|
|
|
218
|
1 |
|
return ! $pivots->isEmpty(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function isUnused() |
222
|
|
|
{ |
223
|
|
|
$pivots = DB::table('asset_pivots')->where('asset_id', $this->id)->where('unused', false)->get(); |
224
|
|
|
|
225
|
|
|
return $pivots->isEmpty(); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param $width |
230
|
|
|
* @param $height |
231
|
|
|
* @param $x |
232
|
|
|
* @param $y |
233
|
|
|
* @return $this |
234
|
|
|
* @throws ConfigException |
235
|
|
|
*/ |
236
|
2 |
|
public function crop($width, $height, $x, $y) |
237
|
|
|
{ |
238
|
2 |
|
if (! config('thinktomorrow.assetlibrary.allowCropping')) { |
239
|
1 |
|
throw ConfigException::croppingDisabled(); |
240
|
|
|
} |
241
|
1 |
|
$this->media[0]->manipulations = [ |
|
|
|
|
242
|
|
|
'cropped' => [ |
243
|
1 |
|
'manualCrop' => $width.', '.$height.', '.$x.', '.$y, |
244
|
|
|
], |
245
|
|
|
]; |
246
|
|
|
|
247
|
1 |
|
$this->media[0]->save(); |
248
|
|
|
|
249
|
1 |
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Register the conversions that should be performed. |
254
|
|
|
* |
255
|
|
|
* @param Media|null $media |
256
|
|
|
* @throws \Spatie\Image\Exceptions\InvalidManipulation |
257
|
|
|
*/ |
258
|
49 |
|
public function registerMediaConversions(Media $media = null) |
259
|
|
|
{ |
260
|
49 |
|
$conversions = config('thinktomorrow.assetlibrary.conversions'); |
261
|
|
|
|
262
|
49 |
|
foreach ($conversions as $key => $value) { |
263
|
49 |
|
$this->addMediaConversion($key) |
264
|
49 |
|
->width($value['width']) |
265
|
49 |
|
->height($value['height']) |
266
|
49 |
|
->keepOriginalImageFormat() |
267
|
49 |
|
->optimize(); |
268
|
|
|
} |
269
|
|
|
|
270
|
49 |
|
if (config('thinktomorrow.assetlibrary.allowCropping')) { |
271
|
1 |
|
$this->addMediaConversion('cropped') |
272
|
1 |
|
->keepOriginalImageFormat() |
273
|
1 |
|
->optimize(); |
274
|
|
|
} |
275
|
49 |
|
} |
276
|
|
|
} |
277
|
|
|
|