[BUGFIX] Fix several typos in php comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / OnlineMedia / Processing / PreviewProcessing.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\OnlineMedia\Processing;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Core\Environment;
18 use TYPO3\CMS\Core\Imaging\GraphicalFunctions;
19 use TYPO3\CMS\Core\Imaging\ImageMagickFile;
20 use TYPO3\CMS\Core\Resource\Driver\DriverInterface;
21 use TYPO3\CMS\Core\Resource\File;
22 use TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry;
23 use TYPO3\CMS\Core\Resource\ProcessedFile;
24 use TYPO3\CMS\Core\Resource\ProcessedFileRepository;
25 use TYPO3\CMS\Core\Resource\Service\FileProcessingService;
26 use TYPO3\CMS\Core\Type\File\ImageInfo;
27 use TYPO3\CMS\Core\Utility\CommandUtility;
28 use TYPO3\CMS\Core\Utility\GeneralUtility;
29 use TYPO3\CMS\Core\Utility\PathUtility;
30 use TYPO3\CMS\Frontend\Imaging\GifBuilder;
31
32 /**
33 * Preview of Online Media item Processing
34 */
35 class PreviewProcessing
36 {
37 /**
38 * @param ProcessedFile $processedFile
39 * @return bool
40 */
41 protected function needsReprocessing($processedFile)
42 {
43 return $processedFile->isNew()
44 || (!$processedFile->usesOriginalFile() && !$processedFile->exists())
45 || $processedFile->isOutdated();
46 }
47
48 /**
49 * Process file
50 * Create static image preview for Online Media item when possible
51 *
52 * @param FileProcessingService $fileProcessingService
53 * @param DriverInterface $driver
54 * @param ProcessedFile $processedFile
55 * @param File $file
56 * @param string $taskType
57 * @param array $configuration
58 */
59 public function processFile(FileProcessingService $fileProcessingService, DriverInterface $driver, ProcessedFile $processedFile, File $file, $taskType, array $configuration)
60 {
61 if ($taskType !== ProcessedFile::CONTEXT_IMAGEPREVIEW && $taskType !== ProcessedFile::CONTEXT_IMAGECROPSCALEMASK) {
62 return;
63 }
64 // Check if processing is needed
65 if (!$this->needsReprocessing($processedFile)) {
66 return;
67 }
68 // Check if there is an OnlineMediaHelper registered for this file type
69 $helper = OnlineMediaHelperRegistry::getInstance()->getOnlineMediaHelper($file);
70 if ($helper === false) {
71 return;
72 }
73 // Check if helper provides a preview image
74 $temporaryFileName = $helper->getPreviewImage($file);
75 if (empty($temporaryFileName) || !file_exists($temporaryFileName)) {
76 return;
77 }
78 $temporaryFileNameForResizedThumb = uniqid(Environment::getVarPath() . '/transient/online_media_' . $file->getHashedIdentifier()) . '.jpg';
79 $configuration = $processedFile->getProcessingConfiguration();
80 switch ($taskType) {
81 case ProcessedFile::CONTEXT_IMAGEPREVIEW:
82 $this->resizeImage($temporaryFileName, $temporaryFileNameForResizedThumb, $configuration);
83 break;
84
85 case ProcessedFile::CONTEXT_IMAGECROPSCALEMASK:
86 $this->cropScaleImage($temporaryFileName, $temporaryFileNameForResizedThumb, $configuration);
87 break;
88 }
89 GeneralUtility::unlink_tempfile($temporaryFileName);
90 if (is_file($temporaryFileNameForResizedThumb)) {
91 $processedFile->setName($this->getTargetFileName($processedFile));
92 $imageInfo = GeneralUtility::makeInstance(ImageInfo::class, $temporaryFileNameForResizedThumb);
93 $processedFile->updateProperties(
94 [
95 'width' => $imageInfo->getWidth(),
96 'height' => $imageInfo->getHeight(),
97 'size' => filesize($temporaryFileNameForResizedThumb),
98 'checksum' => $processedFile->getTask()->getConfigurationChecksum()
99 ]
100 );
101 $processedFile->updateWithLocalFile($temporaryFileNameForResizedThumb);
102 GeneralUtility::unlink_tempfile($temporaryFileNameForResizedThumb);
103
104 /** @var ProcessedFileRepository $processedFileRepository */
105 $processedFileRepository = GeneralUtility::makeInstance(ProcessedFileRepository::class);
106 $processedFileRepository->add($processedFile);
107 }
108 }
109
110 /**
111 * @param ProcessedFile $processedFile
112 * @param string $prefix
113 * @return string
114 */
115 protected function getTargetFileName(ProcessedFile $processedFile, $prefix = 'preview_')
116 {
117 return $prefix . $processedFile->getTask()->getConfigurationChecksum() . '_' . $processedFile->getOriginalFile()->getNameWithoutExtension() . '.jpg';
118 }
119
120 /**
121 * @param string $originalFileName
122 * @param string $temporaryFileName
123 * @param array $configuration
124 */
125 protected function resizeImage($originalFileName, $temporaryFileName, $configuration)
126 {
127 // Create the temporary file
128 if (empty($GLOBALS['TYPO3_CONF_VARS']['GFX']['processor_enabled'])) {
129 return;
130 }
131
132 if (file_exists($originalFileName)) {
133 $arguments = CommandUtility::escapeShellArguments([
134 'width' => $configuration['width'],
135 'height' => $configuration['height'],
136 ]);
137 $parameters = '-sample ' . $arguments['width'] . 'x' . $arguments['height']
138 . ' ' . ImageMagickFile::fromFilePath($originalFileName, 0)
139 . ' ' . CommandUtility::escapeShellArgument($temporaryFileName);
140
141 $cmd = CommandUtility::imageMagickCommand('convert', $parameters) . ' 2>&1';
142 CommandUtility::exec($cmd);
143 }
144
145 if (!file_exists($temporaryFileName)) {
146 // Create an error image
147 $graphicalFunctions = $this->getGraphicalFunctionsObject();
148 $graphicalFunctions->getTemporaryImageWithText($temporaryFileName, 'No thumb', 'generated!', PathUtility::basename($originalFileName));
149 }
150 }
151
152 /**
153 * cropScaleImage
154 *
155 * @param string $originalFileName
156 * @param string $temporaryFileName
157 * @param array $configuration
158 */
159 protected function cropScaleImage($originalFileName, $temporaryFileName, $configuration)
160 {
161 if (file_exists($originalFileName)) {
162 $gifBuilder = GeneralUtility::makeInstance(GifBuilder::class);
163
164 $options = $this->getConfigurationForImageCropScaleMask($configuration, $gifBuilder);
165 $info = $gifBuilder->getImageDimensions($originalFileName);
166 $data = $gifBuilder->getImageScale($info, $configuration['width'], $configuration['height'], $options);
167
168 $info[0] = $data[0];
169 $info[1] = $data[1];
170 $frame = '';
171 $params = $gifBuilder->cmds['jpg'];
172
173 // Cropscaling:
174 if ($data['crs']) {
175 if (!$data['origW']) {
176 $data['origW'] = $data[0];
177 }
178 if (!$data['origH']) {
179 $data['origH'] = $data[1];
180 }
181 $offsetX = (int)(($data[0] - $data['origW']) * ($data['cropH'] + 100) / 200);
182 $offsetY = (int)(($data[1] - $data['origH']) * ($data['cropV'] + 100) / 200);
183 $params .= ' -crop ' . $data['origW'] . 'x' . $data['origH'] . '+' . $offsetX . '+' . $offsetY . '! ';
184 }
185 $command = $gifBuilder->scalecmd . ' ' . $info[0] . 'x' . $info[1] . '! ' . $params . ' ';
186 $gifBuilder->imageMagickExec($originalFileName, $temporaryFileName, $command, $frame);
187 }
188 if (!file_exists($temporaryFileName)) {
189 // Create an error image
190 $graphicalFunctions = $this->getGraphicalFunctionsObject();
191 $graphicalFunctions->getTemporaryImageWithText($temporaryFileName, 'No thumb', 'generated!', PathUtility::basename($originalFileName));
192 }
193 }
194
195 /**
196 * Get configuration for ImageCropScaleMask processing
197 *
198 * @param array $configuration
199 * @param GifBuilder $gifBuilder
200 * @return array
201 */
202 protected function getConfigurationForImageCropScaleMask(array $configuration, GifBuilder $gifBuilder)
203 {
204 if (!empty($configuration['useSample'])) {
205 $gifBuilder->scalecmd = '-sample';
206 }
207 $options = [];
208 if (!empty($configuration['maxWidth'])) {
209 $options['maxW'] = $configuration['maxWidth'];
210 }
211 if (!empty($configuration['maxHeight'])) {
212 $options['maxH'] = $configuration['maxHeight'];
213 }
214 if (!empty($configuration['minWidth'])) {
215 $options['minW'] = $configuration['minWidth'];
216 }
217 if (!empty($configuration['minHeight'])) {
218 $options['minH'] = $configuration['minHeight'];
219 }
220
221 $options['noScale'] = $configuration['noScale'];
222
223 return $options;
224 }
225
226 /**
227 * @return GraphicalFunctions
228 */
229 protected function getGraphicalFunctionsObject(): GraphicalFunctions
230 {
231 return GeneralUtility::makeInstance(GraphicalFunctions::class);
232 }
233 }