ImageField.php
6.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/**
* +-----------------------------------------------------------------------------------------------------------------------
* 重写 laravel-admin Form\Field\ImageField 类
* +-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Admin\Rewrite\Form\Field
* @package App\Admin\Rewrite\Form\Field
* @author Richer <yangzi1028@163.com>
* @date 2020年11月12日13:49:31
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Admin\Rewrite\Form\Field;
use Intervention\Image\Constraint;
use Intervention\Image\Facades\Image as InterventionImage;
use Intervention\Image\ImageManagerStatic;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Trait ImageField
*
* @category App\Admin\Rewrite\Form\Field
* @package App\Admin\Rewrite\Form\Field
* @author Richer <yangzi1028@163.com>
* @date 2020年11月12日13:49:31
* @copyright 2020-2022 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
trait ImageField
{
/**
* Intervention calls.
*
* @var array
*/
protected $interventionCalls = [];
/**
* Thumbnail settings.
*
* @var array
*/
protected $thumbnails = [];
/**
* Default directory for file to upload.
*
* @return mixed
*/
public function defaultDirectory()
{
return config('admin.upload.directory.image');
}
/**
* Execute Intervention calls.
*
* @param string $target
*
* @return mixed
*/
public function callInterventionMethods($target)
{
if (!empty($this->interventionCalls)) {
$image = ImageManagerStatic::make($target);
foreach ($this->interventionCalls as $call) {
call_user_func_array(
[$image, $call['method']],
$call['arguments']
)->save($target);
}
}
return $target;
}
/**
* Call intervention methods.
*
* @param string $method
* @param array $arguments
*
* @throws \Exception
*
* @return $this
*/
public function __call($method, $arguments)
{
if (static::hasMacro($method)) {
return $this;
}
if (!class_exists(ImageManagerStatic::class)) {
throw new \Exception('To use image handling and manipulation, please install [intervention/image] first.');
}
$this->interventionCalls[] = [
'method' => $method,
'arguments' => $arguments,
];
return $this;
}
/**
* Render a image form field.
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function render()
{
$this->options(['allowedFileTypes' => ['image'], 'msgPlaceholder' => trans('admin.choose_image')]);
//modify by ww 于2020年07月29日16:45:21 展示默认图片
if (empty($this->value)) {
$this->value = $this->getDefault() ?: null;
}
return parent::render();
}
/**
* @param string|array $name
* @param int $width
* @param int $height
*
* @return $this
*/
public function thumbnail($name, int $width = null, int $height = null)
{
if (func_num_args() == 1 && is_array($name)) {
foreach ($name as $key => $size) {
if (count($size) >= 2) {
$this->thumbnails[$key] = $size;
}
}
} elseif (func_num_args() == 3) {
$this->thumbnails[$name] = [$width, $height];
}
return $this;
}
/**
* Destroy original thumbnail files.
*
* @return void.
*/
public function destroyThumbnail()
{
if ($this->retainable) {
return;
}
foreach ($this->thumbnails as $name => $_) {
// We need to get extension type ( .jpeg , .png ...)
$ext = pathinfo($this->original, PATHINFO_EXTENSION);
// We remove extension from file name so we can append thumbnail type
$path = str_replace_last('.'.$ext, '', $this->original);
// We merge original name + thumbnail name + extension
$path = $path.'-'.$name.'.'.$ext;
if ($this->storage->exists($path)) {
$this->storage->delete($path);
}
}
}
/**
* Upload file and delete original thumbnail files.
*
* @param UploadedFile $file
*
* @return $this
*/
protected function uploadAndDeleteOriginalThumbnail(UploadedFile $file)
{
foreach ($this->thumbnails as $name => $size) {
// We need to get extension type ( .jpeg , .png ...)
$ext = pathinfo($this->name, PATHINFO_EXTENSION);
// We remove extension from file name so we can append thumbnail type
$path = str_replace_last('.'.$ext, '', $this->name);
// We merge original name + thumbnail name + extension
$path = $path.'-'.$name.'.'.$ext;
/** @var \Intervention\Image\Image $image */
$image = InterventionImage::make($file);
$action = $size[2] ?? 'resize';
// Resize image with aspect ratio
$image->$action($size[0], $size[1], function (Constraint $constraint) {
$constraint->aspectRatio();
})->resizeCanvas($size[0], $size[1], 'center', false, '#ffffff');
if (!is_null($this->storagePermission)) {
$this->storage->put("{$this->getDirectory()}/{$path}", $image->encode(), $this->storagePermission);
} else {
$this->storage->put("{$this->getDirectory()}/{$path}", $image->encode());
}
}
$this->destroyThumbnail();
return $this;
}
}