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