• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FluidTYPO3 / vhs / 13566190336

27 Feb 2025 12:18PM UTC coverage: 72.127% (-0.6%) from 72.746%
13566190336

push

github

NamelessCoder
[TER] 7.1.0

5649 of 7832 relevant lines covered (72.13%)

20.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.45
/Classes/Traits/SourceSetViewHelperTrait.php
1
<?php
2
namespace FluidTYPO3\Vhs\Traits;
3

4
use FluidTYPO3\Vhs\Utility\ContentObjectFetcher;
5
use FluidTYPO3\Vhs\Utility\ContextUtility;
6
use FluidTYPO3\Vhs\Utility\FrontendSimulationUtility;
7
use TYPO3\CMS\Core\Utility\GeneralUtility;
8
use TYPO3\CMS\Core\Utility\MathUtility;
9
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
10
use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;
11

12
/*
13
 * This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
14
 *
15
 * For the full copyright and license information, please read the
16
 * LICENSE.md file that was distributed with this source code.
17
 */
18

19
/**
20
 * This trait can be used by viewhelpers that generate image tags
21
 * to add srcsets based to the imagetag for better responsiveness
22
 */
23
trait SourceSetViewHelperTrait
24
{
25
    /**
26
     * Used to attach srcset variants of a given image to the specified tag.
27
     */
28
    public function addSourceSet(TagBuilder $tag, string $src): array
29
    {
30
        $srcsets = $this->getSourceSetWidths();
21✔
31

32
        $tsfeBackup = FrontendSimulationUtility::simulateFrontendEnvironment();
21✔
33

34
        /** @var string|null $format */
35
        $format = $this->arguments['format'];
21✔
36
        /** @var int $quality */
37
        $quality = $this->arguments['quality'];
21✔
38
        /** @var string|null $crop */
39
        $crop = $this->arguments['crop'];
21✔
40
        $treatIdAsReference = (boolean) $this->arguments['treatIdAsReference'];
21✔
41
        if ($treatIdAsReference) {
21✔
42
            /** @var string $src */
43
            $src = $this->arguments['src'];
×
44
        }
45

46
        $imageSources = [];
21✔
47
        $srcsetVariants = [];
21✔
48

49
        foreach ($srcsets as $width) {
21✔
50
            $srcsetVariant = $this->getImgResource($src, $width, $format, $quality, $treatIdAsReference, null, $crop);
21✔
51

52
            if ($srcsetVariant['processedFile'] ?? false) {
21✔
53
                $imageUrl = $srcsetVariant['processedFile']->getPublicUrl();
×
54
            } else {
55
                $imageUrl = $srcsetVariant[3] ?? '';
21✔
56
            }
57
            $srcsetVariantSrc = rawurldecode($imageUrl);
21✔
58
            $srcsetVariantSrc = static::preprocessSourceUri(
21✔
59
                str_replace('%2F', '/', rawurlencode($srcsetVariantSrc)),
21✔
60
                $this->arguments
21✔
61
            );
21✔
62

63
            $imageSources[$srcsetVariant[0]] = [
21✔
64
                'src' => $srcsetVariantSrc,
21✔
65
                'width' => $srcsetVariant[0],
21✔
66
                'height' => $srcsetVariant[1],
21✔
67
            ];
21✔
68
            $srcsetVariants[$srcsetVariant[0]] = $srcsetVariantSrc . ' ' . $srcsetVariant[0] . 'w';
21✔
69
        }
70

71
        $tag->addAttribute('srcset', implode(',', $srcsetVariants));
21✔
72

73
        FrontendSimulationUtility::resetFrontendEnvironment($tsfeBackup);
21✔
74

75
        return $imageSources;
21✔
76
    }
77

78
    /**
79
     * Generates a copy of a give image with a specific width
80
     *
81
     * @param string $src path of the image to convert
82
     * @param integer $width width to convert the image to
83
     * @param string $format format of the resulting copy
84
     * @param integer $quality quality of the resulting copy
85
     * @param bool $treatIdAsReference given src argument is a sys_file_reference record
86
     * @param string|null $params additional params for the image rendering
87
     * @param string|null $crop image editor cropping configuration
88
     * @return array
89
     */
90
    public function getImgResource(
91
        string $src,
92
        int $width,
93
        ?string $format,
94
        int $quality,
95
        bool $treatIdAsReference,
96
        ?string $params = null,
97
        ?string $crop = null
98
    ): array {
99
        $contentObject = ContentObjectFetcher::resolve($this->configurationManager);
21✔
100
        if ($contentObject === null) {
21✔
101
            throw new Exception(static::class . ' requires a ContentObjectRenderer, none found', 1737808465);
×
102
        }
103

104
        $setup = [
21✔
105
            'width' => $width,
21✔
106
            'treatIdAsReference' => $treatIdAsReference,
21✔
107
            'crop' => $crop,
21✔
108
            'params' => $params,
21✔
109
        ];
21✔
110
        if (!empty($format)) {
21✔
111
            $setup['ext'] = $format;
21✔
112
        }
113
        if (0 < $quality) {
21✔
114
            $quality = MathUtility::forceIntegerInRange($quality, 10, 100, 75);
21✔
115
            $setup['params'] .= ' -quality ' . $quality;
21✔
116
        }
117

118
        if (ContextUtility::isBackend() && '../' === substr($src, 0, 3)) {
21✔
119
            $src = substr($src, 3);
×
120
        }
121
        return (array) $contentObject->getImgResource($src, $setup);
21✔
122
    }
123

124
    /**
125
     * Returns an array of srcsets based on the mixed ViewHelper
126
     * input (list, csv, array, iterator).
127
     */
128
    public function getSourceSetWidths(): array
129
    {
130
        $srcsets = $this->arguments['srcset'];
21✔
131
        if ($srcsets instanceof \Traversable) {
21✔
132
            $srcsets = iterator_to_array($srcsets);
7✔
133
        } elseif (is_string($srcsets)) {
14✔
134
            $srcsets = GeneralUtility::trimExplode(',', $srcsets, true);
7✔
135
        } else {
136
            $srcsets = (array) $srcsets;
7✔
137
        }
138
        return $srcsets;
21✔
139
    }
140
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc