| Total Complexity | 74 |
| Total Lines | 709 |
| Duplicated Lines | 0 % |
| Changes | 14 | ||
| Bugs | 2 | 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 |
||
| 27 | class OptimizedImage extends Model |
||
| 28 | { |
||
| 29 | // Public Properties |
||
| 30 | // ========================================================================= |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var ?array An array of optimized image variant URLs |
||
| 34 | */ |
||
| 35 | public ?array $optimizedImageUrls = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var ?array An array of optimized .webp image variant URLs |
||
| 39 | */ |
||
| 40 | public ?array $optimizedWebPImageUrls = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var ?array An array of the widths of the optimized image variants |
||
| 44 | */ |
||
| 45 | public ?array $variantSourceWidths = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var ?array An array of the heights of the optimized image variants |
||
| 49 | */ |
||
| 50 | public ?array $variantHeights = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array An array of the x,y image focal point coords, ranging from 0.0 to 1.0 |
||
| 54 | */ |
||
| 55 | public ?array $focalPoint = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var int The width of the original source image |
||
| 59 | */ |
||
| 60 | public ?int $originalImageWidth = 0; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var ?int The height of the original source image |
||
| 64 | */ |
||
| 65 | public ?int $originalImageHeight = 0; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var ?string The base64 encoded placeholder LQIP image |
||
| 69 | */ |
||
| 70 | public ?string $placeholder = ''; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var ?string The base64 encoded placeholder LQIP SVG image |
||
| 74 | */ |
||
| 75 | public ?string $placeholderSvg = ''; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var ?array An array the 5 most dominant colors in the image |
||
| 79 | */ |
||
| 80 | public ?array $colorPalette = []; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var ?int The overall lightness of the image, from 0..100 |
||
| 84 | */ |
||
| 85 | public ?int $lightness = 0; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var ?int The width of the placeholder image |
||
| 89 | */ |
||
| 90 | public ?int $placeholderWidth = 0; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var ?int The height of the placeholder image |
||
| 94 | */ |
||
| 95 | public ?int $placeholderHeight = 0; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var ?array An array of errors logged when generating the image transforms |
||
| 99 | */ |
||
| 100 | public ?array $stickyErrors = []; |
||
| 101 | |||
| 102 | // Public Methods |
||
| 103 | // ========================================================================= |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @inheritdoc |
||
| 107 | */ |
||
| 108 | public function rules(): array |
||
| 109 | { |
||
| 110 | return [ |
||
| 111 | ['optimizedImageUrls', ArrayValidator::class], |
||
| 112 | ['optimizedWebPImageUrls', ArrayValidator::class], |
||
| 113 | ['variantSourceWidths', ArrayValidator::class], |
||
| 114 | ['variantHeights', ArrayValidator::class], |
||
| 115 | ['focalPoint', 'safe'], |
||
| 116 | ['originalImageWidth', 'integer'], |
||
| 117 | ['originalImageHeight', 'integer'], |
||
| 118 | ['placeholder', 'string'], |
||
| 119 | ['placeholderSvg', 'string'], |
||
| 120 | ['colorPalette', ArrayValidator::class], |
||
| 121 | ['placeholderWidth', 'integer'], |
||
| 122 | ['placeholderHeight', 'integer'], |
||
| 123 | ['stickyErrors', ArrayValidator::class], |
||
| 124 | ]; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Return the first image variant URL or the specific one passed in via |
||
| 129 | * $width |
||
| 130 | * |
||
| 131 | * @param int $width |
||
| 132 | * |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | public function src(int $width = 0): string |
||
| 136 | { |
||
| 137 | if (empty($width)) { |
||
| 138 | return Template::raw(reset($this->optimizedImageUrls)); |
||
| 139 | } |
||
| 140 | |||
| 141 | return Template::raw($this->optimizedImageUrls[$width] ?? ''); |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Getter for CraftQL |
||
| 146 | * |
||
| 147 | * @param int $width |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function getSrc(int $width = 0): string |
||
| 152 | { |
||
| 153 | return $this->src($width); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Return a string of image URLs and their sizes |
||
| 158 | * |
||
| 159 | * @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
||
| 160 | * srcsets |
||
| 161 | * |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | public function srcset(bool $dpr = false): string |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Getter for CraftQL |
||
| 171 | * |
||
| 172 | * @param bool $dpr |
||
| 173 | * |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | public function getSrcset(bool $dpr = false): string |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Return a string of image URLs and their sizes that match $width |
||
| 183 | * |
||
| 184 | * @param int $width |
||
| 185 | * @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
||
| 186 | * srcsets |
||
| 187 | * |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function srcsetWidth(int $width, bool $dpr = false): string |
||
| 191 | { |
||
| 192 | $subset = $this->getSrcsetSubsetArray($this->optimizedImageUrls, $width, 'width'); |
||
| 193 | |||
| 194 | return Template::raw($this->getSrcsetFromArray($subset, $dpr)); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Return a string of image URLs and their sizes that are at least $width |
||
| 199 | * or larger |
||
| 200 | * |
||
| 201 | * @param int $width |
||
| 202 | * @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
||
| 203 | * srcsets |
||
| 204 | * |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public function srcsetMinWidth(int $width, bool $dpr = false): string |
||
| 208 | { |
||
| 209 | $subset = $this->getSrcsetSubsetArray($this->optimizedImageUrls, $width, 'minwidth'); |
||
| 210 | |||
| 211 | return Template::raw($this->getSrcsetFromArray($subset, $dpr)); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Return a string of image URLs and their sizes that are $width or smaller |
||
| 216 | * |
||
| 217 | * @param int $width |
||
| 218 | * @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
||
| 219 | * srcsets |
||
| 220 | * |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | public function srcsetMaxWidth(int $width, bool $dpr = false): string |
||
| 224 | { |
||
| 225 | $subset = $this->getSrcsetSubsetArray($this->optimizedImageUrls, $width, 'maxwidth'); |
||
| 226 | |||
| 227 | return Template::raw($this->getSrcsetFromArray($subset, $dpr)); |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Return the first webp image variant URL or the specific one passed in |
||
| 232 | * via $width |
||
| 233 | * |
||
| 234 | * @param int $width |
||
| 235 | * |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | public function srcWebp(int $width = 0): string |
||
| 239 | { |
||
| 240 | if (empty($width)) { |
||
| 241 | return Template::raw(reset($this->optimizedWebPImageUrls)); |
||
| 242 | } |
||
| 243 | |||
| 244 | return Template::raw($this->optimizedWebPImageUrls[$width] ?? ''); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Getter for CraftQL |
||
| 249 | * |
||
| 250 | * @param int $width |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | public function getSrcWebp(int $width = 0): string |
||
| 255 | { |
||
| 256 | return $this->srcWebp($width); |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Return a string of webp image URLs and their sizes |
||
| 261 | * |
||
| 262 | * @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
||
| 263 | * srcsets |
||
| 264 | * |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | public function srcsetWebp(bool $dpr = false): string |
||
| 268 | { |
||
| 269 | return Template::raw($this->getSrcsetFromArray($this->optimizedWebPImageUrls, $dpr)); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Getter for CraftQL |
||
| 274 | * |
||
| 275 | * @param bool $dpr |
||
| 276 | * |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | public function getSrcsetWebp(bool $dpr = false): string |
||
| 280 | { |
||
| 281 | return $this->srcsetWebp($dpr); |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Return a string of webp image URLs and their sizes that match $width |
||
| 286 | * |
||
| 287 | * @param int $width |
||
| 288 | * @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
||
| 289 | * srcsets |
||
| 290 | * |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | public function srcsetWidthWebp(int $width, bool $dpr = false): string |
||
| 294 | { |
||
| 295 | $subset = $this->getSrcsetSubsetArray($this->optimizedWebPImageUrls, $width, 'width'); |
||
| 296 | |||
| 297 | return Template::raw($this->getSrcsetFromArray($subset, $dpr)); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Return a string of webp image URLs and their sizes that are at least |
||
| 302 | * $width or larger |
||
| 303 | * |
||
| 304 | * @param int $width |
||
| 305 | * @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
||
| 306 | * srcsets |
||
| 307 | * |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | public function srcsetMinWidthWebp(int $width, bool $dpr = false): string |
||
| 311 | { |
||
| 312 | $subset = $this->getSrcsetSubsetArray($this->optimizedWebPImageUrls, $width, 'minwidth'); |
||
| 313 | |||
| 314 | return Template::raw($this->getSrcsetFromArray($subset, $dpr)); |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Return a string of webp image URLs and their sizes that are $width or |
||
| 319 | * smaller |
||
| 320 | * |
||
| 321 | * @param int $width |
||
| 322 | * @param bool $dpr Whether to generate 1x, 2x srcsets vs the normal XXXw |
||
| 323 | * srcsets |
||
| 324 | * |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public function srcsetMaxWidthWebp(int $width, bool $dpr = false): string |
||
| 328 | { |
||
| 329 | $subset = $this->getSrcsetSubsetArray($this->optimizedWebPImageUrls, $width, 'maxwidth'); |
||
| 330 | |||
| 331 | return Template::raw($this->getSrcsetFromArray($subset, $dpr)); |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Work around issues with `<img srcset>` returning sizes larger than are |
||
| 336 | * available as per: |
||
| 337 | * https://medium.com/@MRWwebDesign/responsive-images-the-sizes-attribute-and-unexpected-image-sizes-882a2eadb6db |
||
| 338 | * |
||
| 339 | * @return int |
||
| 340 | */ |
||
| 341 | public function maxSrcsetWidth(): int |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Getter for CraftQL |
||
| 357 | * |
||
| 358 | * @return int |
||
| 359 | */ |
||
| 360 | public function getMaxSrcsetWidth(): int |
||
| 361 | { |
||
| 362 | return $this->maxSrcsetWidth(); |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Return the colors as an array of RGB colors |
||
| 367 | */ |
||
| 368 | public function colorPaletteRgb(): array |
||
| 369 | { |
||
| 370 | $colors = []; |
||
| 371 | |||
| 372 | foreach ($this->colorPalette as $color) { |
||
| 373 | if (!empty($color)) { |
||
| 374 | $colors[] = ColorHelper::HTMLToRGB($color); |
||
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | return $colors; |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Generate a complete <link rel="preload"> tag for this OptimizedImages model |
||
| 383 | * ref: https://web.dev/preload-responsive-images/#imagesrcset-and-imagesizes |
||
| 384 | * |
||
| 385 | * @return LinkPreloadTag |
||
| 386 | */ |
||
| 387 | public function linkPreloadTag(): LinkPreloadTag |
||
| 388 | { |
||
| 389 | return new LinkPreloadTag(['optimizedImage' => $this]); |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Generate a complete <img> tag for this OptimizedImage model |
||
| 394 | * |
||
| 395 | * @return ImgTag |
||
| 396 | */ |
||
| 397 | public function imgTag(): ImgTag |
||
| 398 | { |
||
| 399 | return new ImgTag(['optimizedImage' => $this]); |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Generate a complete <picture> tag for this OptimizedImage model |
||
| 404 | * |
||
| 405 | * @return PictureTag |
||
| 406 | */ |
||
| 407 | public function pictureTag(): PictureTag |
||
| 408 | { |
||
| 409 | return new PictureTag(['optimizedImage' => $this]); |
||
| 410 | } |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Return a base64-encoded placeholder image |
||
| 414 | * |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | public function placeholderImage(): string |
||
| 418 | { |
||
| 419 | $header = 'data:image/jpeg;base64,'; |
||
| 420 | if (!empty($this->placeholder)) { |
||
| 421 | $content = $this->placeholder; |
||
| 422 | } else { |
||
| 423 | // At least return something |
||
| 424 | return $this->defaultPlaceholderImage(); |
||
| 425 | } |
||
| 426 | |||
| 427 | return Template::raw($header . rawurlencode($content)); |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Getter for CraftQL |
||
| 432 | * |
||
| 433 | * @return string |
||
| 434 | */ |
||
| 435 | public function getPlaceholderImage(): string |
||
| 436 | { |
||
| 437 | return $this->placeholderImage(); |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @return string |
||
| 442 | */ |
||
| 443 | public function placeholderImageSize(): string |
||
| 444 | { |
||
| 445 | $placeholder = $this->placeholderImage(); |
||
| 446 | $contentLength = !empty(strlen($placeholder)) ? strlen($placeholder) : 0; |
||
| 447 | |||
| 448 | return ImageOptimize::$plugin->optimize->humanFileSize($contentLength, 1); |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Return an SVG box as a placeholder image |
||
| 453 | * |
||
| 454 | * @param string|null $color |
||
| 455 | * |
||
| 456 | * @return string |
||
| 457 | */ |
||
| 458 | public function placeholderBox(?string $color = null): string |
||
| 459 | { |
||
| 460 | $width = $this->placeholderWidth ?? 1; |
||
| 461 | $height = $this->placeholderHeight ?? 1; |
||
| 462 | $color = $color ?? $this->colorPalette[0] ?? '#CCC'; |
||
| 463 | |||
| 464 | return Template::raw(ImageOptimize::$plugin->placeholder->generatePlaceholderBox($width, $height, $color)); |
||
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @param string|null $color |
||
| 469 | * |
||
| 470 | * @return string |
||
| 471 | */ |
||
| 472 | public function getPlaceholderBox(string $color = null): string |
||
| 473 | { |
||
| 474 | return $this->placeholderBox($color); |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Getter for CraftQL |
||
| 479 | * |
||
| 480 | * @return string |
||
| 481 | */ |
||
| 482 | public function placeholderBoxSize(): string |
||
| 483 | { |
||
| 484 | $placeholder = $this->placeholderBox(); |
||
| 485 | $contentLength = !empty(strlen($placeholder)) ? strlen($placeholder) : 0; |
||
| 486 | |||
| 487 | return ImageOptimize::$plugin->optimize->humanFileSize($contentLength, 1); |
||
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Return a silhouette of the image as an SVG placeholder |
||
| 492 | * |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | public function placeholderSilhouette(): string |
||
| 496 | { |
||
| 497 | $header = 'data:image/svg+xml,'; |
||
| 498 | if (!empty($this->placeholderSvg)) { |
||
| 499 | $content = $this->placeholderSvg; |
||
| 500 | } else { |
||
| 501 | // At least return something |
||
| 502 | return $this->defaultPlaceholderImage(); |
||
| 503 | } |
||
| 504 | |||
| 505 | return Template::raw($header . $content); |
||
| 506 | } |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Getter for CraftQL |
||
| 510 | * |
||
| 511 | * @return string |
||
| 512 | */ |
||
| 513 | public function getPlaceholderSilhouette(): string |
||
| 514 | { |
||
| 515 | return $this->placeholderSilhouette(); |
||
| 516 | } |
||
| 517 | |||
| 518 | /** |
||
| 519 | * @return string |
||
| 520 | */ |
||
| 521 | public function placeholderSilhouetteSize(): string |
||
| 522 | { |
||
| 523 | $placeholder = $this->placeholderSilhouette(); |
||
| 524 | $contentLength = !empty(strlen($placeholder)) ? strlen($placeholder) : 0; |
||
| 525 | |||
| 526 | return ImageOptimize::$plugin->optimize->humanFileSize($contentLength, 1); |
||
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Get the file size of any remote resource (using curl), |
||
| 531 | * either in bytes or - default - as human-readable formatted string. |
||
| 532 | * |
||
| 533 | * @param string $url Takes the remote object's URL. |
||
| 534 | * @param bool $formatSize Whether to return size in bytes or |
||
| 535 | * formatted. |
||
| 536 | * @param bool $useHead Whether to use HEAD requests. If false, |
||
| 537 | * uses GET. |
||
| 538 | * |
||
| 539 | * @return mixed Returns human-readable formatted size |
||
| 540 | * or size in bytes (default: formatted). |
||
| 541 | * @noinspection PhpComposerExtensionStubsInspection*@author Stephan Schmitz <[email protected]> |
||
| 542 | * @license MIT <http://eyecatchup.mit-license.org/> |
||
| 543 | * @url <https://gist.github.com/eyecatchup/f26300ffd7e50a92bc4d> |
||
| 544 | * |
||
| 545 | * @noinspection PhpComposerExtensionStubsInspection |
||
| 546 | */ |
||
| 547 | public function getRemoteFileSize(string $url, bool $formatSize = true, bool $useHead = true): mixed |
||
| 548 | { |
||
| 549 | // Get an absolute URL with protocol that curl will be happy with |
||
| 550 | $url = UrlHelper::absoluteUrlWithProtocol($url); |
||
| 551 | $ch = curl_init($url); |
||
| 552 | /** @noinspection CurlSslServerSpoofingInspection */ |
||
| 553 | curl_setopt_array($ch, [ |
||
| 554 | CURLOPT_RETURNTRANSFER => 1, |
||
| 555 | CURLOPT_FOLLOWLOCATION => 1, |
||
| 556 | CURLOPT_SSL_VERIFYPEER => 0, |
||
| 557 | ]); |
||
| 558 | if ($useHead) { |
||
| 559 | curl_setopt($ch, CURLOPT_NOBODY, 1); |
||
| 560 | } |
||
| 561 | curl_exec($ch); |
||
| 562 | // content-length of download (in bytes), read from Content-Length: field |
||
| 563 | $contentLength = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD); |
||
| 564 | $error = curl_error($ch); |
||
| 565 | if (!empty($error)) { |
||
| 566 | Craft::error($error, __METHOD__); |
||
| 567 | } |
||
| 568 | curl_close($ch); |
||
| 569 | // cannot retrieve file size, return "-1" |
||
| 570 | if (!$contentLength) { |
||
| 571 | return -1; |
||
| 572 | } |
||
| 573 | // return size in bytes |
||
| 574 | if (!$formatSize) { |
||
| 575 | return $contentLength; |
||
| 576 | } |
||
| 577 | |||
| 578 | return ImageOptimize::$plugin->optimize->humanFileSize($contentLength, 1); |
||
| 579 | } |
||
| 580 | |||
| 581 | /** |
||
| 582 | * @param array $array |
||
| 583 | * @param bool $dpr |
||
| 584 | * |
||
| 585 | * @return string |
||
| 586 | */ |
||
| 587 | public function getSrcsetFromArray(array $array, bool $dpr = false): string |
||
| 588 | { |
||
| 589 | $srcset = ''; |
||
| 590 | foreach ($array as $key => $value) { |
||
| 591 | if ($dpr) { |
||
| 592 | $descriptor = '1x'; |
||
| 593 | if (!empty($array[(int)$key / 2])) { |
||
| 594 | $descriptor = '2x'; |
||
| 595 | } |
||
| 596 | if (!empty($array[(int)$key / 3])) { |
||
| 597 | $descriptor = '3x'; |
||
| 598 | } |
||
| 599 | } else { |
||
| 600 | $descriptor = $key . 'w'; |
||
| 601 | } |
||
| 602 | $srcset .= $value . ' ' . $descriptor . ', '; |
||
| 603 | } |
||
| 604 | |||
| 605 | return rtrim($srcset, ', '); |
||
| 606 | } |
||
| 607 | |||
| 608 | // Protected Methods |
||
| 609 | // ========================================================================= |
||
| 610 | |||
| 611 | protected function getSrcsetSubsetArray(array $set, int $width, string $comparison): array |
||
| 612 | { |
||
| 613 | $subset = []; |
||
| 614 | $index = 0; |
||
| 615 | if (empty($this->variantSourceWidths)) { |
||
| 616 | return $subset; |
||
| 617 | } |
||
| 618 | foreach ($this->variantSourceWidths as $variantSourceWidth) { |
||
| 619 | $match = false; |
||
| 620 | switch ($comparison) { |
||
| 621 | case 'width': |
||
| 622 | if ($variantSourceWidth == $width) { |
||
| 623 | $match = true; |
||
| 624 | } |
||
| 625 | break; |
||
| 626 | |||
| 627 | case 'minwidth': |
||
| 628 | if ($variantSourceWidth >= $width) { |
||
| 629 | $match = true; |
||
| 630 | } |
||
| 631 | break; |
||
| 632 | |||
| 633 | case 'maxwidth': |
||
| 634 | if ($variantSourceWidth <= $width) { |
||
| 635 | $match = true; |
||
| 636 | } |
||
| 637 | break; |
||
| 638 | } |
||
| 639 | if ($match) { |
||
| 640 | $subset += array_slice($set, $index, 1, true); |
||
| 641 | } |
||
| 642 | $index++; |
||
| 643 | } |
||
| 644 | |||
| 645 | return $subset; |
||
| 646 | } |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Return a default placeholder image |
||
| 650 | * |
||
| 651 | * @return string |
||
| 652 | */ |
||
| 653 | protected function defaultPlaceholderImage(): string |
||
| 654 | { |
||
| 655 | $width = 1; |
||
| 656 | $height = 1; |
||
| 657 | $color = '#CCC'; |
||
| 658 | |||
| 659 | return Template::raw(ImageOptimize::$plugin->placeholder->generatePlaceholderBox($width, $height, $color)); |
||
| 660 | } |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Swap the tag attributes to work with lazy loading |
||
| 664 | * ref: https://web.dev/native-lazy-loading/#how-do-i-handle-browsers-that-don't-yet-support-native-lazy-loading |
||
| 665 | * |
||
| 666 | * @param string $loading 'eager', 'lazy', 'lazySizes', 'lazySizesFallback' |
||
| 667 | * @param string $placeHolder 'box', 'color', 'image', 'silhouette' |
||
| 668 | * @param array $attrs |
||
| 669 | * |
||
| 670 | * @return array |
||
| 671 | */ |
||
| 672 | protected function swapLazyLoadAttrs(string $loading, string $placeHolder, array $attrs): array |
||
| 719 | } |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Return a lazy loading placeholder image based on the passed in $lazyload setting |
||
| 723 | * |
||
| 724 | * @param string $lazyLoad |
||
| 725 | * |
||
| 726 | * @return string |
||
| 727 | */ |
||
| 728 | protected function getLazyLoadSrc(string $lazyLoad): string |
||
| 736 | }; |
||
| 737 | } |
||
| 738 | } |
||
| 739 |