[BUGFIX] Do not overwrite imagecaption in content adapter
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Service / MagicImageService.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Service;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012 Stanislas Rolland <stanislas.rolland@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Magic image service
31 *
32 * @author Stanislas Rolland <stanislas.rolland@typo3.org>
33 */
34 class MagicImageService {
35
36 /**
37 * @var \TYPO3\CMS\Core\Imaging\GraphicalFunctions
38 */
39 protected $imageObject;
40
41 /**
42 * Internal function to retrieve the target magic image folder
43 *
44 * @param string $targetFolderCombinedIdentifier
45 * @return \TYPO3\CMS\Core\Resource\Folder
46 */
47 protected function getMagicFolder($targetFolderCombinedIdentifier) {
48 $fileFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
49 // @todo Proper exception handling is missing here
50 if ($targetFolderCombinedIdentifier) {
51 $magicFolder = $fileFactory->getFolderObjectFromCombinedIdentifier($targetFolderCombinedIdentifier);
52 }
53 if (empty($magicFolder) || !$magicFolder instanceof \TYPO3\CMS\Core\Resource\Folder) {
54 $magicFolder = $fileFactory->getFolderObjectFromCombinedIdentifier($GLOBALS['TYPO3_CONF_VARS']['BE']['RTE_imageStorageDir']);
55 }
56 return $magicFolder;
57 }
58
59 /**
60 * Internal function to retrieve the image object,
61 * if it does not exist, an instance will be created
62 *
63 * @return \TYPO3\CMS\Core\Imaging\GraphicalFunctions
64 */
65 protected function getImageObject() {
66 if ($this->imageObject === NULL) {
67 /** @var $this->imageObject \TYPO3\CMS\Core\Imaging\GraphicalFunctions */
68 $this->imageObject = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Imaging\\GraphicalFunctions');
69 $this->imageObject->init();
70 $this->imageObject->mayScaleUp = 0;
71 $this->imageObject->tempPath = PATH_site . $this->imageObject->tempPath;
72 }
73 return $this->imageObject;
74 }
75
76 /**
77 * Creates a magic image
78 *
79 * @param \TYPO3\CMS\Core\Resource\FileInterface $imageFileObject: the original image file
80 * @param array $fileConfiguration (width, height, maxW, maxH)
81 * @param string $targetFolderCombinedIdentifier: target folder combined identifier
82 * @return \TYPO3\CMS\Core\Resource\FileInterface
83 */
84 public function createMagicImage(\TYPO3\CMS\Core\Resource\FileInterface $imageFileObject, array $fileConfiguration, $targetFolderCombinedIdentifier) {
85 $magicImage = NULL;
86 // Get file for processing
87 $imageFilePath = $imageFileObject->getForLocalProcessing(TRUE);
88 // Process dimensions
89 $maxWidth = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($fileConfiguration['width'], 0, $fileConfiguration['maxW']);
90 $maxHeight = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($fileConfiguration['height'], 0, $fileConfiguration['maxH']);
91 if (!$maxWidth) {
92 $maxWidth = $fileConfiguration['maxW'];
93 }
94 if (!$maxHeight) {
95 $maxHeight = $fileConfiguration['maxH'];
96 }
97 // Create the magic image
98 $magicImageInfo = $this->getImageObject()->imageMagickConvert($imageFilePath, 'WEB', $maxWidth . 'm', $maxHeight . 'm');
99 if ($magicImageInfo[3]) {
100 $targetFileName = 'RTEmagicC_' . pathInfo($imageFileObject->getName(), PATHINFO_FILENAME) . '.' . pathinfo($magicImageInfo[3], PATHINFO_EXTENSION);
101 $magicFolder = $this->getMagicFolder($targetFolderCombinedIdentifier);
102 if ($magicFolder instanceof \TYPO3\CMS\Core\Resource\Folder) {
103 $magicImage = $magicFolder->addFile($magicImageInfo[3], $targetFileName, 'changeName');
104 }
105 }
106 return $magicImage;
107 }
108
109 }
110
111
112 ?>