80bea78143c9262053cbd2a7f2333565a825ff78
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Processing / LocalPreviewHelper.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Processing;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012-2013 Andreas Wolf <andreas.wolf@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the text file GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29
30 use \TYPO3\CMS\Core\Resource, \TYPO3\CMS\Core\Utility;
31
32 /**
33 * Helper for creating local image previews using TYPO3s image processing classes.
34 */
35 class LocalPreviewHelper {
36 /**
37 * @var LocalImageProcessor
38 */
39 protected $processor;
40
41 /**
42 * @param LocalImageProcessor $processor
43 */
44 public function __construct(LocalImageProcessor $processor) {
45 $this->processor = $processor;
46 }
47
48 /**
49 * This method actually does the processing of files locally
50 *
51 * takes the original file (on remote storages this will be fetched from the remote server)
52 * does the IM magic on the local server by creating a temporary typo3temp/ file
53 * copies the typo3temp/ file to the processing folder of the target storage
54 * removes the typo3temp/ file
55 *
56 * @param TaskInterface $task
57 * @return array
58 */
59 public function process(TaskInterface $task) {
60 $targetFile = $task->getTargetFile();
61 $sourceFile = $task->getSourceFile();
62
63 // Merge custom configuration with default configuration
64 $configuration = array_merge(array('width' => 64, 'height' => 64), $task->getConfiguration());
65 $configuration['width'] = Utility\MathUtility::forceIntegerInRange($configuration['width'], 1);
66 $configuration['height'] = Utility\MathUtility::forceIntegerInRange($configuration['height'], 1);
67
68 $originalFileName = $sourceFile->getForLocalProcessing(FALSE);
69
70 // Create the thumb filename in typo3temp/preview_....jpg
71 $temporaryFileName = Utility\GeneralUtility::tempnam('preview_') . '.' . $task->getTargetFileExtension();
72 // Check file extension
73 if ($sourceFile->getType() != Resource\File::FILETYPE_IMAGE &&
74 !Utility\GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $sourceFile->getExtension())) {
75 // Create a default image
76 $this->processor->getTemporaryImageWithText($temporaryFileName, 'Not imagefile!', 'No ext!', $sourceFile->getName());
77 } else {
78 // Create the temporary file
79 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im']) {
80 $parameters = '-sample ' . $configuration['width'] . 'x' . $configuration['height'] . ' '
81 . $this->processor->wrapFileName($originalFileName) . '[0] ' . $this->processor->wrapFileName($temporaryFileName);
82
83 $cmd = Utility\GeneralUtility::imageMagickCommand('convert', $parameters) . ' 2>&1';
84 Utility\CommandUtility::exec($cmd);
85
86 if (!file_exists($temporaryFileName)) {
87 // Create a error gif
88 $this->processor->getTemporaryImageWithText($temporaryFileName, 'No thumb', 'generated!', $sourceFile->getName());
89 }
90 }
91 }
92
93 return array(
94 'filePath' => $temporaryFileName,
95 );
96 }
97 }