1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Riclep\Storyblok\Support\ImageTransformers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Imgix\UrlBuilder; |
7
|
|
|
|
8
|
|
|
class Imgix extends BaseTransformer |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Resize the image to the given dimensions |
13
|
|
|
* |
14
|
|
|
* @param $width |
15
|
|
|
* @param $height |
16
|
|
|
* @return $this |
17
|
|
|
*/ |
18
|
|
|
public function resize($width = 0, $height = 0) |
19
|
|
|
{ |
20
|
|
|
$this->transformations = array_merge($this->transformations, [ |
21
|
|
|
'w' => $width, |
22
|
|
|
'h' => $height, |
23
|
|
|
]); |
24
|
|
|
|
25
|
|
|
//dd($this); |
26
|
|
|
|
27
|
|
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Fit the image in the given dimensions |
32
|
|
|
* |
33
|
|
|
* @param $mode |
34
|
|
|
* @param $options |
35
|
|
|
* @return $this |
36
|
|
|
*/ |
37
|
|
|
public function fit($mode, $options = []) |
38
|
|
|
{ |
39
|
|
|
$this->transformations = array_merge($this->transformations, [ |
40
|
|
|
'fit' => $mode |
41
|
|
|
]); |
42
|
|
|
|
43
|
|
|
$this->transformations = array_merge($this->transformations, $options); |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Specify the crop type to use for the image |
50
|
|
|
* |
51
|
|
|
* @param $mode |
52
|
|
|
* @param $options |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
|
|
public function crop($mode, $options = []) |
56
|
|
|
{ |
57
|
|
|
$this->transformations = array_merge($this->transformations, [ |
58
|
|
|
'crop' => $mode |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
$this->transformations = array_merge($this->transformations, $options); |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set the image format you want returned |
68
|
|
|
* |
69
|
|
|
* @param $format |
70
|
|
|
* @param $quality |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function format($format, $quality = null) |
74
|
|
|
{ |
75
|
|
|
if ($format === 'auto') { |
76
|
|
|
$this->transformations = array_merge($this->transformations, [ |
77
|
|
|
'auto' => 'format', |
78
|
|
|
]); |
79
|
|
|
} else { |
80
|
|
|
$this->transformations = array_merge($this->transformations, [ |
81
|
|
|
'fm' => $format, |
82
|
|
|
]); |
83
|
|
|
|
84
|
|
|
if ($quality) { |
85
|
|
|
$this->transformations = array_merge($this->transformations, [ |
86
|
|
|
'q' => $quality, |
87
|
|
|
]); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Manually set any options you want for the transformation as |
96
|
|
|
* and array of key value pairs |
97
|
|
|
* |
98
|
|
|
* @param $options |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function options($options) |
102
|
|
|
{ |
103
|
|
|
$this->transformations = array_merge($this->transformations, $options); |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Returns an imgix URL using their builder |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function buildUrl() { |
115
|
|
|
if ($this->transformations === 'svg') { |
|
|
|
|
116
|
|
|
return $this->image->content()['filename']; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$builder = new UrlBuilder(config('storyblok.imgix_domain')); |
120
|
|
|
$builder->setUseHttps(true); |
121
|
|
|
$builder->setSignKey(config('storyblok.imgix_token')); |
122
|
|
|
|
123
|
|
|
return $builder->createURL($this->image->content()['filename'], $this->transformations); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Gets the image meta from the given Storyblok URL |
128
|
|
|
* |
129
|
|
|
* @return void|null |
130
|
|
|
*/ |
131
|
|
|
protected function extractMetaDetails() { |
132
|
|
|
$path = $this->image->content()['filename']; |
133
|
|
|
|
134
|
|
|
preg_match_all('/(?<width>\d+)x(?<height>\d+).+\.(?<extension>[a-z]{3,4})/mi', $path, $dimensions, PREG_SET_ORDER, 0); |
135
|
|
|
|
136
|
|
|
if ($dimensions) { |
137
|
|
|
if (Str::endsWith(strtolower($this->image->content()['filename']), '.svg')) { |
138
|
|
|
$this->meta = [ |
139
|
|
|
'height' => false, |
140
|
|
|
'width' => false, |
141
|
|
|
'extension' => 'svg', |
142
|
|
|
'mime' => 'image/svg+xml', |
143
|
|
|
]; |
144
|
|
|
} else { |
145
|
|
|
$this->meta = [ |
146
|
|
|
'height' => $dimensions[0]['height'], |
147
|
|
|
'width' => $dimensions[0]['width'], |
148
|
|
|
'extension' => strtolower($dimensions[0]['extension']), |
149
|
|
|
'mime' => $this->setMime(strtolower($dimensions[0]['extension'])), |
150
|
|
|
]; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |