Total Complexity | 57 |
Total Lines | 590 |
Duplicated Lines | 0 % |
Changes | 10 | ||
Bugs | 1 | Features | 0 |
Complex classes like OptimizedImage 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 OptimizedImage, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class OptimizedImage extends Model |
||
27 | { |
||
28 | // Public Properties |
||
29 | // ========================================================================= |
||
30 | |||
31 | /** |
||
32 | * @var string[] An array of optimized image variant URLs |
||
33 | */ |
||
34 | public $optimizedImageUrls = []; |
||
35 | |||
36 | /** |
||
37 | * @var string[] An array of optimized .webp image variant URLs |
||
38 | */ |
||
39 | public $optimizedWebPImageUrls = []; |
||
40 | |||
41 | /** |
||
42 | * @var int[] An array of the widths of the optimized image variants |
||
43 | */ |
||
44 | public $variantSourceWidths = []; |
||
45 | |||
46 | /** |
||
47 | * @var int[] An array of the heights of the optimized image variants |
||
48 | */ |
||
49 | public $variantHeights = []; |
||
50 | |||
51 | /** |
||
52 | * @var float[] An array of the x,y image focal point coords, ranging from 0.0 to 1.0 |
||
53 | */ |
||
54 | public $focalPoint; |
||
55 | |||
56 | /** |
||
57 | * @var int The width of the original source image |
||
58 | */ |
||
59 | public $originalImageWidth; |
||
60 | |||
61 | /** |
||
62 | * @var int The height of the original source image |
||
63 | */ |
||
64 | public $originalImageHeight; |
||
65 | |||
66 | /** |
||
67 | * @var string The base64 encoded placeholder LQIP image |
||
68 | */ |
||
69 | public $placeholder = ''; |
||
70 | |||
71 | /** |
||
72 | * @var string The base64 encoded placeholder LQIP SVG image |
||
73 | */ |
||
74 | public $placeholderSvg = ''; |
||
75 | |||
76 | /** |
||
77 | * @var string[] An array the 5 most dominant colors in the image |
||
78 | */ |
||
79 | public $colorPalette = []; |
||
80 | |||
81 | /** |
||
82 | * @var int The overall lightness of the image, from 0..100 |
||
83 | */ |
||
84 | public $lightness; |
||
85 | |||
86 | /** |
||
87 | * @var int The width of the placeholder image |
||
88 | */ |
||
89 | public $placeholderWidth; |
||
90 | |||
91 | /** |
||
92 | * @var int The height of the placeholder image |
||
93 | */ |
||
94 | public $placeholderHeight; |
||
95 | |||
96 | // Public Methods |
||
97 | // ========================================================================= |
||
98 | |||
99 | /** |
||
100 | * @inheritdoc |
||
101 | */ |
||
102 | public function rules(): array |
||
103 | { |
||
104 | return [ |
||
105 | ['optimizedImageUrls', ArrayValidator::class], |
||
106 | ['optimizedWebPImageUrls', ArrayValidator::class], |
||
107 | ['variantSourceWidths', ArrayValidator::class], |
||
108 | ['variantHeights', ArrayValidator::class], |
||
109 | ['focalPoint', 'safe'], |
||
110 | ['originalImageWidth', 'integer'], |
||
111 | ['originalImageHeight', 'integer'], |
||
112 | ['placeholder', 'string'], |
||
113 | ['placeholderSvg', 'string'], |
||
114 | ['colorPalette', ArrayValidator::class], |
||
115 | ['placeholderWidth', 'integer'], |
||
116 | ['placeholderHeight', 'integer'], |
||
117 | ]; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Return the first image variant URL or the specific one passed in via |
||
122 | * $width |
||
123 | * |
||
124 | * @param int $width |
||
125 | * |
||
126 | * @return \Twig\Markup|null |
||
127 | */ |
||
128 | public function src(int $width = 0): string |
||
129 | { |
||
130 | if (empty($width)) { |
||
131 | return Template::raw(reset($this->optimizedImageUrls)); |
||
132 | } |
||
133 | |||
134 | return Template::raw($this->optimizedImageUrls[$width] ?? ''); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Getter for CraftQL |
||
139 | * |
||
140 | * @param int $width |
||
141 | * |
||
142 | * @return null|string|\Twig\Markup |
||
143 | */ |
||
144 | public function getSrc(int $width = 0): string |
||
145 | { |
||
146 | return $this->src($width); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Return a string of image URLs and their sizes |
||
151 | * |
||
152 | * @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
||
153 | * srcsets |
||
154 | * |
||
155 | * @return \Twig\Markup|null |
||
156 | */ |
||
157 | public function srcset(bool $dpr = false): string |
||
158 | { |
||
159 | return Template::raw($this->getSrcsetFromArray($this->optimizedImageUrls, $dpr)); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Getter for CraftQL |
||
164 | * |
||
165 | * @param bool $dpr |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | public function getSrcset(bool $dpr = false): string |
||
170 | { |
||
171 | return $this->srcset($dpr); |
||
172 | } |
||
173 | /** |
||
174 | * Return a string of image URLs and their sizes that match $width |
||
175 | * |
||
176 | * @param int $width |
||
177 | * @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
||
178 | * srcsets |
||
179 | * |
||
180 | * @return \Twig\Markup|null |
||
181 | */ |
||
182 | public function srcsetWidth(int $width, bool $dpr = false): string |
||
183 | { |
||
184 | $subset = $this->getSrcsetSubsetArray($this->optimizedImageUrls, $width, 'width'); |
||
185 | |||
186 | return Template::raw($this->getSrcsetFromArray($subset, $dpr)); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Return a string of image URLs and their sizes that are at least $width |
||
191 | * or larger |
||
192 | * |
||
193 | * @param int $width |
||
194 | * @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
||
195 | * srcsets |
||
196 | * |
||
197 | * @return \Twig\Markup|null |
||
198 | */ |
||
199 | public function srcsetMinWidth(int $width, bool $dpr = false): string |
||
200 | { |
||
201 | $subset = $this->getSrcsetSubsetArray($this->optimizedImageUrls, $width, 'minwidth'); |
||
202 | |||
203 | return Template::raw($this->getSrcsetFromArray($subset, $dpr)); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Return a string of image URLs and their sizes that are $width or smaller |
||
208 | * |
||
209 | * @param int $width |
||
210 | * @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
||
211 | * srcsets |
||
212 | * |
||
213 | * @return \Twig\Markup|null |
||
214 | */ |
||
215 | public function srcsetMaxWidth(int $width, bool $dpr = false): string |
||
216 | { |
||
217 | $subset = $this->getSrcsetSubsetArray($this->optimizedImageUrls, $width, 'maxwidth'); |
||
218 | |||
219 | return Template::raw($this->getSrcsetFromArray($subset, $dpr)); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Return the first webp image variant URL or the specific one passed in |
||
224 | * via $width |
||
225 | * |
||
226 | * @param int $width |
||
227 | * |
||
228 | * @return \Twig\Markup|null |
||
229 | */ |
||
230 | public function srcWebp(int $width = 0): string |
||
231 | { |
||
232 | if (empty($width)) { |
||
233 | return Template::raw(reset($this->optimizedWebPImageUrls)); |
||
234 | } |
||
235 | |||
236 | return Template::raw($this->optimizedWebPImageUrls[$width] ?? ''); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Getter for CraftQL |
||
241 | * |
||
242 | * @param int $width |
||
243 | * |
||
244 | * @return string |
||
245 | */ |
||
246 | public function getSrcWebp(int $width = 0): string |
||
247 | { |
||
248 | return $this->srcWebp($width); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Return a string of webp image URLs and their sizes |
||
253 | * |
||
254 | * @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
||
255 | * srcsets |
||
256 | * |
||
257 | * @return \Twig\Markup|null |
||
258 | */ |
||
259 | public function srcsetWebp(bool $dpr = false): string |
||
260 | { |
||
261 | return Template::raw($this->getSrcsetFromArray($this->optimizedWebPImageUrls, $dpr)); |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Getter for CraftQL |
||
266 | * |
||
267 | * @param bool $dpr |
||
268 | * |
||
269 | * @return string |
||
270 | */ |
||
271 | public function getSrcsetWebp(bool $dpr = false): string |
||
272 | { |
||
273 | return $this->srcsetWebp($dpr); |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Return a string of webp image URLs and their sizes that match $width |
||
278 | * |
||
279 | * @param int $width |
||
280 | * @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
||
281 | * srcsets |
||
282 | * |
||
283 | * @return \Twig\Markup|null |
||
284 | */ |
||
285 | public function srcsetWidthWebp(int $width, bool $dpr = false): string |
||
286 | { |
||
287 | $subset = $this->getSrcsetSubsetArray($this->optimizedWebPImageUrls, $width, 'width'); |
||
288 | |||
289 | return Template::raw($this->getSrcsetFromArray($subset, $dpr)); |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Return a string of webp image URLs and their sizes that are at least |
||
294 | * $width or larger |
||
295 | * |
||
296 | * @param int $width |
||
297 | * @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
||
298 | * srcsets |
||
299 | * |
||
300 | * @return \Twig\Markup|null |
||
301 | */ |
||
302 | public function srcsetMinWidthWebp(int $width, bool $dpr = false): string |
||
303 | { |
||
304 | $subset = $this->getSrcsetSubsetArray($this->optimizedWebPImageUrls, $width, 'minwidth'); |
||
305 | |||
306 | return Template::raw($this->getSrcsetFromArray($subset, $dpr)); |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Return a string of webp image URLs and their sizes that are $width or |
||
311 | * smaller |
||
312 | * |
||
313 | * @param int $width |
||
314 | * @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
||
315 | * srcsets |
||
316 | * |
||
317 | * @return \Twig\Markup|null |
||
318 | */ |
||
319 | public function srcsetMaxWidthWebp(int $width, bool $dpr = false): string |
||
320 | { |
||
321 | $subset = $this->getSrcsetSubsetArray($this->optimizedWebPImageUrls, $width, 'maxwidth'); |
||
322 | |||
323 | return Template::raw($this->getSrcsetFromArray($subset, $dpr)); |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Work around issues with `<img srcset>` returning sizes larger than are |
||
328 | * available as per: |
||
329 | * https://medium.com/@MRWwebDesign/responsive-images-the-sizes-attribute-and-unexpected-image-sizes-882a2eadb6db |
||
330 | * |
||
331 | * @return int |
||
332 | */ |
||
333 | public function maxSrcsetWidth(): int |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Getter for CraftQL |
||
349 | * |
||
350 | * @return int |
||
351 | */ |
||
352 | public function getMaxSrcsetWidth(): int |
||
353 | { |
||
354 | return $this->maxSrcsetWidth(); |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * Return the colors as an array of RGB colors |
||
359 | */ |
||
360 | public function colorPaletteRgb(): array |
||
361 | { |
||
362 | $colors = []; |
||
363 | |||
364 | foreach ($this->colorPalette as $color) { |
||
365 | if (!empty($color)) { |
||
366 | $colors[] = ColorHelper::HTMLToRGB($color); |
||
367 | } |
||
368 | } |
||
369 | |||
370 | return $colors; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Return a base64-encoded placeholder image |
||
375 | * |
||
376 | * @return \Twig\Markup|null |
||
377 | */ |
||
378 | public function placeholderImage() |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Getter for CraftQL |
||
393 | * |
||
394 | * @return string |
||
395 | */ |
||
396 | public function getPlaceholderImage(): string |
||
397 | { |
||
398 | return (string)$this->placeholderImage(); |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * @return string |
||
403 | */ |
||
404 | public function placeholderImageSize(): string |
||
405 | { |
||
406 | $placeholder = $this->placeholderImage(); |
||
407 | $contentLength = !empty(\strlen($placeholder)) ? \strlen($placeholder) : 0; |
||
408 | |||
409 | return ImageOptimize::$plugin->optimize->humanFileSize($contentLength, 1); |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * Return an SVG box as a placeholder image |
||
414 | * |
||
415 | * @param string|null $color |
||
416 | * |
||
417 | * @return \Twig\Markup|null |
||
418 | */ |
||
419 | public function placeholderBox(string $color = null) |
||
420 | { |
||
421 | $width = $this->placeholderWidth ?? 1; |
||
422 | $height = $this->placeholderHeight ?? 1; |
||
423 | $color = $color ?? $this->colorPalette[0] ?? '#CCC'; |
||
424 | |||
425 | return Template::raw(ImageOptimize::$plugin->placeholder->generatePlaceholderBox($width, $height, $color)); |
||
426 | } |
||
427 | |||
428 | /** |
||
429 | * @param string|null $color |
||
430 | * |
||
431 | * @return string |
||
432 | */ |
||
433 | public function getPlaceholderBox(string $color = null): string |
||
434 | { |
||
435 | return (string)$this->placeholderBox($color); |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * Getter for CraftQL |
||
440 | * |
||
441 | * @return string |
||
442 | */ |
||
443 | public function placeholderBoxSize(): string |
||
444 | { |
||
445 | $placeholder = $this->placeholderBox(); |
||
446 | $contentLength = !empty(\strlen($placeholder)) ? \strlen($placeholder) : 0; |
||
447 | |||
448 | return ImageOptimize::$plugin->optimize->humanFileSize($contentLength, 1); |
||
449 | } |
||
450 | |||
451 | /** |
||
452 | * Return a silhouette of the image as an SVG placeholder |
||
453 | * |
||
454 | * @return \Twig\Markup|null |
||
455 | */ |
||
456 | public function placeholderSilhouette() |
||
457 | { |
||
458 | $header = 'data:image/svg+xml,'; |
||
459 | if (!empty($this->placeholderSvg)) { |
||
460 | $content = $this->placeholderSvg; |
||
461 | } else { |
||
462 | // At least return something |
||
463 | return $this->defaultPlaceholderImage(); |
||
464 | } |
||
465 | |||
466 | return Template::raw($header.$content); |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * Getter for CraftQL |
||
471 | * |
||
472 | * @return string |
||
473 | */ |
||
474 | public function getPlaceholderSilhouette(): string |
||
475 | { |
||
476 | return (string)$this->placeholderSilhouette(); |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * @return string |
||
481 | */ |
||
482 | public function placeholderSilhouetteSize(): string |
||
483 | { |
||
484 | $placeholder = $this->placeholderSilhouette(); |
||
485 | $contentLength = !empty(\strlen($placeholder)) ? \strlen($placeholder) : 0; |
||
486 | |||
487 | return ImageOptimize::$plugin->optimize->humanFileSize($contentLength, 1); |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * Get the file size of any remote resource (using curl), |
||
492 | * either in bytes or - default - as human-readable formatted string. |
||
493 | * |
||
494 | * @author Stephan Schmitz <[email protected]> |
||
495 | * @license MIT <http://eyecatchup.mit-license.org/> |
||
496 | * @url <https://gist.github.com/eyecatchup/f26300ffd7e50a92bc4d> |
||
497 | * |
||
498 | * @param string $url Takes the remote object's URL. |
||
499 | * @param boolean $formatSize Whether to return size in bytes or |
||
500 | * formatted. |
||
501 | * @param boolean $useHead Whether to use HEAD requests. If false, |
||
502 | * uses GET. |
||
503 | * |
||
504 | * @return int|mixed|string Returns human-readable formatted size |
||
505 | * or size in bytes (default: formatted). |
||
506 | */ |
||
507 | public function getRemoteFileSize($url, $formatSize = true, $useHead = true) |
||
508 | { |
||
509 | // Get an absolute URL with protocol that curl will be happy with |
||
510 | $url = UrlHelper::absoluteUrlWithProtocol($url); |
||
511 | $ch = curl_init($url); |
||
512 | curl_setopt_array($ch, [ |
||
513 | CURLOPT_RETURNTRANSFER => 1, |
||
514 | CURLOPT_FOLLOWLOCATION => 1, |
||
515 | CURLOPT_SSL_VERIFYPEER => 0, |
||
516 | ]); |
||
517 | if ($useHead) { |
||
518 | curl_setopt($ch, CURLOPT_NOBODY, 1); |
||
519 | } |
||
520 | curl_exec($ch); |
||
521 | // content-length of download (in bytes), read from Content-Length: field |
||
522 | $contentLength = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD); |
||
523 | curl_close($ch); |
||
524 | // cannot retrieve file size, return "-1" |
||
525 | if (!$contentLength) { |
||
526 | return -1; |
||
527 | } |
||
528 | // return size in bytes |
||
529 | if (!$formatSize) { |
||
530 | return $contentLength; |
||
531 | } |
||
532 | |||
533 | return ImageOptimize::$plugin->optimize->humanFileSize($contentLength, 1); |
||
534 | } |
||
535 | |||
536 | // Protected Methods |
||
537 | // ========================================================================= |
||
538 | |||
539 | protected function getSrcsetSubsetArray(array $set, int $width, string $comparison): array |
||
574 | } |
||
575 | |||
576 | /** |
||
577 | * @param array $array |
||
578 | * @param bool $dpr |
||
579 | * |
||
580 | * @return string |
||
581 | */ |
||
582 | protected function getSrcsetFromArray(array $array, bool $dpr = false): string |
||
583 | { |
||
584 | $srcset = ''; |
||
585 | foreach ($array as $key => $value) { |
||
602 | } |
||
603 | |||
604 | /** |
||
605 | * Return a default placeholder image |
||
606 | * |
||
607 | * @return \Twig\Markup |
||
608 | */ |
||
609 | protected function defaultPlaceholderImage(): \Twig\Markup |
||
616 | } |
||
617 | } |
||
618 |