‪TYPO3CMS  11.5
DeferredBackendImageProcessor.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;
30 
35 {
36  public function ‪canProcessTask(‪TaskInterface $task): bool
37  {
38  $context = GeneralUtility::makeInstance(Context::class);
39  return (‪$GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface
40  && ‪ApplicationType::fromRequest(‪$GLOBALS['TYPO3_REQUEST'])->isBackend()
41  && $task->‪getType() === 'Image'
42  && in_array($task->‪getName(), ['Preview', 'CropScaleMask'], true)
43  && (!$context->hasAspect('fileProcessing') || $context->getPropertyFromAspect('fileProcessing', 'deferProcessing'))
44  && $task->‪getSourceFile()->‪getProperty('width') > 0
45  && $task->‪getSourceFile()->‪getProperty('height') > 0
46  // Let the local image processor update the properties in case the target file exists already
48  }
49 
50  public function ‪processTask(‪TaskInterface $task): void
51  {
52  try {
53  $imageDimension = ‪ImageDimension::fromProcessingTask($task);
54  } catch (‪ZeroImageDimensionException $e) {
55  // To not fail image processing, we just assume an image dimension here
56  $imageDimension = new ‪ImageDimension(64, 64);
57  }
58  $processedFile = $task->‪getTargetFile();
59  if (!$processedFile->isPersisted()) {
60  // For now, we need to persist the processed file in the repository to be able to reference its uid
61  // We could instead introduce a processing queue and persist the information there
62  $processedFileRepository = GeneralUtility::makeInstance(ProcessedFileRepository::class);
63  $processedFileRepository->add($processedFile);
64  }
65  $processedFile->setName($task->‪getTargetFileName());
66  $processingUrl = (string)GeneralUtility::makeInstance(UriBuilder::class)
67  ->buildUriFromRoute(
68  'image_processing',
69  [
70  'id' => $processedFile->getUid(),
71  ]
72  );
73  $processedFile->updateProcessingUrl(GeneralUtility::locationHeaderUrl($processingUrl));
74  $processedFile->updateProperties(
75  [
76  'width' => $imageDimension->getWidth(),
77  'height' => $imageDimension->getHeight(),
78  'size' => 0,
79  'checksum' => $task->‪getConfigurationChecksum(),
80  ]
81  );
82  $task->‪setExecuted(true);
83  }
84 }
‪TYPO3\CMS\Core\Http\ApplicationType\fromRequest
‪static static fromRequest(ServerRequestInterface $request)
Definition: ApplicationType.php:62
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository
Definition: ProcessedFileRepository.php:31
‪TYPO3\CMS\Backend\Resource\Processing\DeferredBackendImageProcessor\processTask
‪processTask(TaskInterface $task)
Definition: DeferredBackendImageProcessor.php:50
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getType
‪string getType()
‪TYPO3\CMS\Core\Imaging\Exception\ZeroImageDimensionException
Definition: ZeroImageDimensionException.php:26
‪TYPO3\CMS\Core\Resource\Folder\hasFile
‪bool hasFile($name)
Definition: Folder.php:400
‪TYPO3\CMS\Backend\Resource\Processing\DeferredBackendImageProcessor
Definition: DeferredBackendImageProcessor.php:35
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface
Definition: TaskInterface.php:33
‪TYPO3\CMS\Core\Resource\ResourceStorage\getProcessingFolder
‪Folder getProcessingFolder(File $file=null)
Definition: ResourceStorage.php:2777
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getSourceFile
‪Resource File getSourceFile()
‪TYPO3\CMS\Backend\Resource\Processing\DeferredBackendImageProcessor\canProcessTask
‪canProcessTask(TaskInterface $task)
Definition: DeferredBackendImageProcessor.php:36
‪TYPO3\CMS\Core\Http\ApplicationType
Definition: ApplicationType.php:52
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Core\Imaging\ImageDimension\fromProcessingTask
‪static fromProcessingTask(TaskInterface $task)
Definition: ImageDimension.php:57
‪TYPO3\CMS\Backend\Resource\Processing
Definition: DeferredBackendImageProcessor.php:18
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getTargetFile
‪Resource ProcessedFile getTargetFile()
‪TYPO3\CMS\Backend\Routing\UriBuilder
Definition: UriBuilder.php:40
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getName
‪string getName()
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Resource\File\getProperty
‪mixed getProperty($key)
Definition: File.php:65
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Resource\Processing\ProcessorInterface
Definition: ProcessorInterface.php:22
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\setExecuted
‪setExecuted($successful)
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getConfigurationChecksum
‪string getConfigurationChecksum()
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getTargetFileName
‪string getTargetFileName()
‪TYPO3\CMS\Core\Resource\AbstractFile\getStorage
‪ResourceStorage getStorage()
Definition: AbstractFile.php:395
‪TYPO3\CMS\Core\Imaging\ImageDimension
Definition: ImageDimension.php:32