[BUGFIX] Gifbuilder: Fix image-mask-functionality
[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 $imageDimensions = $this->getGraphicalFunctionsObject()->getImageDimensions($result['filePath']);
69 $task->getTargetFile()->setName($task->getTargetFileName());
70 $task->getTargetFile()->updateProperties(
71 array('width' => $imageDimensions[0], 'height' => $imageDimensions[1], 'size' => filesize($result['filePath']), 'checksum' => $task->getConfigurationChecksum())
72 );
73 $task->getTargetFile()->updateWithLocalFile($result['filePath']);
74 } else {
75 // Seems we have no valid processing result
76 $task->setExecuted(FALSE);
77 }
78 } catch (\Exception $e) {
79 $task->setExecuted(FALSE);
80 }
81 }
82
83 /**
84 * @param string $taskName
85 * @return LocalCropScaleMaskHelper|LocalPreviewHelper
86 * @throws \InvalidArgumentException
87 */
88 protected function getHelperByTaskName($taskName) {
89 switch ($taskName) {
90 case 'Preview':
91 $helper = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper', $this);
92 break;
93 case 'CropScaleMask':
94 $helper = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Core\Resource\Processing\LocalCropScaleMaskHelper', $this);
95 break;
96 default:
97 throw new \InvalidArgumentException('Cannot find helper for task name: "' . $taskName . '"', 1353401352);
98 }
99
100 return $helper;
101 }
102
103 /**
104 * Escapes a file name so it can safely be used on the command line.
105 *
106 * @param string $inputName filename to safeguard, must not be empty
107 * @return string $inputName escaped as needed
108 *
109 * @internal Don't use this method from outside the LocalImageProcessor!
110 */
111 public function wrapFileName($inputName) {
112 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']) {
113 $currentLocale = setlocale(LC_CTYPE, 0);
114 setlocale(LC_CTYPE, $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemLocale']);
115 $escapedInputName = escapeshellarg($inputName);
116 setlocale(LC_CTYPE, $currentLocale);
117 } else {
118 $escapedInputName = escapeshellarg($inputName);
119 }
120 return $escapedInputName;
121 }
122
123 /**
124 * Creates error image based on gfx/notfound_thumb.png
125 * Requires GD lib enabled, otherwise it will exit with the three
126 * textstrings outputted as text. Outputs the image stream to browser and exits!
127 *
128 * @param string $filename Name of the file
129 * @param string $textline1 Text line 1
130 * @param string $textline2 Text line 2
131 * @param string $textline3 Text line 3
132 * @return void
133 * @throws \RuntimeException
134 *
135 * @internal Don't use this method from outside the LocalImageProcessor!
136 */
137 public function getTemporaryImageWithText($filename, $textline1, $textline2, $textline3) {
138 if (!$GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib']) {
139 throw new \RuntimeException('TYPO3 Fatal Error: No gdlib. ' . $textline1 . ' ' . $textline2 . ' ' . $textline3, 1270853952);
140 }
141 // Creates the basis for the error image
142 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']) {
143 $im = imagecreatefrompng(PATH_typo3 . 'gfx/notfound_thumb.png');
144 } else {
145 $im = imagecreatefromgif(PATH_typo3 . 'gfx/notfound_thumb.gif');
146 }
147 // Sets background color and print color.
148 $white = imageColorAllocate($im, 255, 255, 255);
149 $black = imageColorAllocate($im, 0, 0, 0);
150 // Prints the text strings with the build-in font functions of GD
151 $x = 0;
152 $font = 0;
153 if ($textline1) {
154 imagefilledrectangle($im, $x, 9, 56, 16, $white);
155 imageString($im, $font, $x, 9, $textline1, $black);
156 }
157 if ($textline2) {
158 imagefilledrectangle($im, $x, 19, 56, 26, $white);
159 imageString($im, $font, $x, 19, $textline2, $black);
160 }
161 if ($textline3) {
162 imagefilledrectangle($im, $x, 29, 56, 36, $white);
163 imageString($im, $font, $x, 29, substr($textline3, -14), $black);
164 }
165 // Outputting the image stream and exit
166 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']) {
167 imagePng($im, $filename);
168 } else {
169 imageGif($im, $filename);
170 }
171 }
172
173 /**
174 * @return \TYPO3\CMS\Core\Imaging\GraphicalFunctions
175 */
176 protected function getGraphicalFunctionsObject() {
177 static $graphicalFunctionsObject = NULL;
178
179 if ($graphicalFunctionsObject === NULL) {
180 $graphicalFunctionsObject = Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Imaging\\GraphicalFunctions');
181 }
182
183 return $graphicalFunctionsObject;
184 }
185 }