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

FluidTYPO3 / vhs / 12966185640

25 Jan 2025 03:24PM UTC coverage: 72.268% (-0.2%) from 72.434%
12966185640

Pull #1922

github

web-flow
Merge 6eba7534d into 02c21f3ce
Pull Request #1922: [BUGFIX] Use late instancing of ContentObjectRenderer

16 of 39 new or added lines in 11 files covered. (41.03%)

5548 of 7677 relevant lines covered (72.27%)

13.46 hits per line

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

42.07
/Classes/ViewHelpers/Media/Image/AbstractImageViewHelper.php
1
<?php
2
namespace FluidTYPO3\Vhs\ViewHelpers\Media\Image;
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\ViewHelpers\Media\AbstractMediaViewHelper;
15
use TYPO3\CMS\Core\Utility\CommandUtility;
16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
use TYPO3\CMS\Core\Utility\MathUtility;
18
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
19
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
20
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
21

22
/**
23
 * Base class for image related view helpers adapted from FLUID
24
 * original image viewhelper.
25
 */
26

27
abstract class AbstractImageViewHelper extends AbstractMediaViewHelper
28
{
29
    /**
30
     * @var ConfigurationManagerInterface
31
     */
32
    protected $configurationManager;
33

34
    /**
35
     * Result of \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::getImgResource()
36
     * @var array|null
37
     */
38
    protected $imageInfo;
39

40
    public function injectConfigurationManager(ConfigurationManagerInterface $configurationManager): void
41
    {
42
        $this->configurationManager = $configurationManager;
36✔
43
    }
44

45
    public function initializeArguments(): void
46
    {
47
        parent::initializeArguments();
24✔
48
        $this->registerArgument(
24✔
49
            'width',
24✔
50
            'string',
24✔
51
            'Width of the image. This can be a numeric value representing the fixed width of the image in pixels. ' .
24✔
52
            'But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width ' .
24✔
53
            'for possible options.'
24✔
54
        );
24✔
55
        $this->registerArgument(
24✔
56
            'height',
24✔
57
            'string',
24✔
58
            'Height of the image. This can be a numeric value representing the fixed height of the image in pixels. ' .
24✔
59
            'But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width ' .
24✔
60
            'for possible options.'
24✔
61
        );
24✔
62
        $this->registerArgument('maxW', 'integer', 'Maximum Width of the image. (no upscaling)');
24✔
63
        $this->registerArgument('maxH', 'integer', 'Maximum Height of the image. (no upscaling)');
24✔
64
        $this->registerArgument('minW', 'integer', 'Minimum Width of the image.');
24✔
65
        $this->registerArgument('minH', 'integer', 'Minimum Height of the image.');
24✔
66
        $this->registerArgument(
24✔
67
            'format',
24✔
68
            'string',
24✔
69
            'Format of the processed file - also determines the target file format. If blank, TYPO3/IM/GM default is ' .
24✔
70
            'taken into account.'
24✔
71
        );
24✔
72
        $this->registerArgument(
24✔
73
            'quality',
24✔
74
            'integer',
24✔
75
            'Quality of the processed image. If blank/not present falls back to the default quality defined in ' .
24✔
76
            'install tool.',
24✔
77
            false,
24✔
78
            $GLOBALS['TYPO3_CONF_VARS']['GFX']['jpg_quality'] ?? 90
24✔
79
        );
24✔
80
        $this->registerArgument(
24✔
81
            'treatIdAsReference',
24✔
82
            'boolean',
24✔
83
            'When TRUE treat given src argument as sys_file_reference record. Applies only to TYPO3 6.x and above.',
24✔
84
            false,
24✔
85
            false
24✔
86
        );
24✔
87
        $this->registerArgument('canvasWidth', 'integer', 'Width of an optional canvas to place the image on.');
24✔
88
        $this->registerArgument('canvasHeight', 'integer', 'Height of an optional canvas to place the image on.');
24✔
89
        $this->registerArgument(
24✔
90
            'canvasColor',
24✔
91
            'string',
24✔
92
            'Background color of an optional canvas to place the image on (hex triplet).'
24✔
93
        );
24✔
94
        $this->registerArgument(
24✔
95
            'transparencyColor',
24✔
96
            'string',
24✔
97
            'Color to set transparent when using canvas feature (hex triplet).'
24✔
98
        );
24✔
99
        $this->registerArgument('crop', 'string', 'Information generated by the backend\'s graphical cropping UI');
24✔
100
        $this->registerArgument(
24✔
101
            'graceful',
24✔
102
            'bool',
24✔
103
            'Set to TRUE to ignore files that cannot be loaded. Default behavior is to throw an Exception.',
24✔
104
            false,
24✔
105
            false
24✔
106
        );
24✔
107
    }
108

109
    public function preprocessImage(?string $imageSource = null): void
110
    {
111
        /** @var string $src */
112
        $src = (null === $imageSource) ? $this->arguments['src'] : $imageSource;
×
113
        $width = $this->arguments['width'];
×
114
        $height = $this->arguments['height'];
×
115
        $minW = $this->arguments['minW'];
×
116
        $minH = $this->arguments['minH'];
×
117
        $maxW = $this->arguments['maxW'];
×
118
        $maxH = $this->arguments['maxH'];
×
119
        $format = $this->arguments['format'];
×
120
        /** @var int $quality */
121
        $quality = $this->arguments['quality'];
×
122
        $treatIdAsReference = (boolean) $this->arguments['treatIdAsReference'];
×
123
        $crop = $this->arguments['crop'];
×
124

125
        if ($src instanceof FileReference) {
×
126
            if ($crop === null) {
×
127
                $crop = $src->_getProperty('crop');
×
128
            }
129
            $src = $src->getUid();
×
130
            $treatIdAsReference = true;
×
131
        }
132

133
        $tsfeBackup = FrontendSimulationUtility::simulateFrontendEnvironment();
×
134

NEW
135
        $contentObject = ContentObjectFetcher::resolve($this->configurationManager);
×
NEW
136
        if ($contentObject === null) {
×
NEW
137
            throw new Exception(static::class . ' requires a ContentObjectRenderer, none found', 1737808465);
×
138
        }
139

140
        $setup = [
×
141
            'width' => $width,
×
142
            'height' => $height,
×
143
            'minW' => $minW,
×
144
            'minH' => $minH,
×
145
            'maxW' => $maxW,
×
146
            'maxH' => $maxH,
×
147
            'treatIdAsReference' => $treatIdAsReference,
×
148
            'crop' => $crop,
×
149
        ];
×
150
        if (!empty($format)) {
×
151
            $setup['ext'] = $format;
×
152
        }
153
        if (0 < (integer) $quality) {
×
154
            /** @var int $quality */
155
            $quality = MathUtility::forceIntegerInRange($quality, 10, 100, 75);
×
156
            $setup['params'] = '-quality ' . $quality;
×
157
        }
158

159
        if (ContextUtility::isBackend() && strpos($src, '../') === 0) {
×
160
            $src = mb_substr($src, 3);
×
161
        }
NEW
162
        $this->imageInfo = $contentObject->getImgResource($src, $setup);
×
163

164
        if (!is_array($this->imageInfo)) {
×
165
            if ($this->arguments['graceful'] ?? false) {
×
166
                $this->mediaSource = '';
×
167
                FrontendSimulationUtility::resetFrontendEnvironment($tsfeBackup);
×
168
                return;
×
169
            }
170
            throw new Exception('Could not get image resource for "' . htmlspecialchars($src) . '".', 1253191060);
×
171
        }
172

173
        if (property_exists($GLOBALS['TSFE'], 'lastImageInfo')) {
×
174
            $GLOBALS['TSFE']->lastImageInfo = $this->imageInfo;
×
175
        }
176

177
        if ($this->hasArgument('canvasWidth') && $this->hasArgument('canvasHeight')) {
×
178
            /** @var int $canvasWidth */
179
            $canvasWidth = $this->arguments['canvasWidth'];
×
180
            /** @var int $canvasHeight */
181
            $canvasHeight = $this->arguments['canvasHeight'];
×
182
            /** @var string $canvasColor */
183
            $canvasColor = $this->arguments['canvasColor'] ?? '';
×
184
            $canvasColor = str_replace('#', '', $canvasColor);
×
185
            $originalFilename = $this->imageInfo[3];
×
186
            $originalExtension = mb_substr($originalFilename, -3);
×
187
            $tempPath = 'typo3temp/assets/';
×
188
            $destinationFilename = $tempPath .
×
189
                'vhs-canvas-' .
×
190
                md5($originalFilename . $canvasColor . $canvasWidth . $canvasHeight) .
×
191
                '.' .
×
192
                $originalExtension;
×
193
            $destinationFilepath = GeneralUtility::getFileAbsFileName($destinationFilename);
×
194
            $transparency = '';
×
195
            if ($this->hasArgument('transparencyColor')) {
×
196
                /** @var string $transparencyColor */
197
                $transparencyColor = $this->arguments['transparencyColor'];
×
198
                $transparencyColor = str_replace('#', '', $transparencyColor);
×
199
                $transparency = ' -transparent \'#' . $transparencyColor . '\'';
×
200
            }
201
            if (!file_exists($destinationFilepath)) {
×
202
                $arguments = sprintf(
×
203
                    '%s -background \'#%s\'%s -gravity center -extent %dx%d %s',
×
204
                    $originalFilename,
×
205
                    $canvasColor,
×
206
                    $transparency,
×
207
                    $canvasWidth,
×
208
                    $canvasHeight,
×
209
                    $destinationFilepath
×
210
                );
×
211
                $command = CommandUtility::imageMagickCommand('convert', $arguments);
×
212
                CommandUtility::exec($command);
×
213
            }
214
            $this->mediaSource = $destinationFilename;
×
215
        } elseif ($this->imageInfo['processedFile'] ?? false) {
×
216
            $this->mediaSource = $this->imageInfo['processedFile']->getPublicUrl();
×
217
        } else {
218
            $this->mediaSource = rawurldecode($this->imageInfo[3]);
×
219
        }
220

221
        if (property_exists($GLOBALS['TSFE'], 'imagesOnPage')) {
×
222
            $GLOBALS['TSFE']->imagesOnPage[] = $this->imageInfo[3];
×
223
        }
224

225
        FrontendSimulationUtility::resetFrontendEnvironment($tsfeBackup);
×
226
    }
227
}
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