|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SpeedPuzzling\Web\Twig; |
| 6 | + |
| 7 | +use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
| 8 | +use Twig\Extension\AbstractExtension; |
| 9 | +use Twig\TwigFunction; |
| 10 | + |
| 11 | +final class LazyImageTwigExtension extends AbstractExtension |
| 12 | +{ |
| 13 | + private const string PLACEHOLDER_IMAGE = '/img/placeholder-puzzle.png'; |
| 14 | + |
| 15 | + public function __construct( |
| 16 | + readonly private CacheManager $cacheManager, |
| 17 | + ) { |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * @return array<TwigFunction> |
| 22 | + */ |
| 23 | + public function getFunctions(): array |
| 24 | + { |
| 25 | + return [ |
| 26 | + new TwigFunction('lazy_puzzle_image', $this->lazyPuzzleImage(...), ['is_safe' => ['html']]), |
| 27 | + ]; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Generates a lazy-loaded puzzle image with wrapper and proper attributes. |
| 32 | + * |
| 33 | + * @param string|null $path Image path in S3 |
| 34 | + * @param string $filter LiipImagine filter (puzzle_small, puzzle_medium) |
| 35 | + * @param string $alt Alt text for the image |
| 36 | + * @param int $position Position in list (1-based). First 4 positions are eager-loaded. |
| 37 | + * @param int $size Display size in pixels (60, 80, or 90) |
| 38 | + * @param string $class Additional CSS classes for the wrapper |
| 39 | + */ |
| 40 | + public function lazyPuzzleImage( |
| 41 | + null|string $path, |
| 42 | + string $filter, |
| 43 | + string $alt, |
| 44 | + int $position = 999, |
| 45 | + int $size = 80, |
| 46 | + string $class = '', |
| 47 | + ): string { |
| 48 | + $src = $this->getImageSrc($path, $filter); |
| 49 | + $sizeClass = $this->getSizeClass($size); |
| 50 | + |
| 51 | + // First 4 images are above-the-fold (eager loading) |
| 52 | + $isEager = $position <= 4; |
| 53 | + $loading = $isEager ? 'eager' : 'lazy'; |
| 54 | + |
| 55 | + // Build wrapper classes |
| 56 | + $wrapperClasses = trim(sprintf('lazy-img-wrapper %s %s', $sizeClass, $class)); |
| 57 | + |
| 58 | + // Build img classes and onload handler |
| 59 | + if ($isEager) { |
| 60 | + $imgClasses = 'lazy-img loaded'; |
| 61 | + $onload = ''; |
| 62 | + } else { |
| 63 | + $imgClasses = 'lazy-img'; |
| 64 | + $onload = ' onload="this.classList.add(\'loaded\')"'; |
| 65 | + } |
| 66 | + |
| 67 | + return sprintf( |
| 68 | + '<span class="%s"><img src="%s" alt="%s" loading="%s" class="%s"%s></span>', |
| 69 | + htmlspecialchars($wrapperClasses, ENT_QUOTES, 'UTF-8'), |
| 70 | + htmlspecialchars($src, ENT_QUOTES, 'UTF-8'), |
| 71 | + htmlspecialchars($alt, ENT_QUOTES, 'UTF-8'), |
| 72 | + $loading, |
| 73 | + $imgClasses, |
| 74 | + $onload, |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + private function getImageSrc(null|string $path, string $filter): string |
| 79 | + { |
| 80 | + if ($path === null) { |
| 81 | + return self::PLACEHOLDER_IMAGE; |
| 82 | + } |
| 83 | + |
| 84 | + return $this->cacheManager->getBrowserPath($path, $filter); |
| 85 | + } |
| 86 | + |
| 87 | + private function getSizeClass(int $size): string |
| 88 | + { |
| 89 | + return match (true) { |
| 90 | + $size <= 40 => 'lazy-img-40', |
| 91 | + $size <= 50 => 'lazy-img-50', |
| 92 | + $size <= 60 => 'lazy-img-60', |
| 93 | + $size <= 80 => 'lazy-img-80', |
| 94 | + $size <= 90 => 'lazy-img-90', |
| 95 | + default => 'lazy-img-100', |
| 96 | + }; |
| 97 | + } |
| 98 | +} |
0 commit comments