TYPO3 CMS  TYPO3_7-6
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 
23 
54 {
58  protected $tagName = 'img';
59 
65  public function initializeArguments()
66  {
67  parent::initializeArguments();
69  $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', false);
70  }
71 
81  public function render($file, $additionalConfig = [], $width = null, $height = null)
82  {
83 
84  // get Resource Object (non ExtBase version)
85  if (is_callable([$file, 'getOriginalResource'])) {
86  // We have a domain model, so we need to fetch the FAL resource object from there
87  $file = $file->getOriginalResource();
88  }
89 
90  $fileRenderer = RendererRegistry::getInstance()->getRenderer($file);
91 
92  // Fallback to image when no renderer is found
93  if ($fileRenderer === null) {
94  return $this->renderImage($file, $width, $height);
95  } else {
96  $additionalConfig = array_merge_recursive($this->arguments, $additionalConfig);
97  return $fileRenderer->render($file, $width, $height, $additionalConfig);
98  }
99  }
100 
109  protected function renderImage(FileInterface $image, $width, $height)
110  {
111  $crop = $image instanceof FileReference ? $image->getProperty('crop') : null;
112  $processingInstructions = [
113  'width' => $width,
114  'height' => $height,
115  'crop' => $crop,
116  ];
117  $imageService = $this->getImageService();
118  $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
119  $imageUri = $imageService->getImageUri($processedImage);
120 
121  $this->tag->addAttribute('src', $imageUri);
122  $this->tag->addAttribute('width', $processedImage->getProperty('width'));
123  $this->tag->addAttribute('height', $processedImage->getProperty('height'));
124 
125  $alt = $image->getProperty('alternative');
126  $title = $image->getProperty('title');
127 
128  // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
129  if (empty($this->arguments['alt'])) {
130  $this->tag->addAttribute('alt', $alt);
131  }
132  if (empty($this->arguments['title']) && $title) {
133  $this->tag->addAttribute('title', $title);
134  }
135 
136  return $this->tag->render();
137  }
138 
144  protected function getImageService()
145  {
146  return $this->objectManager->get(ImageService::class);
147  }
148 }
registerTagAttribute($name, $type, $description, $required=false, $default=null)
render($file, $additionalConfig=[], $width=null, $height=null)
renderImage(FileInterface $image, $width, $height)