1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NovaThinKit\FeatureImage\Nova; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Laravel\Nova\Fields\Image; |
7
|
|
|
use NovaThinKit\FeatureImage\Models\WithFeatureImage; |
8
|
|
|
|
9
|
|
|
trait HasFeatureImage |
10
|
|
|
{ |
11
|
10 |
|
public function fieldFeatureImage(string $title = 'Feature Image', ?string $tag = null): Image |
12
|
|
|
{ |
13
|
10 |
|
$model = $this->model(); |
|
|
|
|
14
|
|
|
|
15
|
10 |
|
$attr = method_exists($model, 'featureImageKey') ? $model->featureImageKey($tag) : 'image'; |
16
|
|
|
|
17
|
10 |
|
return Image::make($title, $attr) |
18
|
10 |
|
->rules(['mimes:jpeg,png', 'max:' . 1024 * 10]) |
19
|
10 |
|
->deletable(true) |
20
|
10 |
|
->store(function (Request $request, WithFeatureImage $model, string $attribute) use ($tag) { |
21
|
2 |
|
return function () use ($request, $model, $attribute, $tag) { |
22
|
2 |
|
if ($request->hasFile($attribute)) { |
23
|
2 |
|
$model->$attribute = $model |
24
|
2 |
|
->featureImageManager($tag) |
25
|
2 |
|
->storeUploaded($request->file($attribute)); |
|
|
|
|
26
|
2 |
|
$model->save(); |
|
|
|
|
27
|
|
|
} |
28
|
2 |
|
}; |
29
|
10 |
|
}) |
30
|
10 |
|
->delete(function (Request $request, WithFeatureImage $model) use ($attr, $tag) { |
31
|
1 |
|
if ($model->featureImageManager($tag)->delete()) { |
32
|
1 |
|
$model->$attr = null; |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
return true; |
36
|
10 |
|
}) |
37
|
10 |
|
->preview(function ($value, $disk, WithFeatureImage $model) use ($tag) { |
38
|
4 |
|
return $model->featureImageManager($tag)->url('thumb'); |
39
|
10 |
|
}) |
40
|
10 |
|
->thumbnail(function ($value, $disk, WithFeatureImage $model) use ($tag) { |
41
|
4 |
|
return $model->featureImageManager($tag)->url('thumb'); |
42
|
10 |
|
}) |
43
|
10 |
|
->download(function ($request, $model, $disk, $path) use ($tag) { |
|
|
|
|
44
|
1 |
|
return $model->featureImageManager($tag)->download(); |
45
|
10 |
|
}) |
46
|
10 |
|
->help('Image: jpg or png, ~1600x1200px. The script will automatically crop images'); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|