• 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.22
/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\Imaging\ImageResource;
16
use TYPO3\CMS\Core\Utility\CommandUtility;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
use TYPO3\CMS\Core\Utility\MathUtility;
19
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
20
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
21
use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
22

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

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

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

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

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

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

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

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

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

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

160
        if (ContextUtility::isBackend() && strpos($src, '../') === 0) {
×
161
            $src = mb_substr($src, 3);
×
162
        }
163
        $imageInfo = $contentObject->getImgResource($src, $setup);
×
164
        if ($imageInfo instanceof ImageResource) {
×
165
            $this->imageInfo = $imageInfo->getLegacyImageResourceInformation();
×
166
        } else {
167
            $this->imageInfo = $imageInfo;
×
168
        }
169

170
        if (!is_array($this->imageInfo)) {
×
171
            if ($this->arguments['graceful'] ?? false) {
×
172
                $this->mediaSource = '';
×
173
                FrontendSimulationUtility::resetFrontendEnvironment($tsfeBackup);
×
174
                return;
×
175
            }
176
            throw new Exception('Could not get image resource for "' . htmlspecialchars($src) . '".', 1253191060);
×
177
        }
178

179
        if (property_exists($GLOBALS['TSFE'], 'lastImageInfo')) {
×
180
            $GLOBALS['TSFE']->lastImageInfo = $this->imageInfo;
×
181
        }
182

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

227
        if (property_exists($GLOBALS['TSFE'], 'imagesOnPage')) {
×
228
            $GLOBALS['TSFE']->imagesOnPage[] = $this->imageInfo[3];
×
229
        }
230

231
        FrontendSimulationUtility::resetFrontendEnvironment($tsfeBackup);
×
232
    }
233
}
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