1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Riclep\Storyblok\Support; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Riclep\Storyblok\Fields\Image; |
8
|
|
|
|
9
|
|
|
class ImageTransformation |
10
|
|
|
{ |
11
|
|
|
private $filename; |
12
|
|
|
private $focus; |
13
|
|
|
protected $transformations = []; |
14
|
|
|
private $image; |
15
|
|
|
|
16
|
|
|
public function __construct(Image $image) |
17
|
|
|
{ |
18
|
|
|
$this->filename = $image->filename; |
|
|
|
|
19
|
|
|
$this->focus = $image->focus; |
|
|
|
|
20
|
|
|
$this->image = $image; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function width() { |
24
|
|
|
return $this->transformations['width'] ?? $this->image->width(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function height() { |
28
|
|
|
return $this->transformations['height'] ?? $this->image->height(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function type() { |
32
|
|
|
if (array_key_exists('format', $this->transformations)) { |
33
|
|
|
return 'image/' . $this->transformations['format']; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $this->image->type(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function resize($width = 0, $height = 0, $focus = null) |
40
|
|
|
{ |
41
|
|
|
$this->transformations = array_merge($this->transformations, [ |
42
|
|
|
'width' => $width, |
43
|
|
|
'height' => $height, |
44
|
|
|
]); |
45
|
|
|
|
46
|
|
|
if ($focus) { |
47
|
|
|
$this->transformations = array_merge($this->transformations, [ |
48
|
|
|
'focus' => $focus, |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function format($format, $quality = null) |
56
|
|
|
{ |
57
|
|
|
$this->transformations = array_merge($this->transformations, [ |
58
|
|
|
'format' => $format, |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
if ($quality) { |
62
|
|
|
$this->transformations = array_merge($this->transformations, [ |
63
|
|
|
'quality' => $quality, |
64
|
|
|
]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function fitIn($width = 0, $height = 0, $fill = 'transparent') |
71
|
|
|
{ |
72
|
|
|
$this->transformations = array_merge($this->transformations, [ |
73
|
|
|
'width' => $width, |
74
|
|
|
'height' => $height, |
75
|
|
|
'fill' => $fill, |
76
|
|
|
'fit-in' => true, |
77
|
|
|
]); |
78
|
|
|
|
79
|
|
|
// has to be an image that supports transparency |
80
|
|
|
if ($fill === 'transparent') { |
81
|
|
|
$this->format('png'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Transforms the image using the Storyblok image service |
89
|
|
|
* See: https://www.storyblok.com/docs/image-service |
90
|
|
|
* |
91
|
|
|
* @param $options |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function createUrl($options): string |
95
|
|
|
{ |
96
|
|
|
$resource = str_replace(['https:', '//a.storyblok.com'], '', $this->filename); |
97
|
|
|
return '//img2.storyblok.com' . $options . $resource; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getTransformations() { |
101
|
|
|
return $this->transformations; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function __toString() |
105
|
|
|
{ |
106
|
|
|
$transforms = ''; |
107
|
|
|
|
108
|
|
|
if (array_key_exists('fit-in', $this->transformations)) { |
109
|
|
|
$transforms .= '/fit-in'; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (array_key_exists('width', $this->transformations)) { |
113
|
|
|
$transforms .= '/' . $this->transformations['width'] . 'x' . $this->transformations['height']; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (array_key_exists('focus', $this->transformations) && $this->transformations['focus'] === 'smart') { |
117
|
|
|
$transforms .= '/smart'; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// filters |
121
|
|
|
if (array_key_exists('format', $this->transformations) || array_key_exists('quality', $this->transformations) || array_key_exists('fill', $this->transformations) || (array_key_exists('focus', $this->transformations) && $this->transformations['focus'] === 'focal-point')) { |
122
|
|
|
$transforms .= '/filters'; |
123
|
|
|
|
124
|
|
|
if (array_key_exists('format', $this->transformations)) { |
125
|
|
|
$transforms .= ':format(' . $this->transformations['format'] . ')'; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (array_key_exists('quality', $this->transformations)) { |
129
|
|
|
$transforms .= ':quality(' . $this->transformations['quality'] . ')'; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if (array_key_exists('fill', $this->transformations)) { |
133
|
|
|
$transforms .= ':fill(' . $this->transformations['fill'] . ')'; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (array_key_exists('focus', $this->transformations) && $this->transformations['focus'] === 'focal-point' && $this->focus) { |
137
|
|
|
$transforms .= ':focal(' . $this->focus . ')'; |
|
|
|
|
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this->createUrl($transforms); |
142
|
|
|
} |
143
|
|
|
} |