e10a7bb3c16edadbd89325552b50ce4c1bf3b920
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Processing / LocalImageProcessor.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, \TYPO3\CMS\Core\Utility;
18
19 /**
20 * Processes Local Images files
21 */
22 class LocalImageProcessor implements ProcessorInterface {
23
24 /**
25 * @var \TYPO3\CMS\Core\Log\Logger
26 */
27 protected $logger;
28
29 /**
30 * Constructor
31 */
32 public function __construct() {
33 /** @var $logManager \TYPO3\CMS\Core\Log\LogManager */
34 $logManager = Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Log\LogManager');
35 $this->logger = $logManager->getLogger(__CLASS__);
36 }
37
38 /**
39 * Returns TRUE if this processor can process the given task.
40 *
41 * @param TaskInterface $task
42 * @return boolean
43 */
44 public function canProcessTask(TaskInterface $task) {
45 $canProcessTask = $task->getType() === 'Image';
46 $canProcessTask = $canProcessTask & in_array($task->getName(), array('Preview', 'CropScaleMask'));
47 return $canProcessTask;
48 }
49
50 /**
51 * Processes the given task.
52 *
53 * @param TaskInterface $task
54 * @throws \InvalidArgumentException
55 */
56 public function processTask(TaskInterface $task) {
57 if (!$this->canProcessTask($task)) {
58 throw new \InvalidArgumentException('Cannot process task of type "' . $task->getType() . '.' . $task->getName() . '"', 1350570621);
59 }
60 $helper = $this->getHelperByTaskName($task->getName());
61 try {
62 $result = $helper->process($task);
63 if ($result === NULL) {
64 $task->setExecuted(TRUE);
65 $task->getTargetFile()->setUsesOriginalFile();
66 } elseif (file_exists($result['filePath'])) {
67 $task->setExecuted(TRUE);
68 $graphicalFunctions = $this->getGraphicalFunctionsObject();
69 $imageDimensions = $graphicalFunctions->getImageDimensions($result['filePath']);
70
71 $task->getTargetFile()->setName($task->getTargetFileName());
72 $task->getTargetFile()->updateProperties(
73 array('width' => $imageDimensions[0], 'height' => $imageDimensions[1], 'size' => filesize($result['filePath']), 'checksum' => $task->getConfigurationChecksum())
74 );
75 $task->getTargetFile()->updateWithLocalFile($result['filePath']);
76 } else {
77 // Seems we have no valid processing result
78 $task->setExecuted(FALSE);
79 }
80 } catch (\Exception $e) {
81 $task->setExecuted(FALSE);
82 }
83 }
84
85 /**
86 * @param string $taskName
87 * @return LocalCropScaleMaskHelper|LocalPreviewHelper
88 * @throws \InvalidArgumentException
89 */
90 protected function getHelperByTaskName($taskName) {
91 switch ($taskName) {
92 case 'Preview':
93 $helper = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper', $this);
94 break;
95 case 'CropScaleMask':
96 $helper = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Resource\Processing\LocalCropScaleMaskHelper', $this);
97 break;
98 default:
99 throw new \InvalidArgumentException('Cannot find helper for task name: "' . $taskName . '"', 1353401352);
100 }
101
102 return $helper;
103 }
104
105 /**
106 * Escapes a file name so it can safely be used on the command line.
107 *
108 * @param string $inputName filename to safeguard, must not be empty
109 * @return string $inputName escaped as needed
110 *
111 * @internal Don't use this method from outside the LocalImageProcessor!
112 */
113 public function wrapFileName($inputName) {
114 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']) {
115 $currentLocale = setlocale(LC_CTYPE, 0);
116 setlocale(LC_CTYPE, $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemLocale']);
117 $escapedInputName = escapeshellarg($inputName);
118 setlocale(LC_CTYPE, $currentLocale);
119 } else {
120 $escapedInputName = escapeshellarg($inputName);
121 }
122 return $escapedInputName;
123 }
124
125 /**
126 * Creates error image based on gfx/notfound_thumb.png
127 * Requires GD lib enabled, otherwise it will exit with the three
128 * textstrings outputted as text. Outputs the image stream to browser and exits!
129 *
130 * @param string $filename Name of the file
131 * @param string $textline1 Text line 1
132 * @param string $textline2 Text line 2
133 * @param string $textline3 Text line 3
134 * @return void
135 * @throws \RuntimeException
136 *
137 * @internal Don't use this method from outside the LocalImageProcessor!
138 */
139 public function getTemporaryImageWithText($filename, $textline1, $textline2, $textline3) {
140 if (!$GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib']) {
141 throw new \RuntimeException('TYPO3 Fatal Error: No gdlib. ' . $textline1 . ' ' . $textline2 . ' ' . $textline3, 1270853952);
142 }
143 // Creates the basis for the error image
144 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']) {
145 $im = imagecreatefrompng(PATH_typo3 . 'gfx/notfound_thumb.png');
146 } else {
147 $im = imagecreatefromgif(PATH_typo3 . 'gfx/notfound_thumb.gif');
148 }
149 // Sets background color and print color.
150 $white = imageColorAllocate($im, 255, 255, 255);
151 $black = imageColorAllocate($im, 0, 0, 0);
152 // Prints the text strings with the build-in font functions of GD
153 $x = 0;
154 $font = 0;
155 if ($textline1) {
156 imagefilledrectangle($im, $x, 9, 56, 16, $white);
157 imageString($im, $font, $x, 9, $textline1, $black);
158 }
159 if ($textline2) {
160 imagefilledrectangle($im, $x, 19, 56, 26, $white);
161 imageString($im, $font, $x, 19, $textline2, $black);
162 }
163 if ($textline3) {
164 imagefilledrectangle($im, $x, 29, 56, 36, $white);
165 imageString($im, $font, $x, 29, substr($textline3, -14), $black);
166 }
167 // Outputting the image stream and exit
168 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']) {
169 imagePng($im, $filename);
170 } else {
171 imageGif($im, $filename);
172 }
173 }
174
175 /**
176 * @return \TYPO3\CMS\Core\Imaging\GraphicalFunctions
177 */
178 protected function getGraphicalFunctionsObject() {
179 static $graphicalFunctionsObject = NULL;
180
181 if ($graphicalFunctionsObject === NULL) {
182 $graphicalFunctionsObject = Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Imaging\\GraphicalFunctions');
183 }
184
185 return $graphicalFunctionsObject;
186 }
187 }