TYPO3 CMS  TYPO3_7-6
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 
23 
28 {
32  protected $resourceFactory;
33 
38 
42  public function injectResourceFactory(\TYPO3\CMS\Core\Resource\ResourceFactory $resourceFactory)
43  {
44  $this->resourceFactory = $resourceFactory;
45  }
46 
51  {
52  $this->environmentService = $environmentService;
53  }
54 
63  public function applyProcessingInstructions($image, $processingInstructions)
64  {
65  if (is_callable([$image, 'getOriginalFile'])) {
66  // Get the original file from the file reference
67  $image = $image->getOriginalFile();
68  }
69 
70  $processedImage = $image->process(ProcessedFile::CONTEXT_IMAGECROPSCALEMASK, $processingInstructions);
71  $this->setCompatibilityValues($processedImage);
72 
73  return $processedImage;
74  }
75 
84  public function getImageUri(FileInterface $image, $absolute = false)
85  {
86  $imageUrl = $image->getPublicUrl();
87  $parsedUrl = parse_url($imageUrl);
88  // no prefix in case of an already fully qualified URL
89  if (isset($parsedUrl['host'])) {
90  $uriPrefix = '';
91  } elseif ($this->environmentService->isEnvironmentInFrontendMode()) {
92  $uriPrefix = $GLOBALS['TSFE']->absRefPrefix;
93  } else {
94  $uriPrefix = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
95  }
96 
97  if ($absolute) {
98  // If full URL has no scheme we add the same scheme as used by the site
99  // so we have an absolute URL also usable outside of browser scope (e.g. in an email message)
100  if (isset($parsedUrl['host']) && !isset($parsedUrl['scheme'])) {
101  $uriPrefix = (GeneralUtility::getIndpEnv('TYPO3_SSL') ? 'https:' : 'http:') . $uriPrefix;
102  }
103  return GeneralUtility::locationHeaderUrl($uriPrefix . $imageUrl);
104  } else {
105  return $uriPrefix . $imageUrl;
106  }
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  } else {
159  // We have a combined identifier or legacy (storage 0) path
160  $image = $this->resourceFactory->retrieveFileOrFolderObject($src);
161  }
162  return $image;
163  }
164 
172  protected function setCompatibilityValues(ProcessedFile $processedImage)
173  {
174  if ($this->environmentService->isEnvironmentInFrontendMode()) {
175  $imageInfo = $this->getCompatibilityImageResourceValues($processedImage);
176  $GLOBALS['TSFE']->lastImageInfo = $imageInfo;
177  $GLOBALS['TSFE']->imagesOnPage[] = $imageInfo[3];
178  }
179  }
180 
189  protected function getCompatibilityImageResourceValues(ProcessedFile $processedImage)
190  {
191  $hash = $processedImage->calculateChecksum();
192  if (isset($GLOBALS['TSFE']->tmpl->fileCache[$hash])) {
193  $compatibilityImageResourceValues = $GLOBALS['TSFE']->tmpl->fileCache[$hash];
194  } else {
195  $compatibilityImageResourceValues = [
196  0 => $processedImage->getProperty('width'),
197  1 => $processedImage->getProperty('height'),
198  2 => $processedImage->getExtension(),
199  3 => $processedImage->getPublicUrl(),
200  'origFile' => $processedImage->getOriginalFile()->getPublicUrl(),
201  'origFile_mtime' => $processedImage->getOriginalFile()->getModificationTime(),
202  // This is needed by \TYPO3\CMS\Frontend\Imaging\GifBuilder,
203  // in order for the setup-array to create a unique filename hash.
204  'originalFile' => $processedImage->getOriginalFile(),
205  'processedFile' => $processedImage,
206  'fileCacheHash' => $hash
207  ];
208  }
209  return $compatibilityImageResourceValues;
210  }
211 }
getCompatibilityImageResourceValues(ProcessedFile $processedImage)
getImageUri(FileInterface $image, $absolute=false)
injectResourceFactory(\TYPO3\CMS\Core\Resource\ResourceFactory $resourceFactory)
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)