TYPO3 CMS  TYPO3_8-7
ImageViewHelper.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  */
18 
43 {
47  protected $tagName = 'img';
48 
52  public function initializeArguments()
53  {
54  parent::initializeArguments();
55  $this->registerArgument('src', 'string', '', true);
56  $this->registerArgument('width', 'int', 'width of the image');
57  $this->registerArgument('height', 'int', 'height of the image');
58  $this->registerArgument('fallbackImage', 'string', 'an optional fallback image if the $src image cannot be loaded', false, '');
60  $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', false);
61  }
62 
68  public function render()
69  {
70  $src = $this->arguments['src'];
71  $width = $this->arguments['width'];
72  $height = $this->arguments['height'];
73  $fallbackImage = $this->arguments['fallbackImage'];
74 
75  $content = '';
76  $uri = $this->getImageUri($src);
77 
78  if (empty($uri) && $fallbackImage !== '') {
79  $uri = $this->getImageUri($fallbackImage);
80  }
81 
82  if (!empty($uri)) {
83  if ($width) {
84  $this->tag->addAttribute('width', (int)$width);
85  }
86  if ($height) {
87  $this->tag->addAttribute('height', (int)$height);
88  }
89  $this->tag->addAttribute('src', $uri);
90  $content = $this->tag->render();
91  }
92 
93  return $content;
94  }
95 
102  protected function getImageUri($src)
103  {
105  if ($uri !== '' && !file_exists($uri)) {
106  $uri = '';
107  }
108  if ($uri !== '') {
109  $uri = '../' . PathUtility::stripPathSitePrefix($uri);
110  }
111  return $uri;
112  }
113 }
registerTagAttribute($name, $type, $description, $required=false, $default=null)
static getFileAbsFileName($filename, $_=null, $_2=null)