[BUGFIX] RTE maxWidth for magic images
[Packages/TYPO3.CMS.git] / typo3 / sysext / rtehtmlarea / Classes / Controller / ImageRenderingController.php
1 <?php
2 namespace TYPO3\CMS\Rtehtmlarea\Controller;
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\Utility\GeneralUtility;
18 use TYPO3\CMS\Core\Resource;
19
20 /**
21 * Render the image attributes and reconstruct magic images, if necessary (and possible)
22 */
23 class ImageRenderingController extends \TYPO3\CMS\Frontend\Plugin\AbstractPlugin {
24
25 /**
26 * Same as class name
27 *
28 * @var string
29 */
30 public $prefixId = 'ImageRenderingController';
31
32 /**
33 * Path to this script relative to the extension dir
34 *
35 * @var string
36 */
37 public $scriptRelPath = 'Classes/Controller/ImageRenderingController.php';
38
39 /**
40 * The extension key
41 *
42 * @var string
43 */
44 public $extKey = 'rtehtmlarea';
45
46 /**
47 * Configuration
48 *
49 * @var array
50 */
51 public $conf = array();
52
53 /**
54 * cObj object
55 *
56 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
57 */
58 public $cObj;
59
60 /**
61 * Returns a processed image to be displayed on the Frontend.
62 *
63 * @param string $content Content input (not used).
64 * @param array $conf TypoScript configuration
65 * @return string HTML output
66 */
67 public function renderImageAttributes($content = '', $conf) {
68
69 $imageAttributes = $this->getImageAttributes();
70
71 // It is pretty rare to be in presence of an external image as the default behaviour
72 // of the RTE is to download the external image and create a local image.
73 // However, it may happen if the RTE has the flag "disable"
74 if (!$this->isExternalImage()) {
75 $fileUid = (int)$imageAttributes['data-htmlarea-file-uid'];
76 if ($fileUid) {
77 try {
78 $file = Resource\ResourceFactory::getInstance()->getFileObject($fileUid);
79 if ($imageAttributes['src'] !== $file->getPublicUrl()) {
80 // Source file is a processed image
81 $imageConfiguration = array(
82 'width' => (int)$imageAttributes['width'],
83 'height' => (int)$imageAttributes['height']
84 );
85 $processedFile = $this->getMagicImageService()->createMagicImage($file, $imageConfiguration);
86 $additionalAttributes = array(
87 'src' => $processedFile->getPublicUrl(),
88 'title' => $imageAttributes['title'] ?: $file->getProperty('title'),
89 'alt' => $imageAttributes['alt'] ?: $file->getProperty('alternative'),
90 'width' => $processedFile->getProperty('width'),
91 'height' => $processedFile->getProperty('height'),
92 );
93 $imageAttributes = array_merge($imageAttributes, $additionalAttributes);
94 }
95 } catch (Resource\Exception\FileDoesNotExistException $fileDoesNotExistException) {
96 // Log the fact the file could not be retrieved.
97 $message = sprintf('I could not find file with uid "%s"', $fileUid);
98 $this->getLogger()->error($message);
99 }
100 }
101 }
102 return '<img ' . GeneralUtility::implodeAttributes($imageAttributes, TRUE, TRUE) . ' />';
103 }
104
105 /**
106 * Returns a sanitizes array of attributes out of $this->cObj
107 *
108 * @return array
109 */
110 protected function getImageAttributes() {
111 return $this->cObj->parameters;
112 }
113
114 /**
115 * Instantiates and prepares the Magic Image service.
116 *
117 * @return \TYPO3\CMS\Core\Resource\Service\MagicImageService
118 */
119 protected function getMagicImageService() {
120
121 /** @var $magicImageService Resource\Service\MagicImageService */
122 $magicImageService = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\Service\\MagicImageService');
123
124 // Get RTE configuration
125 $pageTSConfig = $this->getFrontendObject()->getPagesTSconfig();
126 if (is_array($pageTSConfig) && is_array($pageTSConfig['RTE.']['default.'])) {
127 $magicImageService->setMagicImageMaximumDimensions($pageTSConfig['RTE.']['default.']);
128 }
129
130 return $magicImageService;
131 }
132
133 /**
134 * Tells whether the image URL is found to be "external".
135 *
136 * @return bool
137 */
138 protected function isExternalImage() {
139 $srcAbsoluteUrl = $this->cObj->parameters['src'];
140 return strtolower(substr($srcAbsoluteUrl, 0, 4)) === 'http' || substr($srcAbsoluteUrl, 0, 2) === '//';
141 }
142
143 /**
144 * @return \TYPO3\CMS\Core\Log\Logger
145 */
146 protected function getLogger() {
147
148 /** @var $logManager \TYPO3\CMS\Core\Log\LogManager */
149 $logManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogManager');
150
151 return $logManager->getLogger(get_class($this));
152 }
153
154 /**
155 * Returns an instance of the Frontend object.
156 *
157 * @return \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
158 */
159 protected function getFrontendObject() {
160 return $GLOBALS['TSFE'];
161 }
162 }