‪TYPO3CMS  ‪main
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 
20 use Psr\Http\Message\ServerRequestInterface;
32 
37 {
41  protected ‪$resourceFactory;
42 
47  {
48  $this->resourceFactory = ‪$resourceFactory;
49  }
50 
56  public function ‪applyProcessingInstructions($image, array $processingInstructions): ‪ProcessedFile
57  {
58  /*
59  * todo: this method should be split to be able to have a proper method signature.
60  * todo: actually, this method only really works with objects of type \TYPO3\CMS\Core\Resource\File, as this
61  * todo: is the only implementation that supports the support method.
62  */
63  if (is_callable([$image, 'getOriginalFile'])) {
64  // Get the original file from the file reference
65  $image = $image->‪getOriginalFile();
66  }
67 
68  $processedImage = $image->‪process(‪ProcessedFile::CONTEXT_IMAGECROPSCALEMASK, $processingInstructions);
69  $this->‪setCompatibilityValues($processedImage);
70 
71  return $processedImage;
72  }
73 
79  public function ‪getImageUri(‪FileInterface $image, bool $absolute = false): string
80  {
81  $imageUrl = $image->getPublicUrl();
82  if (!$absolute || $imageUrl === null) {
83  return (string)$imageUrl;
84  }
85 
86  return GeneralUtility::locationHeaderUrl($imageUrl);
87  }
88 
102  public function ‪getImage(string $src, $image, bool $treatIdAsReference): ‪FileInterface
103  {
104  if ($image instanceof ‪File || $image instanceof ‪FileReference) {
105  // We already received a valid file and therefore just return it
106  return $image;
107  }
108 
109  if (is_callable([$image, 'getOriginalResource'])) {
110  // We have a domain model, so we need to fetch the FAL resource object from there
111  $originalResource = $image->getOriginalResource();
112  if (!($originalResource instanceof ‪File || $originalResource instanceof ‪FileReference)) {
113  throw new \UnexpectedValueException('No original resource could be resolved for supplied file ' . get_class($image), 1625838481);
114  }
115  return $originalResource;
116  }
117 
118  if ($image !== null) {
119  // Some value is given for $image, but it's not a valid type
120  throw new \UnexpectedValueException(
121  'Supplied file must be File or FileReference, ' . get_debug_type($image) . ' given.',
122  1625585157
123  );
124  }
125 
126  // Since image is not given, try to resolve an image from the source string
127  $resolvedImage = $this->‪getImageFromSourceString($src, $treatIdAsReference);
128 
129  if ($resolvedImage instanceof ‪File || $resolvedImage instanceof ‪FileReference) {
130  return $resolvedImage;
131  }
132 
133  if ($resolvedImage === null) {
134  // No image could be resolved using the given source string
135  throw new \UnexpectedValueException('Supplied ' . $src . ' could not be resolved to a File or FileReference.', 1625585158);
136  }
137 
138  // A FileInterface was found, however only File and FileReference are valid
139  throw new \UnexpectedValueException(
140  'Resolved file object type ' . get_class($resolvedImage) . ' for ' . $src . ' must be File or FileReference.',
141  1382687163
142  );
143  }
144 
148  protected function ‪getImageFromSourceString(string $src, bool $treatIdAsReference): ?‪FileInterface
149  {
150  if ((‪$GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface
151  && ‪ApplicationType::fromRequest(‪$GLOBALS['TYPO3_REQUEST'])->isBackend()
152  && str_starts_with($src, '../')
153  ) {
154  $src = substr($src, 3);
155  }
157  if ($treatIdAsReference) {
158  $image = $this->resourceFactory->getFileReferenceObject($src);
159  } else {
160  $image = $this->resourceFactory->getFileObject($src);
161  }
162  } elseif (str_starts_with($src, 't3://file')) {
163  // We have a t3://file link to a file in FAL
164  $linkService = GeneralUtility::makeInstance(LinkService::class);
165  $data = $linkService->resolveByStringRepresentation($src);
166  $image = $data['file'];
167  } else {
168  // We have a combined identifier or legacy (storage 0) path
169  $image = $this->resourceFactory->retrieveFileOrFolderObject($src);
170  }
171 
172  // Check the resolved image as this could also be a FolderInterface
173  return $image instanceof ‪FileInterface ? $image : null;
174  }
175 
180  protected function ‪setCompatibilityValues(‪ProcessedFile $processedImage): void
181  {
182  ‪$publicUrl = $processedImage->‪getPublicUrl();
183  if (‪$publicUrl !== null) {
184  // only add the processed image to AssetCollector if the public url is not NULL
185  $imageInfoValues = $this->‪getCompatibilityImageResourceValues($processedImage);
186  GeneralUtility::makeInstance(AssetCollector::class)->addMedia(
188  $imageInfoValues
189  );
190  }
191  }
192 
198  protected function ‪getCompatibilityImageResourceValues(‪ProcessedFile $processedImage): array
199  {
200  $originalFile = $processedImage->‪getOriginalFile();
201  return [
202  0 => $processedImage->‪getProperty('width'),
203  1 => $processedImage->‪getProperty('height'),
204  2 => $processedImage->‪getExtension(),
205  3 => $processedImage->‪getPublicUrl(),
206  'origFile' => $originalFile->getPublicUrl(),
207  'origFile_mtime' => $originalFile->getModificationTime(),
208  ];
209  }
210 }
‪TYPO3\CMS\Core\Resource\ProcessedFile\getOriginalFile
‪File getOriginalFile()
Definition: ProcessedFile.php:301
‪TYPO3\CMS\Core\Page\AssetCollector
Definition: AssetCollector.php:42
‪TYPO3\CMS\Extbase\Service\ImageService\$resourceFactory
‪ResourceFactory $resourceFactory
Definition: ImageService.php:40
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:24
‪TYPO3\CMS\Core\Resource\ProcessedFile\getPublicUrl
‪non empty string null getPublicUrl()
Definition: ProcessedFile.php:580
‪TYPO3\CMS\Core\Resource\ProcessedFile\CONTEXT_IMAGECROPSCALEMASK
‪const CONTEXT_IMAGECROPSCALEMASK
Definition: ProcessedFile.php:61
‪TYPO3\CMS\Extbase\Service\ImageService\applyProcessingInstructions
‪applyProcessingInstructions($image, array $processingInstructions)
Definition: ImageService.php:55
‪TYPO3\CMS\Extbase\Service\ImageService\getCompatibilityImageResourceValues
‪getCompatibilityImageResourceValues(ProcessedFile $processedImage)
Definition: ImageService.php:197
‪TYPO3\CMS\Core\Resource\ProcessedFile\getProperty
‪getProperty(string $key)
Definition: ProcessedFile.php:468
‪TYPO3\CMS\Core\Resource\FileReference
Definition: FileReference.php:35
‪TYPO3\CMS\Extbase\Service
Definition: CacheService.php:18
‪TYPO3\CMS\Core\Resource\AbstractFile\getExtension
‪getExtension()
Definition: AbstractFile.php:236
‪TYPO3\CMS\Extbase\Service\ImageService\getImage
‪getImage(string $src, $image, bool $treatIdAsReference)
Definition: ImageService.php:101
‪TYPO3\CMS\Extbase\Service\ImageService
Definition: ImageService.php:37
‪TYPO3\CMS\Extbase\Service\ImageService\setCompatibilityValues
‪setCompatibilityValues(ProcessedFile $processedImage)
Definition: ImageService.php:179
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger(mixed $var)
Definition: MathUtility.php:69
‪TYPO3\CMS\Webhooks\Message\$publicUrl
‪identifier readonly string readonly string $publicUrl
Definition: FileUpdatedMessage.php:36
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Extbase\Service\ImageService\getImageFromSourceString
‪getImageFromSourceString(string $src, bool $treatIdAsReference)
Definition: ImageService.php:147
‪TYPO3\CMS\Extbase\Service\ImageService\getImageUri
‪getImageUri(FileInterface $image, bool $absolute=false)
Definition: ImageService.php:78
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:47
‪TYPO3\CMS\Extbase\Service\ImageService\__construct
‪__construct(ResourceFactory $resourceFactory)
Definition: ImageService.php:45
‪TYPO3\CMS\Core\Resource\File\process
‪process(string $taskType, array $configuration)
Definition: File.php:238
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Http\fromRequest
‪@ fromRequest
Definition: ApplicationType.php:67
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Core\Http\ApplicationType
‪ApplicationType
Definition: ApplicationType.php:56