46d6b978e647877077e83bdca2b8f2d68aa501d8
[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\Resource\File;
18 use TYPO3\CMS\Core\Utility\CommandUtility;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Core\Utility\MathUtility;
21 use TYPO3\CMS\Core\Imaging\GraphicalFunctions;
22
23 /**
24 * Helper for creating local image previews using TYPO3s image processing classes.
25 */
26 class LocalPreviewHelper {
27 /**
28 * @var LocalImageProcessor
29 */
30 protected $processor;
31
32 /**
33 * @param LocalImageProcessor $processor
34 */
35 public function __construct(LocalImageProcessor $processor) {
36 $this->processor = $processor;
37 }
38
39 /**
40 * This method actually does the processing of files locally
41 *
42 * takes the original file (on remote storages this will be fetched from the remote server)
43 * does the IM magic on the local server by creating a temporary typo3temp/ file
44 * copies the typo3temp/ file to the processing folder of the target storage
45 * removes the typo3temp/ file
46 *
47 * The returned array has the following structure:
48 * width => 100
49 * height => 200
50 * filePath => /some/path
51 *
52 * If filePath isn't set but width and height are the original file is used as ProcessedFile
53 * with the returned width and height. This is for example useful for SVG images.
54 *
55 * @param TaskInterface $task
56 * @return array|NULL
57 */
58 public function process(TaskInterface $task) {
59 $sourceFile = $task->getSourceFile();
60
61 // Merge custom configuration with default configuration
62 $configuration = array_merge(array('width' => 64, 'height' => 64), $task->getConfiguration());
63 $configuration['width'] = MathUtility::forceIntegerInRange($configuration['width'], 1);
64 $configuration['height'] = MathUtility::forceIntegerInRange($configuration['height'], 1);
65
66 // Only scale down when new dimensions are smaller then existing image
67 if ($configuration['width'] > $sourceFile->getProperty('width')
68 && $configuration['height'] > $sourceFile->getProperty('height')) {
69 return NULL;
70 }
71
72 $originalFileName = $sourceFile->getForLocalProcessing(FALSE);
73
74 // Create a temporaryFile
75 $temporaryFileName = GeneralUtility::tempnam('preview_', '.' . $task->getTargetFileExtension());
76 // Check file extension
77 if ($sourceFile->getType() != File::FILETYPE_IMAGE &&
78 !GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $sourceFile->getExtension())) {
79 // Create a default image
80 $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
81 $graphicalFunctions->getTemporaryImageWithText(
82 $temporaryFileName,
83 'Not imagefile!',
84 'No ext!',
85 $sourceFile->getName()
86 );
87 $result = array(
88 'filePath' => $temporaryFileName,
89 );
90 } elseif ($sourceFile->getExtension() === 'svg') {
91 /** @var $gifBuilder \TYPO3\CMS\Frontend\Imaging\GifBuilder */
92 $gifBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Imaging\GifBuilder::class);
93 $gifBuilder->init();
94 $gifBuilder->absPrefix = PATH_site;
95 $info = $gifBuilder->getImageDimensions($originalFileName);
96 $newInfo = $gifBuilder->getImageScale($info, $configuration['width'], $configuration['height'], array());
97 $result = array(
98 'width' => $newInfo[0],
99 'height' => $newInfo[1],
100 'filePath' => '' // no file = use original
101 );
102 } else {
103 // Create the temporary file
104 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im']) {
105 $parameters = '-sample ' . $configuration['width'] . 'x' . $configuration['height'] . ' '
106 . CommandUtility::escapeShellArgument($originalFileName) . '[0] ' . CommandUtility::escapeShellArgument($temporaryFileName);
107
108 $cmd = GeneralUtility::imageMagickCommand('convert', $parameters) . ' 2>&1';
109 CommandUtility::exec($cmd);
110
111 if (!file_exists($temporaryFileName)) {
112 // Create a error gif
113 $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
114 $graphicalFunctions->getTemporaryImageWithText(
115 $temporaryFileName,
116 'No thumb',
117 'generated!',
118 $sourceFile->getName()
119 );
120 }
121 }
122 $result = array(
123 'filePath' => $temporaryFileName,
124 );
125 }
126
127 return $result;
128 }
129
130 }