TYPO3  7.6
LocalPreviewHelper.php
Go to the documentation of this file.
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 
22 
27 {
31  protected $processor;
32 
37  {
38  $this->processor = $processor;
39  }
40 
60  public function process(TaskInterface $task)
61  {
62  $sourceFile = $task->getSourceFile();
63 
64  // Merge custom configuration with default configuration
65  $configuration = array_merge(array('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 
85  protected function getTemporaryFilePath(TaskInterface $task)
86  {
87  return GeneralUtility::tempnam('preview_', '.' . $task->getTargetFileExtension());
88  }
89 
98  protected function generatePreviewFromFile(File $file, array $configuration, $targetFilePath)
99  {
100  $originalFileName = $file->getForLocalProcessing(false);
101 
102  // Check file extension
103  if ($file->getType() != File::FILETYPE_IMAGE &&
104  !GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $file->getExtension())) {
105  // Create a default image
106  $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
107  $graphicalFunctions->getTemporaryImageWithText(
108  $targetFilePath,
109  'Not imagefile!',
110  'No ext!',
111  $file->getName()
112  );
113  $result = array(
114  'filePath' => $targetFilePath,
115  );
116  } elseif ($file->getExtension() === 'svg') {
118  $gifBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Imaging\GifBuilder::class);
119  $gifBuilder->init();
120  $gifBuilder->absPrefix = PATH_site;
121  $info = $gifBuilder->getImageDimensions($originalFileName);
122  $newInfo = $gifBuilder->getImageScale($info, $configuration['width'], $configuration['height'], array());
123  $result = array(
124  'width' => $newInfo[0],
125  'height' => $newInfo[1],
126  'filePath' => '' // no file = use original
127  );
128  } else {
129  // Create the temporary file
130  if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im']) {
131  $parameters = '-sample ' . $configuration['width'] . 'x' . $configuration['height'] . ' '
132  . CommandUtility::escapeShellArgument($originalFileName) . '[0] ' . CommandUtility::escapeShellArgument($targetFilePath);
133 
134  $cmd = GeneralUtility::imageMagickCommand('convert', $parameters) . ' 2>&1';
135  CommandUtility::exec($cmd);
136 
137  if (!file_exists($targetFilePath)) {
138  // Create an error gif
139  $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
140  $graphicalFunctions->getTemporaryImageWithText(
141  $targetFilePath,
142  'No thumb',
143  'generated!',
144  $file->getName()
145  );
146  }
147  }
148  $result = array(
149  'filePath' => $targetFilePath,
150  );
151  }
152 
153  return $result;
154  }
155 }