TYPO3 CMS  TYPO3_7-6
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 
54  public function initializeArguments()
55  {
56  parent::initializeArguments();
58  $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', false);
59  }
60 
70  public function render($src, $width = null, $height = null, $fallbackImage = '')
71  {
72  $content = '';
73  $uri = $this->getImageUri($src);
74 
75  if (empty($uri) && $fallbackImage !== '') {
76  $uri = $this->getImageUri($fallbackImage);
77  }
78 
79  if (!empty($uri)) {
80  if ($width) {
81  $this->tag->addAttribute('width', (int)$width);
82  }
83  if ($height) {
84  $this->tag->addAttribute('height', (int)$height);
85  }
86  $this->tag->addAttribute('src', $uri);
87  $content = $this->tag->render();
88  }
89 
90  return $content;
91  }
92 
99  protected function getImageUri($src)
100  {
102  if ($uri !== '' && !file_exists($uri)) {
103  $uri = '';
104  }
105  if ($uri !== '') {
106  $uri = '../' . PathUtility::stripPathSitePrefix($uri);
107  }
108  return $uri;
109  }
110 }
registerTagAttribute($name, $type, $description, $required=false, $default=null)
render($src, $width=null, $height=null, $fallbackImage='')
static getFileAbsFileName($filename, $onlyRelative=true, $relToTYPO3_mainDir=false)