TYPO3 CMS  TYPO3_8-7
MediaViewHelper.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 
24 
55 {
59  protected $tagName = 'img';
60 
64  public function initializeArguments()
65  {
66  parent::initializeArguments();
68  $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', false);
69  $this->registerArgument('file', 'object', 'File', true);
70  $this->registerArgument('additionalConfig', 'array', 'This array can hold additional configuration that is passed though to the Renderer object', false, []);
71  $this->registerArgument('width', 'string', 'This can be a numeric value representing the fixed width of in pixels. But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width for possible options.');
72  $this->registerArgument('height', 'string', 'This can be a numeric value representing the fixed height in pixels. But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width for possible options.');
73  $this->registerArgument('cropVariant', 'string', 'select a cropping variant, in case multiple croppings have been specified or stored in FileReference', false, 'default');
74  }
75 
82  public function render()
83  {
84  $file = $this->arguments['file'];
85  $additionalConfig = $this->arguments['additionalConfig'];
86  $width = $this->arguments['width'];
87  $height = $this->arguments['height'];
88 
89  // get Resource Object (non ExtBase version)
90  if (is_callable([$file, 'getOriginalResource'])) {
91  // We have a domain model, so we need to fetch the FAL resource object from there
92  $file = $file->getOriginalResource();
93  }
94 
95  if (!($file instanceof FileInterface || $file instanceof AbstractFileFolder)) {
96  throw new \UnexpectedValueException('Supplied file object type ' . get_class($file) . ' must be FileInterface or AbstractFileFolder.', 1454252193);
97  }
98 
99  $fileRenderer = RendererRegistry::getInstance()->getRenderer($file);
100 
101  // Fallback to image when no renderer is found
102  if ($fileRenderer === null) {
103  return $this->renderImage($file, $width, $height);
104  }
105  $additionalConfig = array_merge_recursive($this->arguments, $additionalConfig);
106  return $fileRenderer->render($file, $width, $height, $additionalConfig);
107  }
108 
117  protected function renderImage(FileInterface $image, $width, $height)
118  {
119  $cropVariant = $this->arguments['cropVariant'] ?: 'default';
120  $cropString = $image instanceof FileReference ? $image->getProperty('crop') : '';
121  $cropVariantCollection = CropVariantCollection::create((string)$cropString);
122  $cropArea = $cropVariantCollection->getCropArea($cropVariant);
123  $processingInstructions = [
124  'width' => $width,
125  'height' => $height,
126  'crop' => $cropArea->isEmpty() ? null : $cropArea->makeAbsoluteBasedOnFile($image),
127  ];
128  $imageService = $this->getImageService();
129  $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
130  $imageUri = $imageService->getImageUri($processedImage);
131 
132  if (!$this->tag->hasAttribute('data-focus-area')) {
133  $focusArea = $cropVariantCollection->getFocusArea($cropVariant);
134  if (!$focusArea->isEmpty()) {
135  $this->tag->addAttribute('data-focus-area', $focusArea->makeAbsoluteBasedOnFile($image));
136  }
137  }
138  $this->tag->addAttribute('src', $imageUri);
139  $this->tag->addAttribute('width', $processedImage->getProperty('width'));
140  $this->tag->addAttribute('height', $processedImage->getProperty('height'));
141 
142  $alt = $image->getProperty('alternative');
143  $title = $image->getProperty('title');
144 
145  // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
146  if (empty($this->arguments['alt'])) {
147  $this->tag->addAttribute('alt', $alt);
148  }
149  if (empty($this->arguments['title']) && $title) {
150  $this->tag->addAttribute('title', $title);
151  }
152 
153  return $this->tag->render();
154  }
155 
161  protected function getImageService()
162  {
163  return $this->objectManager->get(ImageService::class);
164  }
165 }
registerTagAttribute($name, $type, $description, $required=false, $default=null)
static create(string $jsonString, array $tcaConfig=[])
renderImage(FileInterface $image, $width, $height)