‪TYPO3CMS  9.5
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 
25 
30 {
34  protected ‪$resourceFactory;
35 
39  protected ‪$environmentService;
40 
48  {
49  $this->environmentService = ‪$environmentService ?? GeneralUtility::makeInstance(EnvironmentService::class);
50  $this->resourceFactory = ‪$resourceFactory ?? ‪ResourceFactory::getInstance();
51  }
52 
60  public function ‪applyProcessingInstructions($image, $processingInstructions)
61  {
62  if (is_callable([$image, 'getOriginalFile'])) {
63  // Get the original file from the file reference
64  $image = $image->‪getOriginalFile();
65  }
66 
67  $processedImage = $image->‪process(‪ProcessedFile::CONTEXT_IMAGECROPSCALEMASK, $processingInstructions);
68  $this->‪setCompatibilityValues($processedImage);
69 
70  return $processedImage;
71  }
72 
80  public function ‪getImageUri(‪FileInterface $image, $absolute = false)
81  {
82  $imageUrl = $image->‪getPublicUrl();
83  $parsedUrl = parse_url($imageUrl);
84  // no prefix in case of an already fully qualified URL
85  if (isset($parsedUrl['host'])) {
86  $uriPrefix = '';
87  } elseif ($this->environmentService->isEnvironmentInFrontendMode()) {
88  $uriPrefix = ‪$GLOBALS['TSFE']->absRefPrefix;
89  } else {
90  $uriPrefix = GeneralUtility::getIndpEnv('TYPO3_SITE_PATH');
91  }
92 
93  if ($absolute) {
94  // If full URL has no scheme we add the same scheme as used by the site
95  // so we have an absolute URL also usable outside of browser scope (e.g. in an email message)
96  if (isset($parsedUrl['host']) && !isset($parsedUrl['scheme'])) {
97  $uriPrefix = (GeneralUtility::getIndpEnv('TYPO3_SSL') ? 'https:' : 'http:') . $uriPrefix;
98  }
99  return GeneralUtility::locationHeaderUrl($uriPrefix . $imageUrl);
100  }
101  return $uriPrefix . $imageUrl;
102  }
103 
118  public function ‪getImage($src, $image, $treatIdAsReference)
119  {
120  if ($image === null) {
121  $image = $this->‪getImageFromSourceString($src, $treatIdAsReference);
122  } elseif (is_callable([$image, 'getOriginalResource'])) {
123  // We have a domain model, so we need to fetch the FAL resource object from there
124  $image = $image->getOriginalResource();
125  }
126 
127  if (!($image instanceof ‪File || $image instanceof ‪FileReference)) {
128  $class = is_object($image) ? get_class($image) : 'null';
129  throw new \UnexpectedValueException('Supplied file object type ' . $class . ' for ' . $src . ' must be File or FileReference.', 1382687163);
130  }
131 
132  return $image;
133  }
134 
142  protected function ‪getImageFromSourceString($src, $treatIdAsReference)
143  {
144  if ($this->environmentService->isEnvironmentInBackendMode() && strpos($src, '../') === 0) {
145  $src = substr($src, 3);
146  }
148  if ($treatIdAsReference) {
149  $image = $this->resourceFactory->getFileReferenceObject($src);
150  } else {
151  $image = $this->resourceFactory->getFileObject($src);
152  }
153  } elseif (strpos($src, 't3://file') === 0) {
154  // We have a t3://file link to a file in FAL
155  $linkService = GeneralUtility::makeInstance(LinkService::class);
156  $data = $linkService->resolveByStringRepresentation($src);
157  $image = $data['file'];
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 
171  protected function ‪setCompatibilityValues(‪ProcessedFile $processedImage)
172  {
173  if ($this->environmentService->isEnvironmentInFrontendMode()) {
174  ‪$GLOBALS['TSFE']->lastImageInfo = $this->‪getCompatibilityImageResourceValues($processedImage);
175  ‪$GLOBALS['TSFE']->imagesOnPage[] = $processedImage->‪getPublicUrl();
176  }
177  }
178 
187  protected function ‪getCompatibilityImageResourceValues(‪ProcessedFile $processedImage)
188  {
189  return [
190  0 => $processedImage->‪getProperty('width'),
191  1 => $processedImage->‪getProperty('height'),
192  2 => $processedImage->‪getExtension(),
193  3 => $processedImage->‪getPublicUrl(),
194  'origFile' => $processedImage->‪getOriginalFile()->‪getPublicUrl(),
195  'origFile_mtime' => $processedImage->‪getOriginalFile()->‪getModificationTime(),
196  // This is needed by \TYPO3\CMS\Frontend\Imaging\GifBuilder,
197  // in order for the setup-array to create a unique filename hash.
198  'originalFile' => $processedImage->‪getOriginalFile(),
199  'processedFile' => $processedImage
200  ];
201  }
202 }
‪TYPO3\CMS\Core\Resource\ProcessedFile\getOriginalFile
‪File getOriginalFile()
Definition: ProcessedFile.php:293
‪TYPO3\CMS\Extbase\Service\ImageService\getImageFromSourceString
‪FileInterface FileReference TYPO3 CMS Core Resource Folder getImageFromSourceString($src, $treatIdAsReference)
Definition: ImageService.php:140
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:73
‪TYPO3\CMS\Extbase\Service\ImageService\$resourceFactory
‪ResourceFactory $resourceFactory
Definition: ImageService.php:33
‪TYPO3\CMS\Core\Resource\File\getPublicUrl
‪string null getPublicUrl($relativeToCurrentScript=false)
Definition: File.php:360
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:21
‪TYPO3\CMS\Extbase\Service\ImageService\getImageUri
‪string getImageUri(FileInterface $image, $absolute=false)
Definition: ImageService.php:78
‪TYPO3\CMS\Core\Resource\ProcessedFile\CONTEXT_IMAGECROPSCALEMASK
‪const CONTEXT_IMAGECROPSCALEMASK
Definition: ProcessedFile.php:56
‪TYPO3\CMS\Core\Resource\AbstractFile\getModificationTime
‪int getModificationTime()
Definition: AbstractFile.php:239
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:31
‪TYPO3\CMS\Extbase\Service
Definition: CacheService.php:2
‪TYPO3\CMS\Core\Resource\ResourceFactory\getInstance
‪static ResourceFactory getInstance()
Definition: ResourceFactory.php:39
‪TYPO3\CMS\Extbase\Service\ImageService\getImage
‪FileInterface FileReference getImage($src, $image, $treatIdAsReference)
Definition: ImageService.php:116
‪TYPO3\CMS\Core\Resource\File\process
‪ProcessedFile process($taskType, array $configuration)
Definition: File.php:293
‪TYPO3\CMS\Extbase\Service\ImageService\__construct
‪__construct(EnvironmentService $environmentService=null, ResourceFactory $resourceFactory=null)
Definition: ImageService.php:45
‪TYPO3\CMS\Extbase\Service\ImageService
Definition: ImageService.php:30
‪TYPO3\CMS\Extbase\Service\ImageService\setCompatibilityValues
‪setCompatibilityValues(ProcessedFile $processedImage)
Definition: ImageService.php:169
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:34
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:33
‪TYPO3\CMS\Extbase\Service\ImageService\$environmentService
‪EnvironmentService $environmentService
Definition: ImageService.php:37
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Core\Resource\ProcessedFile\getPublicUrl
‪string null getPublicUrl($relativeToCurrentScript=false)
Definition: ProcessedFile.php:567
‪TYPO3\CMS\Extbase\Service\EnvironmentService
Definition: EnvironmentService.php:24
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:42
‪TYPO3\CMS\Core\Resource\ProcessedFile\getProperty
‪mixed getProperty($key)
Definition: ProcessedFile.php:453
‪TYPO3\CMS\Core\Resource\AbstractFile\getExtension
‪string getExtension()
Definition: AbstractFile.php:252
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:21
‪TYPO3\CMS\Extbase\Service\ImageService\applyProcessingInstructions
‪ProcessedFile applyProcessingInstructions($image, $processingInstructions)
Definition: ImageService.php:58
‪TYPO3\CMS\Extbase\Service\ImageService\getCompatibilityImageResourceValues
‪array getCompatibilityImageResourceValues(ProcessedFile $processedImage)
Definition: ImageService.php:185
‪TYPO3\CMS\Core\Resource\FileInterface\getPublicUrl
‪string null getPublicUrl($relativeToCurrentScript=false)
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45