-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathSharpFormFieldWithUpload.php
More file actions
156 lines (121 loc) · 3.92 KB
/
SharpFormFieldWithUpload.php
File metadata and controls
156 lines (121 loc) · 3.92 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
<?php
namespace Code16\Sharp\Form\Fields\Utils;
use Closure;
trait SharpFormFieldWithUpload
{
protected ?float $maxFileSize = null;
protected ?array $cropRatio = null;
protected ?array $transformableFileTypes = null;
protected string $storageDisk = 'local';
protected string|Closure $storageBasePath = 'data';
protected bool $transformable = true;
protected ?bool $transformKeepOriginal = null;
protected bool $compactThumbnail = false;
protected bool $shouldOptimizeImage = false;
protected bool $shouldSanitizeSvg = true;
protected string|array|null $fileFilter = null;
public function setMaxFileSize(float $maxFileSizeInMB): self
{
$this->maxFileSize = $maxFileSizeInMB;
return $this;
}
public function setCropRatio(?string $ratio = null, ?array $transformableFileTypes = null): self
{
if ($ratio) {
$this->cropRatio = explode(':', $ratio);
$this->transformableFileTypes = $transformableFileTypes
? $this->formatFileExtension($transformableFileTypes)
: null;
} else {
$this->cropRatio = null;
$this->transformableFileTypes = null;
}
return $this;
}
public function shouldOptimizeImage(bool $shouldOptimizeImage = true): self
{
$this->shouldOptimizeImage = $shouldOptimizeImage;
return $this;
}
public function isShouldOptimizeImage(): bool
{
return $this->shouldOptimizeImage;
}
public function setSanitizeSvg(bool $shouldSanitizeSvg = true): self
{
$this->shouldSanitizeSvg = $shouldSanitizeSvg;
return $this;
}
public function isShouldSanitizeSvg(): bool
{
return $this->shouldSanitizeSvg;
}
public function setCompactThumbnail(bool $compactThumbnail = true): self
{
$this->compactThumbnail = $compactThumbnail;
return $this;
}
public function setTransformable(bool $transformable = true, ?bool $transformKeepOriginal = null): self
{
$this->transformable = $transformable;
if ($transformable && ! is_null($transformKeepOriginal)) {
$this->transformKeepOriginal = $transformKeepOriginal;
}
return $this;
}
public function isTransformOriginal(): bool
{
return $this->transformable && ! $this->transformKeepOriginal();
}
protected function transformKeepOriginal(): bool
{
return $this->transformKeepOriginal ?? config('sharp.uploads.transform_keep_original_image', true);
}
public function setStorageDisk(string $storageDisk): self
{
$this->storageDisk = $storageDisk;
return $this;
}
public function setStorageBasePath(string|Closure $storageBasePath): self
{
$this->storageBasePath = $storageBasePath;
return $this;
}
public function setFileFilter(string|array $fileFilter): self
{
$this->fileFilter = $this->formatFileExtension($fileFilter);
return $this;
}
public function setFileFilterImages(): self
{
$this->setFileFilter(['.jpg', '.jpeg', '.gif', '.png']);
return $this;
}
public function storageDisk(): string
{
return $this->storageDisk;
}
public function storageBasePath(): string
{
return value($this->storageBasePath);
}
public function cropRatio(): array
{
return $this->cropRatio;
}
private function formatFileExtension(string|array $fileFilter): array
{
if (! is_array($fileFilter)) {
$fileFilter = explode(',', $fileFilter);
}
return collect($fileFilter)
->map(function ($filter) {
$filter = trim($filter);
if (! str_starts_with($filter, '.')) {
$filter = ".$filter";
}
return $filter;
})
->all();
}
}