1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Form\Field; |
4
|
|
|
|
5
|
|
|
use Intervention\Image\ImageManagerStatic; |
6
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
7
|
|
|
|
8
|
|
|
class Image extends File |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Validation rules. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $rules = 'image'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Intervention calls. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $calls = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get default storage path. |
26
|
|
|
* |
27
|
|
|
* @return mixed |
28
|
|
|
*/ |
29
|
|
|
public function defaultStorePath() |
30
|
|
|
{ |
31
|
|
|
return config('admin.upload.directory.image'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Prepare for single upload file. |
36
|
|
|
* |
37
|
|
|
* @param UploadedFile|null $image |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
protected function prepareForSingle(UploadedFile $image = null) |
42
|
|
|
{ |
43
|
|
|
$this->directory = $this->directory ?: $this->defaultStorePath(); |
44
|
|
|
|
45
|
|
|
$this->name = $this->getStoreName($image); |
|
|
|
|
46
|
|
|
|
47
|
|
|
$this->executeCalls($image->getRealPath()); |
|
|
|
|
48
|
|
|
|
49
|
|
|
$target = $this->uploadAndDeleteOriginal($image); |
|
|
|
|
50
|
|
|
|
51
|
|
|
return $target; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Execute Intervention calls. |
56
|
|
|
* |
57
|
|
|
* @param string $target |
58
|
|
|
* |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
|
|
public function executeCalls($target) |
62
|
|
|
{ |
63
|
|
|
if (!empty($this->calls)) { |
64
|
|
|
$image = ImageManagerStatic::make($target); |
65
|
|
|
|
66
|
|
|
foreach ($this->calls as $call) { |
67
|
|
|
call_user_func_array([$image, $call['method']], $call['arguments'])->save($target); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $target; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Build a preview item. |
76
|
|
|
* |
77
|
|
|
* @param string $image |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
protected function buildPreviewItem($image) |
81
|
|
|
{ |
82
|
|
|
return '<img src="'.$this->objectUrl($image).'" class="file-preview-image">'; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Render a image form field. |
87
|
|
|
* |
88
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
89
|
|
|
*/ |
90
|
|
|
public function render() |
91
|
|
|
{ |
92
|
|
|
$this->options(['allowedFileTypes' => ['image']]); |
93
|
|
|
|
94
|
|
|
return parent::render(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Call intervention methods. |
99
|
|
|
* |
100
|
|
|
* @param string $method |
101
|
|
|
* @param array $arguments |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
|
|
public function __call($method, $arguments) |
105
|
|
|
{ |
106
|
|
|
$this->calls[] = [ |
107
|
|
|
'method' => $method, |
108
|
|
|
'arguments' => $arguments, |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):