|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Riclep\Storyblok\Fields; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use Riclep\Storyblok\Support\ImageTransformation; |
|
9
|
|
|
|
|
10
|
|
|
class Image extends Asset |
|
11
|
|
|
{ |
|
12
|
|
|
protected $transformations = []; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct($content, $block) |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
if (is_string($content)) { |
|
18
|
|
|
$this->upgradeOldFields($content); |
|
19
|
|
|
parent::__construct($this->content, $block); |
|
20
|
|
|
} else { |
|
21
|
|
|
parent::__construct($content, $block); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
if ($this->hasFile()) { |
|
25
|
|
|
$this->extractMetaDetails(); |
|
26
|
|
|
|
|
27
|
|
|
if (method_exists($this, 'transformations')) { |
|
28
|
|
|
$this->transformations(); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function transform($name = null) { |
|
34
|
|
|
if ($name) { |
|
35
|
|
|
if (array_key_exists($name, $this->transformations) ) { |
|
36
|
|
|
return $this->transformations[$name]; |
|
37
|
|
|
} |
|
38
|
|
|
return false; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return new ImageTransformation($this); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function width() { |
|
45
|
|
|
return $this->meta('width'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function height() { |
|
49
|
|
|
return $this->meta('height'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function type() { |
|
53
|
|
|
$extension = $this->meta('extension'); |
|
54
|
|
|
|
|
55
|
|
|
if ($extension === 'jpg') { |
|
56
|
|
|
$extension = 'jpeg'; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return 'image/' . $extension; |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function setTransformations($transformations) { |
|
63
|
|
|
$this->transformations = $transformations; |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function picture($alt = '', $default = null, $attributes = [], $view = 'laravel-storyblok::picture-element', $reverse = false) { |
|
69
|
|
|
if ($default) { |
|
70
|
|
|
$imgSrc = (string) $this->transformations[$default]['src']; |
|
71
|
|
|
} else { |
|
72
|
|
|
$imgSrc = $this->filename; |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// srcset seems to work the opposite way to picture elements when working out sizes |
|
76
|
|
|
if ($reverse) { |
|
77
|
|
|
$transformations = array_reverse($this->transformations); |
|
78
|
|
|
} else { |
|
79
|
|
|
$transformations = $this->transformations; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return view($view, [ |
|
83
|
|
|
'alt' => $alt, |
|
84
|
|
|
'attributes' => $attributes, |
|
85
|
|
|
'default' => $default, |
|
86
|
|
|
'imgSrc' => $imgSrc, |
|
87
|
|
|
'transformations' => $transformations, |
|
88
|
|
|
])->render(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function srcset($alt = '', $default = null, $attributes = [], $view = 'laravel-storyblok::srcset') { |
|
|
|
|
|
|
92
|
|
|
return $this->picture($alt, $default, $attributes, 'laravel-storyblok::srcset', true); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function cssVars() { |
|
96
|
|
|
if ($this->transformations) { |
|
|
|
|
|
|
97
|
|
|
$vars = ''; |
|
98
|
|
|
|
|
99
|
|
|
foreach ($this->transformations as $key => $transformation) { |
|
100
|
|
|
if (Str::endsWith($this->filename, 'svg')) { |
|
101
|
|
|
$vars .= '--' . $key . ': url("' . $this->filename . '"); '; |
|
102
|
|
|
} else { |
|
103
|
|
|
$vars .= '--' . $key . ': url("https:' . (string) $transformation['src'] . '"); '; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $vars; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return false; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
protected function getOriginalFilenameAttribute() { |
|
114
|
|
|
return $this->content['filename']; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
protected function extractMetaDetails() { |
|
118
|
|
|
$path = $this->content['filename']; |
|
119
|
|
|
|
|
120
|
|
|
preg_match_all('/(?<width>\d+)x(?<height>\d+).+\.(?<extension>[a-z]{3,4})/mi', $path, $dimensions, PREG_SET_ORDER, 0); |
|
121
|
|
|
|
|
122
|
|
|
if (Str::endsWith(strtolower($this->content['filename']), '.svg')) { |
|
123
|
|
|
$this->addMeta([ |
|
124
|
|
|
'height' => false, |
|
125
|
|
|
'width' => false, |
|
126
|
|
|
'extension' => 'svg', |
|
127
|
|
|
]); |
|
128
|
|
|
} else { |
|
129
|
|
|
$this->addMeta([ |
|
130
|
|
|
'height' => $dimensions[0]['height'], |
|
131
|
|
|
'width' => $dimensions[0]['width'], |
|
132
|
|
|
'extension' => strtolower($dimensions[0]['extension']), |
|
133
|
|
|
]); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
private function upgradeOldFields($content) { |
|
138
|
|
|
$this->content = [ |
|
139
|
|
|
'filename' => $content, |
|
140
|
|
|
'alt' => null, |
|
141
|
|
|
'copyright' => null, |
|
142
|
|
|
'fieltype' => 'asset', |
|
143
|
|
|
'focus' => null, |
|
144
|
|
|
'name' => '', |
|
145
|
|
|
'title' => null, |
|
146
|
|
|
]; |
|
147
|
|
|
} |
|
148
|
|
|
} |