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

FluidTYPO3 / vhs / 30001290505

23 Jul 2026 10:57AM UTC coverage: 70.309% (-1.7%) from 72.022%
30001290505

push

github

NamelessCoder
[TER] 8.0.0

4819 of 6854 relevant lines covered (70.31%)

6.54 hits per line

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

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

4
use FluidTYPO3\Vhs\Proxy\ImageResourceProxy;
5
use FluidTYPO3\Vhs\Utility\ContentObjectFetcher;
6
use FluidTYPO3\Vhs\Utility\ContextUtility;
7
use FluidTYPO3\Vhs\Utility\FrontendSimulationUtility;
8
use TYPO3\CMS\Core\Utility\GeneralUtility;
9
use TYPO3\CMS\Core\Utility\MathUtility;
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();
9✔
31

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

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

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

49
        foreach ($srcsets as $width) {
9✔
50
            $srcsetVariant = $this->getImgResource($src, $width, $format, $quality, $treatIdAsReference, null, $crop);
9✔
51
            if (!$srcsetVariant->isValid()) {
9✔
52
                continue;
×
53
            }
54

55
            $imageUrl = $srcsetVariant->getPublicUrl();
9✔
56
            if (!$imageUrl) {
9✔
57
                continue;
×
58
            }
59

60
            $srcsetVariantSrc = rawurldecode($imageUrl);
9✔
61
            $srcsetVariantSrc = static::preprocessSourceUri(
9✔
62
                str_replace('%2F', '/', rawurlencode($srcsetVariantSrc)),
9✔
63
                $this->arguments
9✔
64
            );
9✔
65

66
            $width = $srcsetVariant->getWidth();
9✔
67

68
            $imageSources[$width] = [
9✔
69
                'src' => $srcsetVariantSrc,
9✔
70
                'width' => $width,
9✔
71
                'height' => $srcsetVariant->getHeight(),
9✔
72
            ];
9✔
73
            $srcsetVariants[$width] = $srcsetVariantSrc . ' ' . $width . 'w';
9✔
74
        }
75

76
        $tag->addAttribute('srcset', implode(',', $srcsetVariants));
9✔
77

78
        FrontendSimulationUtility::resetFrontendEnvironment($tsfeBackup);
9✔
79

80
        return $imageSources;
9✔
81
    }
82

83
    /**
84
     * Generates a copy of a give image with a specific width
85
     *
86
     * @param string $src path of the image to convert
87
     * @param integer $width width to convert the image to
88
     * @param string $format format of the resulting copy
89
     * @param integer $quality quality of the resulting copy
90
     * @param bool $treatIdAsReference given src argument is a sys_file_reference record
91
     * @param string|null $params additional params for the image rendering
92
     * @param string|null $crop image editor cropping configuration
93
     */
94
    public function getImgResource(
95
        string $src,
96
        int $width,
97
        ?string $format,
98
        int $quality,
99
        bool $treatIdAsReference,
100
        ?string $params = null,
101
        ?string $crop = null
102
    ): ImageResourceProxy {
103
        $contentObject = ContentObjectFetcher::resolve($this->configurationManager);
×
104

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

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

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

© 2026 Coveralls, Inc