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