TYPO3  7.6
ImageService.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Extbase\Service;
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 
50  public function injectEnvironmentService(\TYPO3\CMS\Extbase\Service\EnvironmentService $environmentService)
51  {
52  $this->environmentService = $environmentService;
53  }
54 
63  public function applyProcessingInstructions($image, $processingInstructions)
64  {
65  if (is_callable(array($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(array($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  throw new \UnexpectedValueException('Supplied file object type ' . get_class($image) . ' must be File or FileReference.', 1382687163);
134  }
135 
136  return $image;
137  }
138 
146  protected function getImageFromSourceString($src, $treatIdAsReference)
147  {
148  if ($this->environmentService->isEnvironmentInBackendMode() && substr($src, 0, 3) === '../') {
149  $src = substr($src, 3);
150  }
152  if ($treatIdAsReference) {
153  $image = $this->resourceFactory->getFileReferenceObject($src);
154  } else {
155  $image = $this->resourceFactory->getFileObject($src);
156  }
157  } else {
158  // We have a combined identifier or legacy (storage 0) path
159  $image = $this->resourceFactory->retrieveFileOrFolderObject($src);
160  }
161  return $image;
162  }
163 
171  protected function setCompatibilityValues(ProcessedFile $processedImage)
172  {
173  if ($this->environmentService->isEnvironmentInFrontendMode()) {
174  $imageInfo = $this->getCompatibilityImageResourceValues($processedImage);
175  $GLOBALS['TSFE']->lastImageInfo = $imageInfo;
176  $GLOBALS['TSFE']->imagesOnPage[] = $imageInfo[3];
177  }
178  }
179 
188  protected function getCompatibilityImageResourceValues(ProcessedFile $processedImage)
189  {
190  $hash = $processedImage->calculateChecksum();
191  if (isset($GLOBALS['TSFE']->tmpl->fileCache[$hash])) {
192  $compatibilityImageResourceValues = $GLOBALS['TSFE']->tmpl->fileCache[$hash];
193  } else {
194  $compatibilityImageResourceValues = array(
195  0 => $processedImage->getProperty('width'),
196  1 => $processedImage->getProperty('height'),
197  2 => $processedImage->getExtension(),
198  3 => $processedImage->getPublicUrl(),
199  'origFile' => $processedImage->getOriginalFile()->getPublicUrl(),
200  'origFile_mtime' => $processedImage->getOriginalFile()->getModificationTime(),
201  // This is needed by \TYPO3\CMS\Frontend\Imaging\GifBuilder,
202  // in order for the setup-array to create a unique filename hash.
203  'originalFile' => $processedImage->getOriginalFile(),
204  'processedFile' => $processedImage,
205  'fileCacheHash' => $hash
206  );
207  }
208  return $compatibilityImageResourceValues;
209  }
210 }