Total Complexity | 45 |
Total Lines | 314 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 4 | Features | 0 |
Complex classes like Image often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Image, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Image |
||
29 | { |
||
30 | /** |
||
31 | * Create new manager instance with available driver. |
||
32 | */ |
||
33 | private static function manager(): ImageManager |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Scales down an image Asset to the given width, keeping the aspect ratio. |
||
63 | * |
||
64 | * @throws RuntimeException |
||
65 | */ |
||
66 | public static function resize(Asset $asset, int $width, int $quality): string |
||
67 | { |
||
68 | try { |
||
69 | // creates image object from source |
||
70 | $image = self::manager()->read($asset['content']); |
||
71 | // resizes to $width with constraint the aspect-ratio and unwanted upsizing |
||
72 | $image->scaleDown(width: $width); |
||
73 | // return image data |
||
74 | return (string) $image->encodeByMediaType( |
||
75 | $asset['subtype'], |
||
76 | /** @scrutinizer ignore-type */ |
||
77 | progressive: true, |
||
78 | /** @scrutinizer ignore-type */ |
||
79 | interlaced: false, |
||
80 | quality: $quality |
||
81 | ); |
||
82 | } catch (\Exception $e) { |
||
83 | throw new RuntimeException(\sprintf('Asset "%s" can\'t be resized: %s', $asset['path'], $e->getMessage())); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Crops an image Asset to the given width and height, keeping the aspect ratio. |
||
89 | * |
||
90 | * @throws RuntimeException |
||
91 | */ |
||
92 | public static function cover(Asset $asset, int $width, int $height, int $quality): string |
||
93 | { |
||
94 | try { |
||
95 | // creates image object from source |
||
96 | $image = self::manager()->read($asset['content']); |
||
97 | // turns an animated image (i.e GIF) into a static image |
||
98 | if ($image->isAnimated()) { |
||
99 | $image = $image->removeAnimation('25%'); // use 25% to avoid an "empty" frame |
||
100 | } |
||
101 | // crops the image |
||
102 | $image->cover( |
||
103 | width: $width, |
||
104 | height: $height, |
||
105 | position: 'center' |
||
106 | ); |
||
107 | // return image data |
||
108 | return (string) $image->encodeByMediaType( |
||
109 | $asset['subtype'], |
||
110 | /** @scrutinizer ignore-type */ |
||
111 | progressive: true, |
||
112 | /** @scrutinizer ignore-type */ |
||
113 | interlaced: false, |
||
114 | quality: $quality |
||
115 | ); |
||
116 | } catch (\Exception $e) { |
||
117 | throw new RuntimeException(\sprintf('Asset "%s" can\'t be cropped: %s', $asset['path'], $e->getMessage())); |
||
118 | } |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Makes an image Asset maskable, meaning it can be used as a PWA icon. |
||
123 | * |
||
124 | * @throws RuntimeException |
||
125 | */ |
||
126 | public static function maskable(Asset $asset, int $quality, int $padding): string |
||
127 | { |
||
128 | try { |
||
129 | // creates image object from source |
||
130 | $source = self::manager()->read($asset['content']); |
||
131 | // creates a new image with the dominant color as background |
||
132 | // and the size of the original image plus the padding |
||
133 | $image = self::manager()->create( |
||
134 | width: (int) round($asset['width'] * (1 + $padding / 100), 0), |
||
135 | height: (int) round($asset['height'] * (1 + $padding / 100), 0) |
||
136 | )->fill(self::getDominantColor($asset)); |
||
137 | // inserts the original image in the center |
||
138 | $image->place( |
||
139 | $source, |
||
140 | position: 'center' |
||
141 | ); |
||
142 | // scales down the new image to the original image size |
||
143 | $image->scaleDown(width: $asset['width']); |
||
144 | // return image data |
||
145 | return (string) $image->encodeByMediaType( |
||
146 | $asset['subtype'], |
||
147 | /** @scrutinizer ignore-type */ |
||
148 | progressive: true, |
||
149 | /** @scrutinizer ignore-type */ |
||
150 | interlaced: false, |
||
151 | quality: $quality |
||
152 | ); |
||
153 | } catch (\Exception $e) { |
||
154 | throw new RuntimeException(\sprintf('Can\'t make Asset "%s" maskable: %s', $asset['path'], $e->getMessage())); |
||
155 | } |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Converts an image Asset to the target format. |
||
160 | * |
||
161 | * @throws RuntimeException |
||
162 | */ |
||
163 | public static function convert(Asset $asset, string $format, int $quality): string |
||
164 | { |
||
165 | try { |
||
166 | $image = self::manager()->read($asset['content']); |
||
167 | |||
168 | if (!\function_exists("image$format")) { |
||
169 | throw new RuntimeException(\sprintf('Function "image%s" is not available.', $format)); |
||
170 | } |
||
171 | |||
172 | return (string) $image->encodeByExtension( |
||
173 | $format, |
||
174 | /** @scrutinizer ignore-type */ |
||
175 | progressive: true, |
||
176 | /** @scrutinizer ignore-type */ |
||
177 | interlaced: false, |
||
178 | quality: $quality |
||
179 | ); |
||
180 | } catch (\Exception $e) { |
||
181 | throw new RuntimeException(\sprintf('Not able to convert "%s" to %s: %s', $asset['path'], $format, $e->getMessage())); |
||
182 | } |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Returns the Data URL (encoded in Base64). |
||
187 | * |
||
188 | * @throws RuntimeException |
||
189 | */ |
||
190 | public static function getDataUrl(Asset $asset, int $quality): string |
||
191 | { |
||
192 | try { |
||
193 | $image = self::manager()->read($asset['content']); |
||
194 | |||
195 | return (string) $image->encode(new AutoEncoder(quality: $quality))->toDataUri(); |
||
196 | } catch (\Exception $e) { |
||
197 | throw new RuntimeException(\sprintf('Can\'t get Data URL of "%s": %s', $asset['path'], $e->getMessage())); |
||
198 | } |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Returns the dominant RGB color of an image asset. |
||
203 | * |
||
204 | * @throws RuntimeException |
||
205 | */ |
||
206 | public static function getDominantColor(Asset $asset): string |
||
207 | { |
||
208 | try { |
||
209 | $image = self::manager()->read(self::resize($asset, 100, 50)); |
||
210 | |||
211 | return $image->reduceColors(1)->pickColor(0, 0)->toString(); |
||
212 | } catch (\Exception $e) { |
||
213 | throw new RuntimeException(\sprintf('Can\'t get dominant color of "%s": %s', $asset['path'], $e->getMessage())); |
||
214 | } |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Returns a Low Quality Image Placeholder (LQIP) as data URL. |
||
219 | * |
||
220 | * @throws RuntimeException |
||
221 | */ |
||
222 | public static function getLqip(Asset $asset): string |
||
223 | { |
||
224 | try { |
||
225 | $image = self::manager()->read(self::resize($asset, 100, 50)); |
||
226 | |||
227 | return (string) $image->blur(50)->encode()->toDataUri(); |
||
228 | } catch (\Exception $e) { |
||
229 | throw new RuntimeException(\sprintf('can\'t create LQIP of "%s": %s', $asset['path'], $e->getMessage())); |
||
230 | } |
||
231 | } |
||
232 | |||
233 | /** |
||
234 | * Build the `srcset` attribute for responsive images. |
||
235 | * e.g.: `srcset="/img-480.jpg 480w, /img-800.jpg 800w"`. |
||
236 | * |
||
237 | * @throws RuntimeException |
||
238 | */ |
||
239 | public static function buildSrcset(Asset $asset, array $widths): string |
||
240 | { |
||
241 | if (!self::isImage($asset)) { |
||
242 | throw new RuntimeException(\sprintf('can\'t build "srcset" of "%s": it\'s not an image file.', $asset['path'])); |
||
243 | } |
||
244 | |||
245 | $srcset = ''; |
||
246 | $widthMax = 0; |
||
247 | sort($widths, SORT_NUMERIC); |
||
248 | $widths = array_reverse($widths); |
||
249 | foreach ($widths as $width) { |
||
250 | if ($asset['width'] < $width) { |
||
251 | continue; |
||
252 | } |
||
253 | $img = $asset->resize($width); |
||
254 | $srcset .= \sprintf('%s %sw, ', (string) $img, $width); |
||
255 | $widthMax = $width; |
||
256 | } |
||
257 | // adds source image |
||
258 | if (!empty($srcset) && ($asset['width'] < max($widths) && $asset['width'] != $widthMax)) { |
||
259 | $srcset .= \sprintf('%s %sw', (string) $asset, $asset['width']); |
||
260 | } |
||
261 | |||
262 | return rtrim($srcset, ', '); |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Returns the value of the "sizes" attribute corresponding to the configured class. |
||
267 | */ |
||
268 | public static function getSizes(string $class, array $sizes = []): string |
||
269 | { |
||
270 | $result = ''; |
||
271 | $classArray = explode(' ', $class); |
||
272 | foreach ($classArray as $class) { |
||
273 | if (\array_key_exists($class, $sizes)) { |
||
274 | $result = $sizes[$class] . ', '; |
||
275 | } |
||
276 | } |
||
277 | if (!empty($result)) { |
||
278 | return trim($result, ', '); |
||
279 | } |
||
280 | |||
281 | return $sizes['default'] ?? '100vw'; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Checks if an asset is an animated GIF. |
||
286 | */ |
||
287 | public static function isAnimatedGif(Asset $asset): bool |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Returns true if asset is a SVG. |
||
300 | */ |
||
301 | public static function isSVG(Asset $asset): bool |
||
302 | { |
||
303 | return \in_array($asset['subtype'], ['image/svg', 'image/svg+xml']) || $asset['ext'] == 'svg'; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Returns true if asset is an ICO. |
||
308 | */ |
||
309 | public static function isIco(Asset $asset): bool |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Asset is a valid image? |
||
316 | */ |
||
317 | public static function isImage(Asset $asset): bool |
||
318 | { |
||
319 | if ($asset['type'] !== 'image' || self::isSVG($asset) || self::isIco($asset)) { |
||
320 | return false; |
||
321 | } |
||
322 | |||
323 | return true; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Returns SVG attributes. |
||
328 | * |
||
329 | * @return \SimpleXMLElement|false |
||
330 | */ |
||
331 | public static function getSvgAttributes(Asset $asset) |
||
342 | } |
||
343 | } |
||
344 |