TYPO3 CMS  TYPO3_7-6
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 
21 
67 {
71  protected $tagName = 'img';
72 
76  protected $imageService;
77 
81  public function injectImageService(\TYPO3\CMS\Extbase\Service\ImageService $imageService)
82  {
83  $this->imageService = $imageService;
84  }
85 
91  public function initializeArguments()
92  {
93  parent::initializeArguments();
95  $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', false);
96  $this->registerTagAttribute('ismap', 'string', 'Specifies an image as a server-side image-map. Rarely used. Look at usemap instead', false);
97  $this->registerTagAttribute('longdesc', 'string', 'Specifies the URL to a document that contains a long description of an image', false);
98  $this->registerTagAttribute('usemap', 'string', 'Specifies an image as a client-side image-map', false);
99  }
100 
120  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)
121  {
122  if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) {
123  throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('You must either specify a string src or a File object.', 1382284106);
124  }
125 
126  try {
127  $image = $this->imageService->getImage($src, $image, $treatIdAsReference);
128  if ($crop === null) {
129  $crop = $image instanceof FileReference ? $image->getProperty('crop') : null;
130  }
131  $processingInstructions = [
132  'width' => $width,
133  'height' => $height,
134  'minWidth' => $minWidth,
135  'minHeight' => $minHeight,
136  'maxWidth' => $maxWidth,
137  'maxHeight' => $maxHeight,
138  'crop' => $crop,
139  ];
140  $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
141  $imageUri = $this->imageService->getImageUri($processedImage, $absolute);
142 
143  $this->tag->addAttribute('src', $imageUri);
144  $this->tag->addAttribute('width', $processedImage->getProperty('width'));
145  $this->tag->addAttribute('height', $processedImage->getProperty('height'));
146 
147  $alt = $image->getProperty('alternative');
148  $title = $image->getProperty('title');
149 
150  // The alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
151  if (empty($this->arguments['alt'])) {
152  $this->tag->addAttribute('alt', $alt);
153  }
154  if (empty($this->arguments['title']) && $title) {
155  $this->tag->addAttribute('title', $title);
156  }
157  } catch (ResourceDoesNotExistException $e) {
158  // thrown if file does not exist
159  } catch (\UnexpectedValueException $e) {
160  // thrown if a file has been replaced with a folder
161  } catch (\RuntimeException $e) {
162  // RuntimeException thrown if a file is outside of a storage
163  } catch (\InvalidArgumentException $e) {
164  // thrown if file storage does not exist
165  }
166 
167  return $this->tag->render();
168  }
169 }
render($src=null, $width=null, $height=null, $minWidth=null, $minHeight=null, $maxWidth=null, $maxHeight=null, $treatIdAsReference=false, $image=null, $crop=null, $absolute=false)
injectImageService(\TYPO3\CMS\Extbase\Service\ImageService $imageService)
registerTagAttribute($name, $type, $description, $required=false, $default=null)