[TASK] Update version to 6.1.0-dev
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / ImageViewHelper.php
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 /**
17 * Resizes a given image (if required) and renders the respective img tag
18 *
19 * = Examples =
20 *
21 * <code title="Default">
22 * <f:image src="EXT:myext/Resources/Public/typo3_logo.png" alt="alt text" />
23 * </code>
24 * <output>
25 * <img alt="alt text" src="typo3conf/ext/myext/Resources/Public/typo3_logo.png" width="396" height="375" />
26 * or (in BE mode):
27 * <img alt="alt text" src="../typo3conf/ext/viewhelpertest/Resources/Public/typo3_logo.png" width="396" height="375" />
28 * </output>
29 *
30 * <code title="Inline notation">
31 * {f:image(src: 'EXT:viewhelpertest/Resources/Public/typo3_logo.png', alt: 'alt text', minWidth: 30, maxWidth: 40)}
32 * </code>
33 * <output>
34 * <img alt="alt text" src="../typo3temp/pics/f13d79a526.png" width="40" height="38" />
35 * (depending on your TYPO3s encryption key)
36 * </output>
37 *
38 * <code title="non existing image">
39 * <f:image src="NonExistingImage.png" alt="foo" />
40 * </code>
41 * <output>
42 * Could not get image resource for "NonExistingImage.png".
43 * </output>
44 */
45 class ImageViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper {
46
47 /**
48 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
49 */
50 protected $contentObject;
51
52 /**
53 * @var string
54 */
55 protected $tagName = 'img';
56
57 /**
58 * @var \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController contains a backup of the current $GLOBALS['TSFE'] if used in BE mode
59 */
60 protected $tsfeBackup;
61
62 /**
63 * @var string
64 */
65 protected $workingDirectoryBackup;
66
67 /**
68 * @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface
69 */
70 protected $configurationManager;
71
72 /**
73 * @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager
74 * @return void
75 */
76 public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager) {
77 $this->configurationManager = $configurationManager;
78 $this->contentObject = $this->configurationManager->getContentObject();
79 }
80
81 /**
82 * Initialize arguments.
83 *
84 * @return void
85 */
86 public function initializeArguments() {
87 parent::initializeArguments();
88 $this->registerUniversalTagAttributes();
89 $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', TRUE);
90 $this->registerTagAttribute('ismap', 'string', 'Specifies an image as a server-side image-map. Rarely used. Look at usemap instead', FALSE);
91 $this->registerTagAttribute('longdesc', 'string', 'Specifies the URL to a document that contains a long description of an image', FALSE);
92 $this->registerTagAttribute('usemap', 'string', 'Specifies an image as a client-side image-map', FALSE);
93 }
94
95 /**
96 * Resizes a given image (if required) and renders the respective img tag
97 *
98 * @see http://typo3.org/documentation/document-library/references/doc_core_tsref/4.2.0/view/1/5/#id4164427
99 * @param string $src
100 * @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.
101 * @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.
102 * @param integer $minWidth minimum width of the image
103 * @param integer $minHeight minimum height of the image
104 * @param integer $maxWidth maximum width of the image
105 * @param integer $maxHeight maximum height of the image
106 * @throws \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
107 * @return string rendered tag.
108 */
109 public function render($src, $width = NULL, $height = NULL, $minWidth = NULL, $minHeight = NULL, $maxWidth = NULL, $maxHeight = NULL) {
110 if (TYPO3_MODE === 'BE') {
111 $this->simulateFrontendEnvironment();
112 }
113 $setup = array(
114 'width' => $width,
115 'height' => $height,
116 'minW' => $minWidth,
117 'minH' => $minHeight,
118 'maxW' => $maxWidth,
119 'maxH' => $maxHeight
120 );
121 if (TYPO3_MODE === 'BE' && substr($src, 0, 3) === '../') {
122 $src = substr($src, 3);
123 }
124 $imageInfo = $this->contentObject->getImgResource($src, $setup);
125 $GLOBALS['TSFE']->lastImageInfo = $imageInfo;
126 if (!is_array($imageInfo)) {
127 throw new \TYPO3\CMS\Fluid\Core\ViewHelper\Exception('Could not get image resource for "' . htmlspecialchars($src) . '".', 1253191060);
128 }
129 $imageInfo[3] = \TYPO3\CMS\Core\Utility\GeneralUtility::png_to_gif_by_imagemagick($imageInfo[3]);
130 $GLOBALS['TSFE']->imagesOnPage[] = $imageInfo[3];
131 $imageSource = $GLOBALS['TSFE']->absRefPrefix . \TYPO3\CMS\Core\Utility\GeneralUtility::rawUrlEncodeFP($imageInfo[3]);
132 if (TYPO3_MODE === 'BE') {
133 $imageSource = '../' . $imageSource;
134 $this->resetFrontendEnvironment();
135 }
136 $this->tag->addAttribute('src', $imageSource);
137 $this->tag->addAttribute('width', $imageInfo[0]);
138 $this->tag->addAttribute('height', $imageInfo[1]);
139 //the alt-attribute is mandatory to have valid html-code, therefore add it even if it is empty
140 if (empty($this->arguments['alt'])) {
141 $this->tag->addAttribute('alt', '');
142 }
143 if (empty($this->arguments['title']) && !empty($this->arguments['alt'])) {
144 $this->tag->addAttribute('title', $this->arguments['alt']);
145 }
146 return $this->tag->render();
147 }
148
149 /**
150 * Prepares $GLOBALS['TSFE'] for Backend mode
151 * This somewhat hacky work around is currently needed because the getImgResource() function of tslib_cObj relies on those variables to be set
152 *
153 * @return void
154 */
155 protected function simulateFrontendEnvironment() {
156 $this->tsfeBackup = isset($GLOBALS['TSFE']) ? $GLOBALS['TSFE'] : NULL;
157 // set the working directory to the site root
158 $this->workingDirectoryBackup = getcwd();
159 chdir(PATH_site);
160 $typoScriptSetup = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
161 $GLOBALS['TSFE'] = new \stdClass();
162 $template = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\TypoScript\\TemplateService');
163 $template->tt_track = 0;
164 $template->init();
165 $template->getFileName_backPath = PATH_site;
166 $GLOBALS['TSFE']->tmpl = $template;
167 $GLOBALS['TSFE']->tmpl->setup = $typoScriptSetup;
168 $GLOBALS['TSFE']->config = $typoScriptSetup;
169 }
170
171 /**
172 * Resets $GLOBALS['TSFE'] if it was previously changed by simulateFrontendEnvironment()
173 *
174 * @return void
175 * @see simulateFrontendEnvironment()
176 */
177 protected function resetFrontendEnvironment() {
178 $GLOBALS['TSFE'] = $this->tsfeBackup;
179 chdir($this->workingDirectoryBackup);
180 }
181 }
182
183 ?>