1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Models; |
4
|
|
|
|
5
|
|
|
use Hyde\Framework\Actions\FindsContentLengthForImageObject; |
6
|
|
|
use Hyde\Framework\Hyde; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Holds the information for an image. |
10
|
|
|
* |
11
|
|
|
* $schema = [ |
12
|
|
|
* 'path' => '?string', |
13
|
|
|
* 'uri' => '?string', |
14
|
|
|
* 'description' => '?string', |
15
|
|
|
* 'title' => '?string', |
16
|
|
|
* 'copyright' => '?string', |
17
|
|
|
* 'license' => '?string', |
18
|
|
|
* 'licenseUrl' => '?string', |
19
|
|
|
* 'author' => '?string', |
20
|
|
|
* 'credit' => '?string' |
21
|
|
|
* ]; |
22
|
|
|
*/ |
23
|
|
|
class Image implements \Stringable |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The image's path (if it is stored locally (in the _media directory)). |
27
|
|
|
* Example: image.jpg. |
28
|
|
|
* |
29
|
|
|
* @var string|null |
30
|
|
|
*/ |
31
|
|
|
public ?string $path; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The image's URI (if stored externally). |
35
|
|
|
* Example: https://example.com/media/image.jpg. |
36
|
|
|
* |
37
|
|
|
* Will override the path property if both are set. |
38
|
|
|
* |
39
|
|
|
* @var string|null |
40
|
|
|
*/ |
41
|
|
|
public ?string $uri; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The image's description. (Used for alt text for screen readers.) |
45
|
|
|
* You should always set this to provide accessibility. |
46
|
|
|
* Example: "This is an image of a cat sitting in a basket.". |
47
|
|
|
* |
48
|
|
|
* @var string|null |
49
|
|
|
*/ |
50
|
|
|
public ?string $description; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The image's title. (Shows a tooltip on hover.) |
54
|
|
|
* Example: "My Cat Archer". |
55
|
|
|
* |
56
|
|
|
* @var string|null |
57
|
|
|
*/ |
58
|
|
|
public ?string $title; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The image's copyright. |
62
|
|
|
* Example: "Copyright (c) 2020 John Doe". |
63
|
|
|
* |
64
|
|
|
* @var string|null |
65
|
|
|
*/ |
66
|
|
|
public ?string $copyright; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The image's license name. |
70
|
|
|
* Example: "CC BY-NC-SA 4.0". |
71
|
|
|
* |
72
|
|
|
* @var string|null |
73
|
|
|
*/ |
74
|
|
|
public ?string $license; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* The image's license URL. |
78
|
|
|
* Example: "https://creativecommons.org/licenses/by-nc-sa/4.0/". |
79
|
|
|
* |
80
|
|
|
* @var string|null |
81
|
|
|
*/ |
82
|
|
|
public ?string $licenseUrl; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* The image's author. |
86
|
|
|
* Example: "John Doe". |
87
|
|
|
* |
88
|
|
|
* @var string|null |
89
|
|
|
*/ |
90
|
|
|
public ?string $author; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* The image's source (for attribution). |
94
|
|
|
* Example: "https://unsplash.com/photos/example". |
95
|
|
|
* |
96
|
|
|
* @var string|null |
97
|
|
|
*/ |
98
|
|
|
public ?string $credit = null; |
99
|
|
|
|
100
|
|
|
public function __construct(array $data = []) |
101
|
|
|
{ |
102
|
|
|
foreach ($data as $key => $value) { |
103
|
|
|
$this->{$key} = $value; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (isset($this->path)) { |
107
|
|
|
$this->path = basename($this->path); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** @inheritDoc */ |
112
|
|
|
public function __toString() |
113
|
|
|
{ |
114
|
|
|
return $this->getLink(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** Dynamically create an image based on string or front matter array */ |
118
|
|
|
public static function make(string|array $data): static |
119
|
|
|
{ |
120
|
|
|
if (is_string($data)) { |
|
|
|
|
121
|
|
|
return static::fromSource($data); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return new static($data); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public static function fromSource(string $image): static |
128
|
|
|
{ |
129
|
|
|
return str_starts_with($image, 'http') |
130
|
|
|
? new static(['uri' => $image]) |
131
|
|
|
: new static(['path' => $image]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function getSource(): ?string |
135
|
|
|
{ |
136
|
|
|
return $this->uri ?? $this->getPath() ?? null; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function getLink(): string |
140
|
|
|
{ |
141
|
|
|
if (! $this->getSource()) { |
142
|
|
|
return ''; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return Hyde::image($this->getSource()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function getContentLength(): int |
149
|
|
|
{ |
150
|
|
|
return (new FindsContentLengthForImageObject($this))->execute(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function getImageAuthorAttributionString(): string|null |
154
|
|
|
{ |
155
|
|
|
if (isset($this->author)) { |
156
|
|
|
if (isset($this->credit)) { |
157
|
|
|
return '<span itemprop="creator" itemscope="" itemtype="http://schema.org/Person"><a href="'.e($this->credit).'" rel="author noopener nofollow" itemprop="url"><span itemprop="name">'.e($this->author).'</span></a></span>'; |
158
|
|
|
} else { |
159
|
|
|
return '<span itemprop="creator" itemscope="" itemtype="http://schema.org/Person"><span itemprop="name">'.e($this->author).'</span></span>'; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return null; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function getCopyrightString(): string|null |
167
|
|
|
{ |
168
|
|
|
if (isset($this->copyright)) { |
169
|
|
|
return '<span itemprop="copyrightNotice">'.e($this->copyright).'</span>'; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return null; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function getLicenseString(): string|null |
176
|
|
|
{ |
177
|
|
|
if (isset($this->license) && isset($this->licenseUrl)) { |
178
|
|
|
return '<a href="'.e($this->licenseUrl).'" rel="license nofollow noopener" itemprop="license">'.e($this->license).'</a>'; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if (isset($this->license)) { |
182
|
|
|
return '<span itemprop="license">'.e($this->license).'</span>'; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return null; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function getFluentAttribution(): string |
189
|
|
|
{ |
190
|
|
|
$attribution = []; |
191
|
|
|
|
192
|
|
|
$getImageAuthorAttributionString = $this->getImageAuthorAttributionString(); |
193
|
|
|
if ($getImageAuthorAttributionString !== null) { |
194
|
|
|
$attribution[] = 'Image by '.$getImageAuthorAttributionString; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
$getCopyrightString = $this->getCopyrightString(); |
198
|
|
|
if ($getCopyrightString !== null) { |
199
|
|
|
$attribution[] = $getCopyrightString; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$getLicenseString = $this->getLicenseString(); |
203
|
|
|
if ($getLicenseString !== null) { |
204
|
|
|
$attribution[] = 'License '.$getLicenseString; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return implode('. ', $attribution); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Used in resources\views\components\post\image.blade.php to add meta tags with itemprop attributes. |
212
|
|
|
* |
213
|
|
|
* @return array |
214
|
|
|
*/ |
215
|
|
|
public function getMetadataArray(): array |
216
|
|
|
{ |
217
|
|
|
$metadata = []; |
218
|
|
|
|
219
|
|
|
if (isset($this->description)) { |
220
|
|
|
$metadata['text'] = $this->description; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
if (isset($this->title)) { |
224
|
|
|
$metadata['name'] = $this->title; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$metadata['url'] = $this->getLink(); |
228
|
|
|
$metadata['contentUrl'] = $this->getLink(); |
229
|
|
|
|
230
|
|
|
return $metadata; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
protected function getPath(): ?string |
234
|
|
|
{ |
235
|
|
|
if (isset($this->path)) { |
236
|
|
|
return basename($this->path); |
|
|
|
|
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return null; |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|