• 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

41.11
/Classes/ViewHelpers/Media/SourceViewHelper.php
1
<?php
2
namespace FluidTYPO3\Vhs\ViewHelpers\Media;
3

4
/*
5
 * This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.md file that was distributed with this source code.
9
 */
10

11
use FluidTYPO3\Vhs\Traits\TagViewHelperCompatibility;
12
use FluidTYPO3\Vhs\Utility\ContentObjectFetcher;
13
use FluidTYPO3\Vhs\Utility\ContextUtility;
14
use FluidTYPO3\Vhs\Utility\FrontendSimulationUtility;
15
use TYPO3\CMS\Core\Imaging\ImageResource;
16
use TYPO3\CMS\Core\Resource\FileReference;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
use TYPO3\CMS\Core\Utility\MathUtility;
19
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
20
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper;
21
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
22

23
/**
24
 * Used in conjuntion with the `v:media.PictureViewHelper`.
25
 * Please take a look at the `v:media.PictureViewHelper` documentation for more
26
 * information.
27
 */
28
class SourceViewHelper extends AbstractTagBasedViewHelper
29
{
30
    use TagViewHelperCompatibility;
31

32
    const SCOPE = 'FluidTYPO3\Vhs\ViewHelpers\Media\PictureViewHelper';
33
    const SCOPE_VARIABLE_SRC = 'src';
34
    const SCOPE_VARIABLE_ID = 'treatIdAsReference';
35
    const SCOPE_VARIABLE_DEFAULT_SOURCE = 'default-source';
36

37
    /**
38
     * name of the tag to be created by this view helper
39
     *
40
     * @var string
41
     * @api
42
     */
43
    protected $tagName = 'source';
44
    /**
45
     * @var ConfigurationManagerInterface
46
     */
47
    protected $configurationManager;
48

49
    public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager): void
50
    {
51
        $this->configurationManager = $configurationManager;
14✔
52
    }
53

54
    public function initializeArguments(): void
55
    {
56
        parent::initializeArguments();
7✔
57
        $this->registerUniversalTagAttributes();
7✔
58
        $this->registerArgument('media', 'string', 'Media query for which breakpoint this sources applies');
7✔
59
        $this->registerArgument(
7✔
60
            'width',
7✔
61
            'string',
7✔
62
            'Width of the image. This can be a numeric value representing the fixed width of the image in pixels. ' .
7✔
63
            'But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width ' .
7✔
64
            'for possible options.'
7✔
65
        );
7✔
66
        $this->registerArgument(
7✔
67
            'height',
7✔
68
            'string',
7✔
69
            'Height of the image. This can be a numeric value representing the fixed height of the image in pixels. ' .
7✔
70
            'But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width ' .
7✔
71
            'for possible options.'
7✔
72
        );
7✔
73
        $this->registerArgument('maxW', 'integer', 'Maximum Width of the image. (no upscaling)');
7✔
74
        $this->registerArgument('maxH', 'integer', 'Maximum Height of the image. (no upscaling)');
7✔
75
        $this->registerArgument('minW', 'integer', 'Minimum Width of the image.');
7✔
76
        $this->registerArgument('minH', 'integer', 'Minimum Height of the image.');
7✔
77
        $this->registerArgument(
7✔
78
            'format',
7✔
79
            'string',
7✔
80
            'Format of the processed file - also determines the target file format. If blank, TYPO3/IM/GM default ' .
7✔
81
            'is taken into account.'
7✔
82
        );
7✔
83
        $this->registerArgument(
7✔
84
            'quality',
7✔
85
            'integer',
7✔
86
            'Quality of the processed image. If blank/not present falls back to the default quality defined ' .
7✔
87
            'in install tool.',
7✔
88
            false,
7✔
89
            $GLOBALS['TYPO3_CONF_VARS']['GFX']['jpg_quality'] ?? 90
7✔
90
        );
7✔
91
        $this->registerArgument('relative', 'boolean', 'Produce a relative URL instead of absolute', false, false);
7✔
92
    }
93

94
    /**
95
     * Render method
96
     *
97
     * @return string
98
     */
99
    public function render()
