8a059466c39119e3c64798f2b04c7e0fa8749f98
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Processing / LocalCropScaleMaskHelper.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 * Helper class to locally perform a crop/scale/mask task with the TYPO3 image processing classes.
21 */
22 class LocalCropScaleMaskHelper {
23 /**
24 * @var LocalImageProcessor
25 */
26 protected $processor;
27
28
29 /**
30 * @param LocalImageProcessor $processor
31 */
32 public function __construct(LocalImageProcessor $processor) {
33 $this->processor = $processor;
34 }
35
36 /**
37 * This method actually does the processing of files locally
38 *
39 * Takes the original file (for remote storages this will be fetched from the remote server),
40 * does the IM magic on the local server by creating a temporary typo3temp/ file,
41 * copies the typo3temp/ file to the processing folder of the target storage and
42 * removes the typo3temp/ file.
43 *
44 * @param TaskInterface $task
45 * @return array
46 */
47 public function process(TaskInterface $task) {
48 $result = NULL;
49 $targetFile = $task->getTargetFile();
50 $sourceFile = $task->getSourceFile();
51
52 $originalFileName = $sourceFile->getForLocalProcessing(FALSE);
53 /** @var $gifBuilder \TYPO3\CMS\Frontend\Imaging\GifBuilder */
54 $gifBuilder = Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Imaging\\GifBuilder');
55 $gifBuilder->init();
56 $gifBuilder->absPrefix = PATH_site;
57
58 $configuration = $targetFile->getProcessingConfiguration();
59 $configuration['additionalParameters'] = $this->modifyImageMagickStripProfileParameters($configuration['additionalParameters'], $configuration);
60
61 if (empty($configuration['fileExtension'])) {
62 $configuration['fileExtension'] = $task->getTargetFileExtension();
63 }
64
65 $options = $this->getConfigurationForImageCropScaleMask($targetFile, $gifBuilder);
66
67 // Normal situation (no masking)
68 if (!(is_array($configuration['maskImages']) && $GLOBALS['TYPO3_CONF_VARS']['GFX']['im'])) {
69 // the result info is an array with 0=width,1=height,2=extension,3=filename
70 $result = $gifBuilder->imageMagickConvert(
71 $originalFileName,
72 $configuration['fileExtension'],
73 $configuration['width'],
74 $configuration['height'],
75 $configuration['additionalParameters'],
76 $configuration['frame'],
77 $options
78 );
79 } else {
80 $targetFileName = $this->getFilenameForImageCropScaleMask($task);
81 $temporaryFileName = $gifBuilder->tempPath . $targetFileName;
82 $maskImage = $configuration['maskImages']['maskImage'];
83 $maskBackgroundImage = $configuration['maskImages']['backgroundImage'];
84 if ($maskImage instanceof Resource\FileInterface && $maskBackgroundImage instanceof Resource\FileInterface) {
85 $temporaryExtension = 'png';
86 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_mask_temp_ext_gif']) {
87 // If ImageMagick version 5+
88 $temporaryExtension = $gifBuilder->gifExtension;
89 }
90 $tempFileInfo = $gifBuilder->imageMagickConvert(
91 $originalFileName,
92 $temporaryExtension,
93 $configuration['width'],
94 $configuration['height'],
95 $configuration['additionalParameters'],
96 $configuration['frame'],
97 $options
98 );
99 if (is_array($tempFileInfo)) {
100 $maskBottomImage = $configuration['maskImages']['maskBottomImage'];
101 if ($maskBottomImage instanceof $maskBottomImage) {
102 $maskBottomImageMask = $configuration['maskImages']['maskBottomImageMask'];
103 } else {
104 $maskBottomImageMask = NULL;
105 }
106
107 // Scaling: ****
108 $tempScale = array();
109 $command = '-geometry ' . $tempFileInfo[0] . 'x' . $tempFileInfo[1] . '!';
110 $command = $this->modifyImageMagickStripProfileParameters($command, $configuration);
111 $tmpStr = $gifBuilder->randomName();
112 // m_mask
113 $tempScale['m_mask'] = $tmpStr . '_mask.' . $temporaryExtension;
114 $gifBuilder->imageMagickExec($maskImage->getForLocalProcessing(TRUE), $tempScale['m_mask'], $command);
115 // m_bgImg
116 $tempScale['m_bgImg'] = $tmpStr . '_bgImg.miff';
117 $gifBuilder->imageMagickExec($maskBackgroundImage->getForLocalProcessing(), $tempScale['m_bgImg'], $command);
118 // m_bottomImg / m_bottomImg_mask
119 if ($maskBottomImage instanceof Resource\FileInterface && $maskBottomImageMask instanceof Resource\FileInterface) {
120 $tempScale['m_bottomImg'] = $tmpStr . '_bottomImg.' . $temporaryExtension;
121 $gifBuilder->imageMagickExec($maskBottomImage->getForLocalProcessing(), $tempScale['m_bottomImg'], $command);
122 $tempScale['m_bottomImg_mask'] = ($tmpStr . '_bottomImg_mask.') . $temporaryExtension;
123 $gifBuilder->imageMagickExec($maskBottomImageMask->getForLocalProcessing(), $tempScale['m_bottomImg_mask'], $command);
124 // BEGIN combining:
125 // The image onto the background
126 $gifBuilder->combineExec($tempScale['m_bgImg'], $tempScale['m_bottomImg'], $tempScale['m_bottomImg_mask'], $tempScale['m_bgImg']);
127 }
128 // The image onto the background
129 $gifBuilder->combineExec($tempScale['m_bgImg'], $tempFileInfo[3], $tempScale['m_mask'], $temporaryFileName);
130 // Unlink the temp-images...
131 foreach ($tempScale as $tempFile) {
132 if (@is_file($tempFile)) {
133 unlink($tempFile);
134 }
135 }
136 }
137 $result = $tempFileInfo;
138 }
139 }
140 // check if the processing really generated a new file
141 if ($result !== NULL) {
142 if ($result[3] !== $originalFileName) {
143 $result = array(
144 'width' => $result[0],
145 'height' => $result[1],
146 'filePath' => $result[3],
147 );
148 } else {
149 // No file was generated
150 $result = NULL;
151 }
152 }
153
154 return $result;
155 }
156
157 /**
158 * @param Resource\ProcessedFile $processedFile
159 * @param \TYPO3\CMS\Frontend\Imaging\GifBuilder $gifBuilder
160 *
161 * @return array
162 */
163 protected function getConfigurationForImageCropScaleMask(Resource\ProcessedFile $processedFile, \TYPO3\CMS\Frontend\Imaging\GifBuilder $gifBuilder) {
164 $configuration = $processedFile->getProcessingConfiguration();
165
166 if ($configuration['useSample']) {
167 $gifBuilder->scalecmd = '-sample';
168 }
169 $options = array();
170 if ($configuration['maxWidth']) {
171 $options['maxW'] = $configuration['maxWidth'];
172 }
173 if ($configuration['maxHeight']) {
174 $options['maxH'] = $configuration['maxHeight'];
175 }
176 if ($configuration['minWidth']) {
177 $options['minW'] = $configuration['minWidth'];
178 }
179 if ($configuration['minHeight']) {
180 $options['minH'] = $configuration['minHeight'];
181 }
182
183 $options['noScale'] = $configuration['noScale'];
184
185 return $options;
186 }
187
188 /**
189 * Returns the filename for a cropped/scaled/masked file.
190 *
191 * @param TaskInterface $task
192 * @return string
193 */
194 protected function getFilenameForImageCropScaleMask(TaskInterface $task) {
195
196 $configuration = $task->getTargetFile()->getProcessingConfiguration();
197 $targetFileExtension = $task->getSourceFile()->getExtension();
198 $processedFileExtension = $GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png'] ? 'png' : 'gif';
199 if (is_array($configuration['maskImages']) && $GLOBALS['TYPO3_CONF_VARS']['GFX']['im'] && $task->getSourceFile()->getExtension() != $processedFileExtension) {
200 $targetFileExtension = 'jpg';
201 } elseif ($configuration['fileExtension']) {
202 $targetFileExtension = $configuration['fileExtension'];
203 }
204
205 return $task->getTargetFile()->generateProcessedFileNameWithoutExtension() . '.' . ltrim(trim($targetFileExtension), '.');
206 }
207
208 /**
209 * Modifies the parameters for ImageMagick for stripping of profile information.
210 *
211 * @param string $parameters The parameters to be modified (if required)
212 * @param array $configuration The TypoScript configuration of [IMAGE].file
213 * @return string
214 */
215 protected function modifyImageMagickStripProfileParameters($parameters, array $configuration) {
216 // Strips profile information of image to save some space:
217 if (isset($configuration['stripProfile'])) {
218 if ($configuration['stripProfile']) {
219 $parameters = $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_stripProfileCommand'] . $parameters;
220 } else {
221 $parameters .= '###SkipStripProfile###';
222 }
223 }
224 return $parameters;
225 }
226 }