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 
28 
68 {
87  public function render($src = null, $image = null, $width = null, $height = null, $minWidth = null, $minHeight = null, $maxWidth = null, $maxHeight = null, $treatIdAsReference = false, $crop = null, $absolute = false)
88  {
89  return self::renderStatic(
90  [
91  'src' => $src,
92  'image' => $image,
93  'width' => $width,
94  'height' => $height,
95  'minWidth' => $minWidth,
96  'minHeight' => $minHeight,
97  'maxWidth' => $maxWidth,
98  'maxHeight' => $maxHeight,
99  'treatIdAsReference' => $treatIdAsReference,
100  'crop' => $crop,
101  'absolute' => $absolute,
102  ],
104  $this->renderingContext
105  );
106  }
107 
116  {
117  $src = $arguments['src'];
118  $image = $arguments['image'];
119  $treatIdAsReference = $arguments['treatIdAsReference'];
120  $crop = $arguments['crop'];
121  $absolute = $arguments['absolute'];
122 
123  if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) {
124  throw new Exception('You must either specify a string src or a File object.', 1382284105);
125  }
126 
127  try {
128  $imageService = self::getImageService();
129  $image = $imageService->getImage($src, $image, $treatIdAsReference);
130 
131  if ($crop === null) {
132  $crop = $image instanceof FileReference ? $image->getProperty('crop') : null;
133  }
134 
135  $processingInstructions = [
136  'width' => $arguments['width'],
137  'height' => $arguments['height'],
138  'minWidth' => $arguments['minWidth'],
139  'minHeight' => $arguments['minHeight'],
140  'maxWidth' => $arguments['maxWidth'],
141  'maxHeight' => $arguments['maxHeight'],
142  'crop' => $crop,
143  ];
144  $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions);
145  return $imageService->getImageUri($processedImage, $absolute);
146  } catch (ResourceDoesNotExistException $e) {
147  // thrown if file does not exist
148  } catch (\UnexpectedValueException $e) {
149  // thrown if a file has been replaced with a folder
150  } catch (\RuntimeException $e) {
151  // RuntimeException thrown if a file is outside of a storage
152  } catch (\InvalidArgumentException $e) {
153  // thrown if file storage does not exist
154  }
155  return '';
156  }
157 
163  protected static function getImageService()
164  {
166  $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
167  return $objectManager->get(ImageService::class);
168  }
169 }
static renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
render($src=null, $image=null, $width=null, $height=null, $minWidth=null, $minHeight=null, $maxWidth=null, $maxHeight=null, $treatIdAsReference=false, $crop=null, $absolute=false)