100
    {
101
        $viewHelperVariableContainer = $this->renderingContext->getViewHelperVariableContainer();
×
102
        /** @var FileReference|string $imageSource */
103
        $imageSource = $viewHelperVariableContainer->get(static::SCOPE, static::SCOPE_VARIABLE_SRC);
×
104
        $treatIdAsRerefence = $viewHelperVariableContainer->get(static::SCOPE, static::SCOPE_VARIABLE_ID);
×
105

106
        $tsfeBackup = FrontendSimulationUtility::simulateFrontendEnvironment();
×
107

108
        $setup = [
×
109
            'width' => $this->arguments['width'],
×
110
            'height' => $this->arguments['height'],
×
111
            'minW' => $this->arguments['minW'],
×
112
            'minH' => $this->arguments['minH'],
×
113
            'maxW' => $this->arguments['maxW'],
×
114
            'maxH' => $this->arguments['maxH'],
×
115
            'treatIdAsReference' => $treatIdAsRerefence,
×
116
            'params' => '',
×
117
        ];
×
118
        /** @var int $quality */
119
        $quality = $this->arguments['quality'];
×
120
        /** @var string $format */
121
        $format = $this->arguments['format'];
×
122

123
        if (!empty($format)) {
×
124
            $setup['ext'] = $format;
×
125
        }
126
        if (0 < intval($quality)) {
×
127
            $quality = MathUtility::forceIntegerInRange($quality, 10, 100, 75);
×
128
            $setup['params'] .= ' -quality ' . $quality;
×
129
        }
130

131
        if (is_string($imageSource) && ContextUtility::isBackend() && '../' === mb_substr($imageSource, 0, 3)) {
×
132
            $imageSource = mb_substr($imageSource, 3);
×
133
        }
134
        $contentObject = ContentObjectFetcher::resolve($this->configurationManager);
×
135
        if ($contentObject === null) {
×
136
            throw new Exception('v:media.source requires a ContentObjectRenderer, none found', 1737807859);
×
137
        }
138

139
        $result = $contentObject->getImgResource($imageSource, $setup);
×
140
        if ($result instanceof ImageResource) {
×
141
            $processedFile = $result->getProcessedFile();
×
142
        } else {
143
            $processedFile = $result['processedFile'] ?? null;
×
144
        }
145

146
        FrontendSimulationUtility::resetFrontendEnvironment($tsfeBackup);
×
147

148
        if ($processedFile ?? false) {
×
149
            /** @var string $imageUrl */
150
            $imageUrl = $processedFile->getPublicUrl();
×
151
        } else {
152
            if ($result instanceof ImageResource) {
×
153
                $result = $result->getLegacyImageResourceInformation();
×
154
            }
155
            /** @var string $imageUrl */
156
            $imageUrl = $result[3] ?? '';
×
157
        }
158
        $src = $this->preprocessSourceUri(rawurldecode($imageUrl));
×
159

160
        /** @var string|null $media */
161
        $media = $this->arguments['media'];
×
162

163
        if (null === $media) {
×
164
            $viewHelperVariableContainer->addOrUpdate(static::SCOPE, static::SCOPE_VARIABLE_DEFAULT_SOURCE, $src);
×
165
        } else {
166
            $this->tag->addAttribute('media', $media);
×
167
        }
168

169
        $this->tag->addAttribute('srcset', $src);
×
170
        return $this->tag->render();
×
171
    }
172

173
    /**
174
     * Turns a relative source URI into an absolute URL
175
     * if required.
176
     */
177
    public function preprocessSourceUri(string $src): string
178
    {
179
        if (!empty($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_vhs.']['settings.']['prependPath'])) {
×
180
            $src = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_vhs.']['settings.']['prependPath'] . $src;
×
181
        } elseif (ContextUtility::isBackend() || !$this->arguments['relative']) {
×
182
            if (GeneralUtility::isValidUrl($src)) {
×
183
                $src = ltrim($src, '/');
×
184
            } elseif (ContextUtility::isFrontend()) {
×
185
                $src = $GLOBALS['TSFE']->absRefPrefix . ltrim($src, '/');
×
186
            } else {
187
                /** @var string $siteUrl */
188
                $siteUrl = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
×
189
                $src = $siteUrl . ltrim($src, '/');
×
190
            }
191
        }
192
        return $src;
×
193
    }
194
}
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