• 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

98.98
/Classes/ViewHelpers/Resource/AbstractImageViewHelper.php
1
<?php
2
namespace FluidTYPO3\Vhs\ViewHelpers\Resource;
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\Utility\ContentObjectFetcher;
12
use FluidTYPO3\Vhs\Utility\ContextUtility;
13
use FluidTYPO3\Vhs\Utility\FrontendSimulationUtility;
14
use FluidTYPO3\Vhs\Utility\ResourceUtility;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
17
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
18

19
/**
20
 * Base class for image related view helpers adapted from FLUID
21
 * original image viewhelper.
22
 */
23
abstract class AbstractImageViewHelper extends AbstractResourceViewHelper
24
{
25
    /**
26
     * @var ConfigurationManagerInterface
27
     */
28
    protected $configurationManager;
29

30
    public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager): void
31
    {
32
        $this->configurationManager = $configurationManager;
63✔
33
    }
34

35
    public function initializeArguments(): void
36
    {
37
        parent::initializeArguments();
7✔
38
        $this->registerArgument(
7✔
39
            'relative',
7✔
40
            'boolean',
7✔
41
            'If FALSE resource URIs are rendered absolute. URIs in backend mode are always absolute.',
7✔
42
            false,
7✔
43
            true
7✔
44
        );
7✔
45
        $this->registerArgument(
7✔
46
            'width',
7✔
47
            'string',
7✔
48
            'Width of the image. Numeric value in pixels or simple calculations. See imgResource.width ' .
7✔
49
            'for possible options.'
7✔
50
        );
7✔
51
        $this->registerArgument(
7✔
52
            'height',
7✔
53
            'string',
7✔
54
            'Height of the image. Numeric value in pixels or simple calculations. See imgResource.width for ' .
7✔
55
            'possible options.'
7✔
56
        );
7✔
57
        $this->registerArgument(
7✔
58
            'minWidth',
7✔
59
            'string',
7✔
60
            'Minimum width of the image. Numeric value in pixels or simple calculations. See imgResource.width ' .
7✔
61
            'for possible options.'
7✔
62
        );
7✔
63
        $this->registerArgument(
7✔
64
            'minHeight',
7✔
65
            'string',
7✔
66
            'Minimum height of the image. Numeric value in pixels or simple calculations. ' .
7✔
67
            'See imgResource.width for possible options.'
7✔
68
        );
7✔
69
        $this->registerArgument(
7✔
70
            'maxWidth',
7✔
71
            'string',
7✔
72
            'Maximum width of the image. Numeric value in pixels or simple calculations. ' .
7✔
73
            'See imgResource.width for possible options.'
7✔
74
        );
7✔
75
        $this->registerArgument(
7✔
76
            'maxHeight',
7✔
77
            'string',
7✔
78
            'Maximum height of the image. Numeric value in pixels or simple calculations. ' .
7✔
79
            'See imgResource.width for possible options.'
7✔
80
        );
7✔
81
        $this->registerArgument(
7✔
82
            'graceful',
7✔
83
            'bool',
7✔
84
            'Set to TRUE to ignore files that cannot be loaded. Default behavior is to throw an Exception.',
7✔
85
            false,
7✔
86
            false
7✔
87
        );
7✔
88
    }
89

90
    public function preprocessImages(array $files, bool $onlyProperties = false): ?array
91
    {
92
        if (empty($files)) {
35✔
93
            return null;
7✔
94
        }
95

96
        $tsfeBackup = FrontendSimulationUtility::simulateFrontendEnvironment();
28✔
97

98
        $contentObject = ContentObjectFetcher::resolve($this->configurationManager);
28✔
99
        if ($contentObject === null) {
28✔
100
            throw new Exception(static::class . ' requires a ContentObjectRenderer, none found', 1737807859);
×
101
        }
102

103
        $setup = [
28✔
104
            'width' => $this->arguments['width'] ?? null,
28✔
105
            'height' => $this->arguments['height'] ?? null,
28✔
106
            'minW' => $this->arguments['minWidth'] ?? null,
28✔
107
            'minH' => $this->arguments['minHeight'] ?? null,
28✔
108
            'maxW' => $this->arguments['maxWidth'] ?? null,
28✔
109
            'maxH' => $this->arguments['maxHeight'] ?? null,
28✔
110
            'treatIdAsReference' => false
28✔
111
        ];
28✔
112

113
        $images = [];
28✔
114

115
        foreach ($files as $file) {
28✔
116
            $imageInfo = $contentObject->getImgResource($file->getUid(), $setup);
28✔
117

118
            if (!is_array($imageInfo)) {
28✔
119
                if ($this->arguments['graceful'] ?? false) {
14✔
120
                    continue;
7✔
121
                }
122
                throw new Exception(
7✔
123
                    'Could not get image resource for "' . htmlspecialchars($file->getCombinedIdentifier()) . '".',
7✔
124
                    1253191060
7✔
125
                );
7✔
126
            }
127

128
            if (property_exists($GLOBALS['TSFE'], 'imagesOnPage')) {
14✔
129
                $GLOBALS['TSFE']->lastImageInfo = $imageInfo;
14✔
130
                $GLOBALS['TSFE']->imagesOnPage[] = $imageInfo[3];
14✔
131
            }
132

133
            if (GeneralUtility::isValidUrl($imageInfo[3])) {
14✔
134
                $imageSource = $imageInfo[3];
7✔
135
            } else {
136
                $imageSource = $GLOBALS['TSFE']->absRefPrefix . str_replace('%2F', '/', rawurlencode($imageInfo[3]));
7✔
137
            }
138

139
            if ($onlyProperties) {
14✔
140
                $file = ResourceUtility::getFileArray($file);
7✔
141
            }
142

143
            $images[] = [
14✔
144
                'info' => $imageInfo,
14✔
145
                'source' => $imageSource,
14✔
146
                'file' => $file
14✔
147
            ];
14✔
148
        }
149

150
        FrontendSimulationUtility::resetFrontendEnvironment($tsfeBackup);
21✔
151

152
        return $images;
21✔
153
    }
154

155
    /**
156
     * Turns a relative source URI into an absolute URL
157
     * if required.
158
     */
159
    public function preprocessSourceUri(string $source): string
160
    {
161
        if (!empty($GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_vhs.']['settings.']['prependPath'])) {
14✔
162
            $source = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_vhs.']['settings.']['prependPath'] . $source;
7✔
163
        } elseif (ContextUtility::isBackend() || !$this->arguments['relative']) {
7✔
164
            /** @var string $siteUrl */
165
            $siteUrl = GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
7✔
166
            $source = $siteUrl . $source;
7✔
167
        }
168
        return $source;
14✔
169
    }
170
}
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