TYPO3 CMS  TYPO3_8-7
ImageService.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 
24 
29 {
33  protected $resourceFactory;
34 
39 
43  public function injectResourceFactory(\TYPO3\CMS\Core\Resource\ResourceFactory $resourceFactory)
44  {
45  $this->resourceFactory = $resourceFactory;
46  }
47 
52  {
53  $this->environmentService = $environmentService;
54  }
55 
64  public function applyProcessingInstructions($image, $processingInstructions)
65  {
66  if (is_callable([$image, 'getOriginalFile'])) {
67  // Get the original file from the file reference
68  $image = $image->getOriginalFile();
69  }
70 
71  $processedImage = $image->process(ProcessedFile::CONTEXT_IMAGECROPSCALEMASK, $processingInstructions);
72  $this->setCompatibilityValues($processedImage);
73 
74  return $processedImage;
75  }
76 
85  public function getImageUri(FileInterface $image, $absolute = false)
86  {
87  $imageUrl = $image->getPublicUrl();
88  $parsedUrl = parse_url($imageUrl);
89  // no prefix in case of an already fully qualified URL
90  if (isset($parsedUrl['host'])) {
91  $uriPrefix = '';
92  } elseif ($this->environmentService->isEnvironmentInFrontendMode()) {
93  $uriPrefix = $GLOBALS['TSFE']->absRefPrefix;
94  } else {
95  $uriPrefix = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
96  }
97 
98  if ($absolute) {
99  // If full URL has no scheme we add the same scheme as used by the site
100  // so we have an absolute URL also usable outside of browser scope (e.g. in an email message)
101  if (isset($parsedUrl['host']) && !isset($parsedUrl['scheme'])) {
102  $uriPrefix = (GeneralUtility::getIndpEnv('TYPO3_SSL') ? 'https:' : 'http:') . $uriPrefix;
103  }
104  return GeneralUtility::locationHeaderUrl($uriPrefix . $imageUrl);
105  }
106  return $uriPrefix . $imageUrl;
107  }
108 
123  public function getImage($src, $image, $treatIdAsReference)
124  {
125  if (is_null($image)) {
126  $image = $this->getImageFromSourceString($src, $treatIdAsReference);
127  } elseif (is_callable([$image, 'getOriginalResource'])) {
128  // We have a domain model, so we need to fetch the FAL resource object from there
129  $image = $image->getOriginalResource();
130  }
131 
132  if (!($image instanceof File || $image instanceof FileReference)) {
133  $class = is_object($image) ? get_class($image) : 'null';
134  throw new \UnexpectedValueException('Supplied file object type ' . $class . ' for ' . $src . ' must be File or FileReference.', 1382687163);
135  }
136 
137  return $image;
138  }
139 
147  protected function getImageFromSourceString($src, $treatIdAsReference)
148  {
149  if ($this->environmentService->isEnvironmentInBackendMode() && substr($src, 0, 3) === '../') {
150  $src = substr($src, 3);
151  }
153  if ($treatIdAsReference) {
154  $image = $this->resourceFactory->getFileReferenceObject($src);
155  } else {
156  $image = $this->resourceFactory->getFileObject($src);
157  }
158  } elseif (strpos($src, 't3://file') === 0) {
159  // We have a t3://file link to a file in FAL
160  $linkService = GeneralUtility::makeInstance(LinkService::class);
161  $data = $linkService->resolveByStringRepresentation($src);
162  $image = $data['file'];
163  } else {
164  // We have a combined identifier or legacy (storage 0) path
165  $image = $this->resourceFactory->retrieveFileOrFolderObject($src);
166  }
167  return $image;
168  }
169 
176  protected function setCompatibilityValues(ProcessedFile $processedImage)
177  {
178  if ($this->environmentService->isEnvironmentInFrontendMode()) {
179  $imageInfo = $this->getCompatibilityImageResourceValues($processedImage);
180  $GLOBALS['TSFE']->lastImageInfo = $imageInfo;
181  $GLOBALS['TSFE']->imagesOnPage[] = $imageInfo[3];
182  }
183  }
184 
193  protected function getCompatibilityImageResourceValues(ProcessedFile $processedImage)
194  {
195  $hash = $processedImage->calculateChecksum();
196  if (isset($GLOBALS['TSFE']->tmpl->fileCache[$hash])) {
197  $compatibilityImageResourceValues = $GLOBALS['TSFE']->tmpl->fileCache[$hash];
198  } else {
199  $compatibilityImageResourceValues = [
200  0 => $processedImage->getProperty('width'),
201  1 => $processedImage->getProperty('height'),
202  2 => $processedImage->getExtension(),
203  3 => $processedImage->getPublicUrl(),
204  'origFile' => $processedImage->getOriginalFile()->getPublicUrl(),
205  'origFile_mtime' => $processedImage->getOriginalFile()->getModificationTime(),
206  // This is needed by \TYPO3\CMS\Frontend\Imaging\GifBuilder,
207  // in order for the setup-array to create a unique filename hash.
208  'originalFile' => $processedImage->getOriginalFile(),
209  'processedFile' => $processedImage,
210  'fileCacheHash' => $hash
211  ];
212  }
213  return $compatibilityImageResourceValues;
214  }
215 }
getCompatibilityImageResourceValues(ProcessedFile $processedImage)
getImageUri(FileInterface $image, $absolute=false)
injectResourceFactory(\TYPO3\CMS\Core\Resource\ResourceFactory $resourceFactory)
static makeInstance($className,... $constructorArguments)
getPublicUrl($relativeToCurrentScript=false)
injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService)
applyProcessingInstructions($image, $processingInstructions)
setCompatibilityValues(ProcessedFile $processedImage)
getImage($src, $image, $treatIdAsReference)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
getImageFromSourceString($src, $treatIdAsReference)
getPublicUrl($relativeToCurrentScript=false)