TYPO3 CMS  TYPO3_7-6
ImageRenderingController.php
Go to the documentation of this file.
1 <?php
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 
19 
24 {
30  public $prefixId = 'ImageRenderingController';
31 
37  public $scriptRelPath = 'Classes/Controller/ImageRenderingController.php';
38 
44  public $extKey = 'rtehtmlarea';
45 
51  public $conf = [];
52 
58  public $cObj;
59 
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 = [
82  'width' => (int)$imageAttributes['width'],
83  'height' => (int)$imageAttributes['height']
84  ];
85  $processedFile = $this->getMagicImageService()->createMagicImage($file, $imageConfiguration);
86  $additionalAttributes = [
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 
110  protected function getImageAttributes()
111  {
112  return $this->cObj->parameters;
113  }
114 
120  protected function getMagicImageService()
121  {
122 
124  $magicImageService = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Service\MagicImageService::class);
125 
126  // Get RTE configuration
127  $pageTSConfig = $this->frontendController->getPagesTSconfig();
128  if (is_array($pageTSConfig) && is_array($pageTSConfig['RTE.']['default.'])) {
129  $magicImageService->setMagicImageMaximumDimensions($pageTSConfig['RTE.']['default.']);
130  }
131 
132  return $magicImageService;
133  }
134 
140  protected function isExternalImage()
141  {
142  $srcAbsoluteUrl = $this->cObj->parameters['src'];
143  return strtolower(substr($srcAbsoluteUrl, 0, 4)) === 'http' || substr($srcAbsoluteUrl, 0, 2) === '//';
144  }
145 
149  protected function getLogger()
150  {
151 
153  $logManager = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\LogManager::class);
154 
155  return $logManager->getLogger(get_class($this));
156  }
157 }
static implodeAttributes(array $arr, $xhtmlSafe=false, $dontOmitBlankAttribs=false)