[CLEANUP] The correct case must be used for standard PHP types in phpdoc
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Processing / LocalPreviewHelper.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\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\Imaging\GraphicalFunctions;
18 use TYPO3\CMS\Core\Resource\File;
19 use TYPO3\CMS\Core\Utility\CommandUtility;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21 use TYPO3\CMS\Core\Utility\MathUtility;
22
23 /**
24 * Helper for creating local image previews using TYPO3s image processing classes.
25 */
26 class LocalPreviewHelper
27 {
28 /**
29 * @var LocalImageProcessor
30 */
31 protected $processor;
32
33 /**
34 * @param LocalImageProcessor $processor
35 */
36 public function __construct(LocalImageProcessor $processor)
37 {
38 $this->processor = $processor;
39 }
40
41 /**
42 * This method actually does the processing of files locally
43 *
44 * takes the original file (on remote storages this will be fetched from the remote server)
45 * does the IM magic on the local server by creating a temporary typo3temp/ file
46 * copies the typo3temp/ file to the processing folder of the target storage
47 * removes the typo3temp/ file
48 *
49 * The returned array has the following structure:
50 * width => 100
51 * height => 200
52 * filePath => /some/path
53 *
54 * If filePath isn't set but width and height are the original file is used as ProcessedFile
55 * with the returned width and height. This is for example useful for SVG images.
56 *
57 * @param TaskInterface $task
58 * @return array|null
59 */
60 public function process(TaskInterface $task)
61 {
62 $sourceFile = $task->getSourceFile();
63
64 // Merge custom configuration with default configuration
65 $configuration = array_merge(['width' => 64, 'height' => 64], $task->getConfiguration());
66 $configuration['width'] = MathUtility::forceIntegerInRange($configuration['width'], 1);
67 $configuration['height'] = MathUtility::forceIntegerInRange($configuration['height'], 1);
68
69 // Do not scale up if the source file has a size and the target size is larger
70 if ($sourceFile->getProperty('width') > 0 && $sourceFile->getProperty('height') > 0
71 && $configuration['width'] > $sourceFile->getProperty('width')
72 && $configuration['height'] > $sourceFile->getProperty('height')) {
73 return null;
74 }
75
76 return $this->generatePreviewFromFile($sourceFile, $configuration, $this->getTemporaryFilePath($task));
77 }
78
79 /**
80 * Returns the path to a temporary file for processing
81 *
82 * @param TaskInterface $task
83 * @return string
84 */
85 protected function getTemporaryFilePath(TaskInterface $task)
86 {
87 return GeneralUtility::tempnam('preview_', '.' . $task->getTargetFileExtension());
88 }
89
90 /**
91 * Generates a preview for a file
92 *
93 * @param File $file The source file
94 * @param array $configuration Processing configuration
95 * @param string $targetFilePath Output file path
96 * @return array
97 */
98 protected function generatePreviewFromFile(File $file, array $configuration, $targetFilePath)
99 {
100 // Check file extension
101 if ($file->getType() !== File::FILETYPE_IMAGE
102 && !GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $file->getExtension())
103 ) {
104 // Create a default image
105 $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
106 $graphicalFunctions->init();
107 $graphicalFunctions->getTemporaryImageWithText(
108 $targetFilePath,
109 'Not imagefile!',
110 'No ext!',
111 $file->getName()
112 );
113 return [
114 'filePath' => $targetFilePath,
115 ];
116 }
117
118 $originalFileName = $file->getForLocalProcessing(false);
119 if ($file->getExtension() === 'svg') {
120 /** @var $gifBuilder \TYPO3\CMS\Frontend\Imaging\GifBuilder */
121 $gifBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Imaging\GifBuilder::class);
122 $gifBuilder->init();
123 $info = $gifBuilder->getImageDimensions($originalFileName);
124 $newInfo = $gifBuilder->getImageScale($info, $configuration['width'], $configuration['height'], []);
125 $result = [
126 'width' => $newInfo[0],
127 'height' => $newInfo[1],
128 'filePath' => '' // no file = use original
129 ];
130 } else {
131 // Create the temporary file
132 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['processor_enabled']) {
133 $parameters = '-sample ' . $configuration['width'] . 'x' . $configuration['height'] . ' '
134 . CommandUtility::escapeShellArgument($originalFileName) . '[0] ' . CommandUtility::escapeShellArgument($targetFilePath);
135
136 $cmd = CommandUtility::imageMagickCommand('convert', $parameters) . ' 2>&1';
137 CommandUtility::exec($cmd);
138
139 if (!file_exists($targetFilePath)) {
140 // Create an error gif
141 $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
142 $graphicalFunctions->init();
143 $graphicalFunctions->getTemporaryImageWithText(
144 $targetFilePath,
145 'No thumb',
146 'generated!',
147 $file->getName()
148 );
149 }
150 }
151 $result = [
152 'filePath' => $targetFilePath,
153 ];
154 }
155
156 return $result;
157 }
158 }