‪TYPO3CMS  10.4
ImageService.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
30 
35 {
39  protected ‪$resourceFactory;
40 
44  protected ‪$environmentService;
45 
53  {
54  $this->environmentService = ‪$environmentService ?? GeneralUtility::makeInstance(EnvironmentService::class);
55  $this->resourceFactory = ‪$resourceFactory ?? GeneralUtility::makeInstance(ResourceFactory::class);
56  }
57 
65  public function ‪applyProcessingInstructions($image, array $processingInstructions): ‪ProcessedFile
66  {
67  /*
68  * todo: this method should be split to be able to have a proper method signature.
69  * todo: actually, this method only really works with objects of type \TYPO3\CMS\Core\Resource\File, as this
70  * todo: is the only implementation that supports the support method.
71  */
72  if (is_callable([$image, 'getOriginalFile'])) {
73  // Get the original file from the file reference
74  $image = $image->‪getOriginalFile();
75  }
76 
77  $processedImage = $image->‪process(‪ProcessedFile::CONTEXT_IMAGECROPSCALEMASK, $processingInstructions);
78  $this->‪setCompatibilityValues($processedImage);
79 
80  return $processedImage;
81  }
82 
90  public function ‪getImageUri(‪FileInterface $image, bool $absolute = false): string
91  {
92  $imageUrl = $image->‪getPublicUrl();
93  if ($imageUrl === null) {
94  // Image is missing probably, return an empty string instead of parsing
95  return '';
96  }
97 
98  $parsedUrl = parse_url($imageUrl);
99  // no prefix in case of an already fully qualified URL
100  if (isset($parsedUrl['host'])) {
101  $uriPrefix = '';
102  } elseif ($this->environmentService->isEnvironmentInFrontendMode()) {
103  $uriPrefix = ‪$GLOBALS['TSFE']->absRefPrefix;
104  } else {
105  $uriPrefix = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
106  }
107 
108  if ($absolute) {
109  // If full URL has no scheme we add the same scheme as used by the site
110  // so we have an absolute URL also usable outside of browser scope (e.g. in an email message)
111  if (isset($parsedUrl['host']) && !isset($parsedUrl['scheme'])) {
112  $uriPrefix = (GeneralUtility::getIndpEnv('TYPO3_SSL') ? 'https:' : 'http:') . $uriPrefix;
113  }
114  return GeneralUtility::locationHeaderUrl($uriPrefix . $imageUrl);
115  }
116  return $uriPrefix . $imageUrl;
117  }
118 
133  public function ‪getImage(string $src, $image, bool $treatIdAsReference): ‪FileInterface
134  {
135  if ($image instanceof ‪File || $image instanceof ‪FileReference) {
136  // We already received a valid file and therefore just return it
137  return $image;
138  }
139 
140  if (is_callable([$image, 'getOriginalResource'])) {
141  // We have a domain model, so we need to fetch the FAL resource object from there
142  $originalResource = $image->getOriginalResource();
143  if (!($originalResource instanceof ‪File || $originalResource instanceof ‪FileReference)) {
144  throw new \UnexpectedValueException('No original resource could be resolved for supplied file ' . get_class($image), 1625838481);
145  }
146  return $originalResource;
147  }
148 
149  if ($image !== null) {
150  // Some value is given for $image, but it's not a valid type
151  throw new \UnexpectedValueException(
152  'Supplied file must be File or FileReference, ' . (($type = gettype($image)) === 'object' ? get_class($image) : $type) . ' given.',
153  1625585157
154  );
155  }
156 
157  // Since image is not given, try to resolve an image from the source string
158  $resolvedImage = $this->‪getImageFromSourceString($src, $treatIdAsReference);
159 
160  if ($resolvedImage instanceof ‪File || $resolvedImage instanceof ‪FileReference) {
161  return $resolvedImage;
162  }
163 
164  if ($resolvedImage === null) {
165  // No image could be resolved using the given source string
166  throw new \UnexpectedValueException('Supplied ' . $src . ' could not be resolved to a File or FileReference.', 1625585158);
167  }
168 
169  // A FileInterface was found, however only File and FileReference are valid
170  throw new \UnexpectedValueException(
171  'Resolved file object type ' . get_class($resolvedImage) . ' for ' . $src . ' must be File or FileReference.',
172  1382687163
173  );
174  }
175 
183  protected function ‪getImageFromSourceString(string $src, bool $treatIdAsReference): ?‪FileInterface
184  {
185  if ($this->environmentService->isEnvironmentInBackendMode() && strpos($src, '../') === 0) {
186  $src = substr($src, 3);
187  }
189  if ($treatIdAsReference) {
190  $image = $this->resourceFactory->getFileReferenceObject($src);
191  } else {
192  $image = $this->resourceFactory->getFileObject($src);
193  }
194  } elseif (strpos($src, 't3://file') === 0) {
195  // We have a t3://file link to a file in FAL
196  $linkService = GeneralUtility::makeInstance(LinkService::class);
197  $data = $linkService->resolveByStringRepresentation($src);
198  $image = $data['file'];
199  } else {
200  // We have a combined identifier or legacy (storage 0) path
201  $image = $this->resourceFactory->retrieveFileOrFolderObject($src);
202  }
203 
204  // Check the resolved image as this could also be a FolderInterface
205  return $image instanceof ‪FileInterface ? $image : null;
206  }
207 
214  protected function ‪setCompatibilityValues(‪ProcessedFile $processedImage): void
215  {
216  $publicUrl = $processedImage->‪getPublicUrl();
217  if ($publicUrl === null) {
218  return;
219  }
220  $imageInfoValues = $this->‪getCompatibilityImageResourceValues($processedImage);
221  if (
222  $this->environmentService->isEnvironmentInFrontendMode()
223  && is_object(‪$GLOBALS['TSFE'])
224  ) {
225  // This is needed by \TYPO3\CMS\Frontend\Imaging\GifBuilder,
226  // but was never needed to be set in lastImageInfo.
227  // We set it for BC here anyway, as this TSFE property is deprecated anyway.
228  $imageInfoValues['originalFile'] = $processedImage->‪getOriginalFile();
229  $imageInfoValues['processedFile'] = $processedImage;
230  ‪$GLOBALS['TSFE']->lastImageInfo = $imageInfoValues;
231  ‪$GLOBALS['TSFE']->imagesOnPage[] = $publicUrl;
232  }
233  GeneralUtility::makeInstance(AssetCollector::class)->addMedia(
234  $publicUrl,
235  $imageInfoValues
236  );
237  }
238 
247  protected function ‪getCompatibilityImageResourceValues(‪ProcessedFile $processedImage): array
248  {
249  $originalFile = $processedImage->‪getOriginalFile();
250  return [
251  0 => $processedImage->‪getProperty('width'),
252  1 => $processedImage->‪getProperty('height'),
253  2 => $processedImage->‪getExtension(),
254  3 => $processedImage->‪getPublicUrl(),
255  'origFile' => $originalFile->getPublicUrl(),
256  'origFile_mtime' => $originalFile->getModificationTime(),
257  ];
258  }
259 }
‪TYPO3\CMS\Core\Resource\ProcessedFile\getOriginalFile
‪File getOriginalFile()
Definition: ProcessedFile.php:303
‪TYPO3\CMS\Core\Page\AssetCollector
Definition: AssetCollector.php:44
‪TYPO3\CMS\Extbase\Service\ImageService\getImageFromSourceString
‪FileInterface null getImageFromSourceString(string $src, bool $treatIdAsReference)
Definition: ImageService.php:181
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Extbase\Service\ImageService\$resourceFactory
‪ResourceFactory $resourceFactory
Definition: ImageService.php:38
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Core\Resource\ProcessedFile\CONTEXT_IMAGECROPSCALEMASK
‪const CONTEXT_IMAGECROPSCALEMASK
Definition: ProcessedFile.php:58
‪TYPO3\CMS\Extbase\Service\ImageService\getImageUri
‪string getImageUri(FileInterface $image, bool $absolute=false)
Definition: ImageService.php:88
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:33
‪TYPO3\CMS\Extbase\Service
Definition: CacheService.php:18
‪TYPO3\CMS\Core\Resource\File\process
‪ProcessedFile process($taskType, array $configuration)
Definition: File.php:266
‪TYPO3\CMS\Extbase\Service\ImageService\__construct
‪__construct(EnvironmentService $environmentService=null, ResourceFactory $resourceFactory=null)
Definition: ImageService.php:50
‪TYPO3\CMS\Extbase\Service\ImageService
Definition: ImageService.php:35
‪TYPO3\CMS\Extbase\Service\ImageService\setCompatibilityValues
‪setCompatibilityValues(ProcessedFile $processedImage)
Definition: ImageService.php:212
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Extbase\Service\ImageService\$environmentService
‪EnvironmentService $environmentService
Definition: ImageService.php:42
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\ProcessedFile\getPublicUrl
‪string null getPublicUrl($relativeToCurrentScript=false)
Definition: ProcessedFile.php:591
‪TYPO3\CMS\Extbase\Service\EnvironmentService
Definition: EnvironmentService.php:27
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:44
‪TYPO3\CMS\Core\Resource\ProcessedFile\getProperty
‪mixed getProperty($key)
Definition: ProcessedFile.php:477
‪TYPO3\CMS\Core\Resource\AbstractFile\getExtension
‪string getExtension()
Definition: AbstractFile.php:254
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Extbase\Service\ImageService\applyProcessingInstructions
‪ProcessedFile applyProcessingInstructions($image, array $processingInstructions)
Definition: ImageService.php:63
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Extbase\Service\ImageService\getCompatibilityImageResourceValues
‪array getCompatibilityImageResourceValues(ProcessedFile $processedImage)
Definition: ImageService.php:245
‪TYPO3\CMS\Core\Resource\FileInterface\getPublicUrl
‪string null getPublicUrl($relativeToCurrentScript=false)
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Extbase\Service\ImageService\getImage
‪FileInterface File FileReference getImage(string $src, $image, bool $treatIdAsReference)
Definition: ImageService.php:131