|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Riclep\Storyblok\Support\ImageTransformers; |
|
4
|
|
|
|
|
5
|
|
|
use Riclep\Storyblok\Fields\Image; |
|
6
|
|
|
|
|
7
|
|
|
abstract class BaseTransformer |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Stores basic details about the image |
|
11
|
|
|
* |
|
12
|
|
|
* @var |
|
13
|
|
|
*/ |
|
14
|
|
|
protected array $meta = [ |
|
15
|
|
|
'height' => null, |
|
16
|
|
|
'width' => null, |
|
17
|
|
|
'extension' => null, |
|
18
|
|
|
'mime' => null, |
|
19
|
|
|
]; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Stores all the transformations |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected array $transformations = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return string The transformed image URL |
|
30
|
|
|
*/ |
|
31
|
|
|
abstract public function buildUrl(): string; |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Extracts meta details for the current image such as width |
|
36
|
|
|
* and height, mime and anything else of use |
|
37
|
|
|
* |
|
38
|
|
|
* @return void |
|
39
|
|
|
*/ |
|
40
|
|
|
abstract protected function extractMetaDetails(): void; |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param Image $image |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(protected Image $image) { |
|
47
|
|
|
|
|
48
|
|
|
if (method_exists('preprocess', $this)) { |
|
49
|
|
|
$this->preprocess(); |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Returns the width of the transformed image. Optionally you |
|
55
|
|
|
* can request the original width |
|
56
|
|
|
* |
|
57
|
|
|
* @param bool $original |
|
58
|
|
|
* @return int |
|
59
|
|
|
*/ |
|
60
|
|
|
public function width(bool $original = false): int |
|
61
|
|
|
{ |
|
62
|
|
|
if ($original) { |
|
63
|
|
|
return (int) $this->meta['width']; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// the width was not set so we need to compute it |
|
67
|
|
|
if (array_key_exists('width', $this->transformations) && $this->transformations['width'] === 0) { |
|
68
|
|
|
$scalePercent = round(($this->height() / $this->height(true) * 100), 2); |
|
69
|
|
|
|
|
70
|
|
|
return round((int) $this->meta['width'] / 100 * $scalePercent); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $this->transformations['width'] ?? (int) $this->meta['width']; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Returns the height of the transformed image. Optionally you |
|
78
|
|
|
* can request the original height |
|
79
|
|
|
* |
|
80
|
|
|
* @param bool $original |
|
81
|
|
|
* @return int |
|
82
|
|
|
*/ |
|
83
|
|
|
public function height(bool $original = false): ?int |
|
84
|
|
|
{ |
|
85
|
|
|
if ($original) { |
|
86
|
|
|
return (int) $this->meta['height']; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// the height was not set so we need to compute it |
|
90
|
|
|
if (array_key_exists('height', $this->transformations) && $this->transformations['height'] === 0) { |
|
91
|
|
|
$scalePercent = round(($this->width() / $this->width(true) * 100), 2); |
|
92
|
|
|
|
|
93
|
|
|
return round((int) $this->meta['height'] / 100 * $scalePercent); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
return $this->transformations['height'] ?? $this->meta['height']; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Returns the mime of the transformed image. Optionally you |
|
102
|
|
|
* can request the original mime |
|
103
|
|
|
* |
|
104
|
|
|
* @param bool $original |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
public function mime(bool $original = false): ?string |
|
108
|
|
|
{ |
|
109
|
|
|
if ($original) { |
|
110
|
|
|
return $this->meta['mime']; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $this->transformations['mime'] ?? $this->meta['mime']; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Returns the mime from a particular file extension |
|
118
|
|
|
* |
|
119
|
|
|
* @param $extension |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function setMime($extension): string |
|
123
|
|
|
{ |
|
124
|
|
|
return $extension === 'jpg' ? 'image/jpeg' : 'image/' . $extension; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Casts the image transformation as a sting using the |
|
129
|
|
|
* buildUrl method |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
public function __toString(): string |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->buildUrl(); |
|
136
|
|
|
} |
|
137
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.