TYPO3 CMS  TYPO3_6-2
ImageViewHelper.php
Go to the documentation of this file.
1 <?php
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 
61  protected $tagName = 'img';
62 
67  protected $imageService;
68 
74  public function initializeArguments() {
75  parent::initializeArguments();
77  $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', FALSE);
78  $this->registerTagAttribute('ismap', 'string', 'Specifies an image as a server-side image-map. Rarely used. Look at usemap instead', FALSE);
79  $this->registerTagAttribute('longdesc', 'string', 'Specifies the URL to a document that contains a long description of an image', FALSE);
80  $this->registerTagAttribute('usemap', 'string', 'Specifies an image as a client-side image-map', FALSE);
81  }
82 
100  public function render($src = NULL, $width = NULL, $height = NULL, $minWidth = NULL, $minHeight = NULL, $maxWidth = NULL, $maxHeight = NULL, $treatIdAsReference = FALSE, $image = NULL) {
101  if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) {
102  throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('You must either specify a string src or a File object.', 1382284106);
103  }
104 
105  try {
106  $image = $this->imageService->getImage($src, $image, $treatIdAsReference);
107  $processingInstructions = array(
108  'width' => $width,
109  'height' => $height,
110  'minWidth' => $minWidth,
111  'minHeight' => $minHeight,
112  'maxWidth' => $maxWidth,
113  'maxHeight' => $maxHeight,
114  );
115  $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
116  $imageUri = $this->imageService->getImageUri($processedImage);
117 
118  $this->tag->addAttribute('src', $imageUri);
119  $this->tag->addAttribute('width', $processedImage->getProperty('width'));
120  $this->tag->addAttribute('height', $processedImage->getProperty('height'));
121 
122  $alt = $image->getProperty('alternative');
123  $title = $image->getProperty('title');
124 
125  // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
126  if (empty($this->arguments['alt'])) {
127  $this->tag->addAttribute('alt', $alt);
128  }
129  if (empty($this->arguments['title']) && $title) {
130  $this->tag->addAttribute('title', $title);
131  }
132  } catch (ResourceDoesNotExistException $e) {
133  // thrown if file does not exist
134  } catch (\UnexpectedValueException $e) {
135  // thrown if a file has been replaced with a folder
136  } catch (\RuntimeException $e) {
137  // RuntimeException thrown if a file is outside of a storage
138  } catch (\InvalidArgumentException $e) {
139  // thrown if file storage does not exist
140  }
141 
142  return $this->tag->render();
143  }
144 }
registerTagAttribute($name, $type, $description, $required=FALSE, $default=NULL)
render($src=NULL, $width=NULL, $height=NULL, $minWidth=NULL, $minHeight=NULL, $maxWidth=NULL, $maxHeight=NULL, $treatIdAsReference=FALSE, $image=NULL)