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