5b6abdb02bcac98dfd1ab1f2733dc1497ce34630
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / Uri / ImageViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Fluid\ViewHelpers\Uri;
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
17 use TYPO3\CMS\Core\Resource\FileInterface;
18 use TYPO3\CMS\Extbase\Domain\Model\AbstractFileFolder;
19
20 /**
21 * Resizes a given image (if required) and returns its relative path.
22 *
23 * = Examples =
24 *
25 * <code title="Default">
26 * <f:uri.image src="EXT:myext/Resources/Public/typo3_logo.png" />
27 * </code>
28 * <output>
29 * typo3conf/ext/myext/Resources/Public/typo3_logo.png
30 * or (in BE mode):
31 * ../typo3conf/ext/myext/Resources/Public/typo3_logo.png
32 * </output>
33 *
34 * <code title="Image Object">
35 * <f:uri.image image="{imageObject}" />
36 * </code>
37 * <output>
38 * fileadmin/images/image.png
39 * or (in BE mode):
40 * fileadmin/images/image.png
41 * </output>
42 *
43 * <code title="Inline notation">
44 * {f:uri.image(src: 'EXT:myext/Resources/Public/typo3_logo.png', minWidth: 30, maxWidth: 40)}
45 * </code>
46 * <output>
47 * typo3temp/pics/[b4c0e7ed5c].png
48 * (depending on your TYPO3s encryption key)
49 * </output>
50 *
51 * <code title="non existing image">
52 * <f:uri.image src="NonExistingImage.png" />
53 * </code>
54 * <output>
55 * Could not get image resource for "NonExistingImage.png".
56 * </output>
57 */
58 class ImageViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {
59 /**
60 * @var \TYPO3\CMS\Extbase\Service\ImageService
61 * @inject
62 */
63 protected $imageService;
64
65 /**
66 * Resizes the image (if required) and returns its path. If the image was not resized, the path will be equal to $src
67 *
68 * @see http://typo3.org/documentation/document-library/references/doc_core_tsref/4.2.0/view/1/5/#id4164427
69 * @param string $src
70 * @param FileInterface|AbstractFileFolder $image
71 * @param string $width width of the image. This can be a numeric value representing the fixed width of the image in pixels. But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width for possible options.
72 * @param string $height height of the image. This can be a numeric value representing the fixed height of the image in pixels. But you can also perform simple calculations by adding "m" or "c" to the value. See imgResource.width for possible options.
73 * @param int $minWidth minimum width of the image
74 * @param int $minHeight minimum height of the image
75 * @param int $maxWidth maximum width of the image
76 * @param int $maxHeight maximum height of the image
77 * @param bool $treatIdAsReference given src argument is a sys_file_reference record
78 * @param string|bool $crop overrule cropping of image (setting to FALSE disables the cropping set in FileReference)
79 * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
80 * @return string path to the image
81 */
82 public function render($src = NULL, $image = NULL, $width = NULL, $height = NULL, $minWidth = NULL, $minHeight = NULL, $maxWidth = NULL, $maxHeight = NULL, $treatIdAsReference = FALSE, $crop = NULL) {
83 if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) {
84 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('You must either specify a string src or a File object.', 1382284105);
85 }
86 if ($crop === NULL) {
87 $crop = $image instanceof FileReference ? $image->getProperty('crop') : NULL;
88 }
89 $image = $this->imageService->getImage($src, $image, $treatIdAsReference);
90 $processingInstructions = array(
91 'width' => $width,
92 'height' => $height,
93 'minWidth' => $minWidth,
94 'minHeight' => $minHeight,
95 'maxWidth' => $maxWidth,
96 'maxHeight' => $maxHeight,
97 'crop' => $crop,
98 );
99 $processedImage = $this->imageService->applyProcessingInstructions($image, $processingInstructions);
100 return $this->imageService->getImageUri($processedImage);
101 }
102
103 }