TYPO3  7.6
fluid/Classes/ViewHelpers/ImageViewHelper.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers;
3 
4 /* *
5  * This script is part of the TYPO3 project - inspiring people to share! *
6  * *
7  * TYPO3 is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU General Public License version 2 as published by *
9  * the Free Software Foundation. *
10  * *
11  * This script is distributed in the hope that it will be useful, but *
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
14  * Public License for more details. *
15  * */
16 
20 
58 {
62  protected $tagName = 'img';
63 
68  protected $imageService;
69 
75  public function initializeArguments()
76  {
77  parent::initializeArguments();
79  $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', false);
80  $this->registerTagAttribute('ismap', 'string', 'Specifies an image as a server-side image-map. Rarely used. Look at usemap instead', false);
81  $this->registerTagAttribute('longdesc', 'string', 'Specifies the URL to a document that contains a long description of an image', false);
82  $this->registerTagAttribute('usemap', 'string', 'Specifies an image as a client-side image-map', false);
83  }
84 
104  public function render($src = null, $width = null, $height = null, $minWidth = null, $minHeight = null, $maxWidth = null, $maxHeight = null, $treatIdAsReference = false, $image = null, $crop = null, $absolute = false)
105  {
106  if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) {
107  throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('You must either specify a string src or a File object.', 1382284106);
108  }
109  $image = $this->imageService->getImage($src, $image, $treatIdAsReference);
110  if ($crop === null) {
111  $crop = $image instanceof FileReference ? $image->getProperty('crop') : null;
112  }
113  $processingInstructions = array(
114  'width' => $width,
115  'height' => $height,
116  'minWidth' => $minWidth,
117  'minHeight' => $minHeight,
118  'maxWidth' => $maxWidth,
119  'maxHeight' => $maxHeight,
120  'crop' => $crop,
121  );
122  $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
123  $imageUri = $this->imageService->getImageUri($processedImage, $absolute);
124 
125  $this->tag->addAttribute('src', $imageUri);
126  $this->tag->addAttribute('width', $processedImage->getProperty('width'));
127  $this->tag->addAttribute('height', $processedImage->getProperty('height'));
128 
129  $alt = $image->getProperty('alternative');
130  $title = $image->getProperty('title');
131 
132  // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
133  if (empty($this->arguments['alt'])) {
134  $this->tag->addAttribute('alt', $alt);
135  }
136  if (empty($this->arguments['title']) && $title) {
137  $this->tag->addAttribute('title', $title);
138  }
139 
140  return $this->tag->render();
141  }
142 }