-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.php
More file actions
253 lines (208 loc) · 7.6 KB
/
Copy pathImage.php
File metadata and controls
253 lines (208 loc) · 7.6 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
namespace Flyo\Bridge;
use InvalidArgumentException;
/**
* The main intent for the image class is that if you define width and height, the image will be
* automatically resized to the given dimensions trough the flyo storage service but also define
* the width and height attributes in the html tag which allows good browser support for lazy loading
* which is included as well. Therefore its recommend to always defined width and height.
*
* ```php
* <div><?= Image::tag('test.jpg', 'Test Image', 300, 300); ?></div>
* ```
*
* Or only attributes:
*
* ```php
* <img <?= Image::attributes('test.jpg', 'Test Image', 300, 300); ?> />
* ```
*
* Responsive Images generator:
*
* <?= Image::tag((new Responsive('test.jpg'))->add(500, Responsive::PX_OR_LESS, 500, 500)->add(1000, Responsive::PX_OR_MORE, 1000, 1000)); ?>
*
* > Absolute paths starting with http/https or / won't be modified!
*
* @see https://dev.flyo.cloud/dev/infos/images
*/
class Image
{
public function __construct(
protected string|Responsive $src,
protected string $alt,
protected ?int $width = null,
protected ?int $height = null,
protected string $format = 'web',
protected string $loading = 'lazy',
protected string $decoding = 'async',
protected string $fetchpriority = 'auto'
) {
}
public static function attributes(string|Responsive $src, $alt, $width = null, $height = null, $format = 'webp', $loading = 'lazy', $decoding = 'async', $fetchpriority = 'auto'): string
{
return (new self($src, $alt, $width, $height, $format, $loading, $decoding, $fetchpriority))->toAttributes();
}
public static function fromObject(object $image, int $width, int $height, ?string $alt = null, string $format = 'webp', string $loading = 'lazy', string $decoding = 'async', string $fetchpriority = 'auto'): self
{
if (!property_exists($image, 'source') || empty($image->source)) {
throw new InvalidArgumentException('Image object must have a non-empty "source" property.');
}
return new self(
$image->source,
(property_exists($image, 'caption') && !empty($image->caption)) ? $image->caption : $alt,
$width,
$height,
$format,
$loading,
$decoding,
$fetchpriority
);
}
public static function source(string $src, $width = null, $height = null, $format = 'webp'): string
{
$image = new self($src, '', $width, $height, $format);
return $image->getSrc();
}
public static function tag(string|Responsive $src, $alt, $width = null, $height = null, $format = 'webp', $loading = 'lazy', $decoding = 'async', array $options = [], $fetchpriority = 'auto'): string
{
$attributes = self::attributes($src, $alt, $width, $height, $format, $loading, $decoding, $fetchpriority);
foreach ($options as $key => $value) {
$attributes .= sprintf(' %s="%s"', $key, $value);
}
return sprintf('<img %s />', $attributes);
}
public function getAlt(): string
{
return htmlspecialchars($this->alt, ENT_QUOTES);
}
public function getSrc(): string
{
$src = $this->src instanceof Responsive ? $this->src->src : $this->src;
// If the URL starts with 'http://' or 'https://' and is not from 'storage.flyo.cloud', return it directly
if (preg_match('#^https?:\/\/#', $src) && !str_contains($src, 'storage.flyo.cloud')) {
return $src;
}
if (str_starts_with($src, '/')) {
return $src;
}
$url = str_contains($src, 'https://storage.flyo.cloud') ? $src : 'https://storage.flyo.cloud/' . $src;
// If either width or height are defined, we add the /thumb/$widthx$height path to it.
$width = $this->getWidth();
$height = $this->getHeight();
if ($width !== null || $height !== null) {
$width ??= 'null';
$height ??= 'null';
$url .= sprintf('/thumb/%sx%s', $width, $height);
}
// if the original file name is already in the requested format, we don't add the format to the url.
$orginalFormat = pathinfo($url, PATHINFO_EXTENSION) ?: '';
if ($orginalFormat === $this->getFormat()) {
return $url;
}
return $url . '?format=' . $this->getFormat();
}
public function setDecoding(string $decoding): self
{
$this->decoding = $decoding;
return $this;
}
/**
* @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/decoding#sync
*/
public function getDecoding(): string
{
$decoding = strtolower($this->decoding);
if (!in_array($decoding, ['async', 'auto', 'sync'])) {
$decoding = 'auto';
}
return $decoding;
}
public function setFormat(string $format): self
{
$this->format = $format;
return $this;
}
public function getFormat(): string
{
$format = strtolower($this->format);
if (!in_array($format, ['webp', 'png', 'jpg', 'jpeg', 'gif'])) {
$format = 'webp';
}
return $format;
}
public function setHeight($height): self
{
$this->height = $height;
return $this;
}
public function getHeight(): ?int
{
return empty($this->height) ? null : (int) $this->height;
}
public function setLoading(string $loading): self
{
$this->loading = $loading;
return $this;
}
/**
* @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/loading
*/
public function getLoading(): string
{
return strtolower($this->loading) === 'lazy' ? 'lazy' : 'eager';
}
public function setFetchpriority(string $fetchpriority): self
{
$this->fetchpriority = $fetchpriority;
return $this;
}
/**
* @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/fetchPriority
*/
public function getFetchpriority(): string
{
$fetchpriority = strtolower($this->fetchpriority);
if (!in_array($fetchpriority, ['high', 'low', 'auto'])) {
$fetchpriority = 'auto';
}
return $fetchpriority;
}
public function setWidth($width): self
{
$this->width = $width;
return $this;
}
public function getWidth(): ?int
{
return empty($this->width) ? null : (int) $this->width;
}
public function toAttributes(): string
{
$attributes = [
sprintf('src="%s"', $this->getSrc()),
sprintf('alt="%s"', $this->getAlt()),
sprintf('loading="%s"', $this->getLoading()),
sprintf('decoding="%s"', $this->getDecoding()),
sprintf('fetchpriority="%s"', $this->getFetchpriority()),
];
if ($this->getwidth()) {
$attributes[] = sprintf('width="%s"', $this->getwidth());
}
if ($this->getHeight()) {
$attributes[] = sprintf('height="%s"', $this->getHeight());
}
if ($this->src instanceof Responsive) {
$attributes[] = sprintf('srcset="%s"', $this->src->getSrcset($this));
$attributes[] = sprintf('sizes="%s"', $this->src->getSizes($this));
}
return implode(' ', $attributes);
}
public function toTag(array $options = []): string
{
$attributes = $this->toAttributes();
foreach ($options as $key => $value) {
$attributes .= sprintf(' %s="%s"', $key, $value);
}
return sprintf('<img %s />', $attributes);
}
}