TYPO3 CMS  TYPO3_6-2
LocalPreviewHelper.php
Go to the documentation of this file.
1 <?php
3 
21 
29  protected $processor;
30 
35  $this->processor = $processor;
36  }
37 
49  public function process(TaskInterface $task) {
50  $sourceFile = $task->getSourceFile();
51 
52  // Merge custom configuration with default configuration
53  $configuration = array_merge(array('width' => 64, 'height' => 64), $task->getConfiguration());
54  $configuration['width'] = MathUtility::forceIntegerInRange($configuration['width'], 1);
55  $configuration['height'] = MathUtility::forceIntegerInRange($configuration['height'], 1);
56 
57  // Do not scale up if the source file has a size and the target size is larger
58  if (
59  $sourceFile->getProperty('width') > 0 && $sourceFile->getProperty('height') > 0
60  && $configuration['width'] > $sourceFile->getProperty('width')
61  && $configuration['height'] > $sourceFile->getProperty('height')
62  ) {
63  return NULL;
64  }
65 
66  return $this->generatePreviewFromFile($sourceFile, $configuration, $this->getTemporaryFilePath($task));
67  }
68 
75  protected function getTemporaryFilePath(TaskInterface $task) {
76  return GeneralUtility::tempnam('preview_', '.' . $task->getTargetFileExtension());
77  }
78 
87  protected function generatePreviewFromFile(File $file, array $configuration, $targetFilePath) {
88  $originalFileName = $file->getForLocalProcessing(FALSE);
89 
90  // Check file extension
91  if ($file->getType() != File::FILETYPE_IMAGE &&
92  !GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $file->getExtension())) {
93  // Create a default image
94  $this->processor->getTemporaryImageWithText($targetFilePath, 'Not imagefile!', 'No ext!', $file->getName());
95  } else {
96  // Create the temporary file
97  if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im']) {
98  $parameters = '-sample ' . $configuration['width'] . 'x' . $configuration['height'] . ' '
99  . $this->processor->wrapFileName($originalFileName) . '[0] ' . $this->processor->wrapFileName($targetFilePath);
100 
101  $cmd = GeneralUtility::imageMagickCommand('convert', $parameters) . ' 2>&1';
102  CommandUtility::exec($cmd);
103 
104  if (!file_exists($targetFilePath)) {
105  // Create a error gif
106  $this->processor->getTemporaryImageWithText($targetFilePath, 'No thumb', 'generated!', $file->getName());
107  }
108  }
109  }
110 
111  return array(
112  'filePath' => $targetFilePath,
113  );
114  }
115 }
static imageMagickCommand($command, $parameters, $path='')
$parameters
Definition: FileDumpEID.php:15
generatePreviewFromFile(File $file, array $configuration, $targetFilePath)
static forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:32
static tempnam($filePrefix, $fileSuffix='')
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
static exec($command, &$output=NULL, &$returnValue=0)