Merge "[TASK] Increase version number"
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Classes / ViewHelpers / ImageViewHelper.php
1 <?php
2
3 /* *
4 * This script is part of the TYPO3 project - inspiring people to share! *
5 * *
6 * TYPO3 is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU General Public License version 2 as published by *
8 * the Free Software Foundation. *
9 * *
10 * This script is distributed in the hope that it will be useful, but *
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
12 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
13 * Public License for more details. *
14 * */
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 Tx_Fluid_ViewHelpers_ImageViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractTagBasedViewHelper {
46
47 /**
48 * @var tslib_cObj
49 */
50 protected $contentObject;
51
52 /**
53 * @var string
54 */
55 protected $tagName = 'img';
56
57 /**
58 * @var t3lib_fe 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 Tx_Extbase_Configuration_ConfigurationManagerInterface
69 */
70 protected $configurationManager;
71
72 /**
73 * @param Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager
74 * @return void
75 */
76 public function injectConfigurationManager(Tx_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 * @see http://typo3.org/documentation/document-library/references/doc_core_tsref/4.2.0/view/1/5/#id4164427
98 *
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 *
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 Tx_Fluid_Core_ViewHelper_Exception('Could not get image resource for "' . htmlspecialchars($src) . '".' , 1253191060);
128 }
129 $imageInfo[3] = t3lib_div::png_to_gif_by_imagemagick($imageInfo[3]);
130 $GLOBALS['TSFE']->imagesOnPage[] = $imageInfo[3];
131
132 $imageSource = $GLOBALS['TSFE']->absRefPrefix . t3lib_div::rawUrlEncodeFP($imageInfo[3]);
133 if (TYPO3_MODE === 'BE') {
134 $imageSource = '../' . $imageSource;
135 $this->resetFrontendEnvironment();
136 }
137 $this->tag->addAttribute('src', $imageSource);
138 $this->tag->addAttribute('width', $imageInfo[0]);
139 $this->tag->addAttribute('height', $imageInfo[1]);
140 if ($this->arguments['title'] === '') {
141 $this->tag->addAttribute('title', $this->arguments['alt']);
142 }
143
144 return $this->tag->render();
145 }
146
147 /**
148 * Prepares $GLOBALS['TSFE'] for Backend mode
149 * This somewhat hacky work around is currently needed because the getImgResource() function of tslib_cObj relies on those variables to be set
150 *
151 * @return void
152 */
153 protected function simulateFrontendEnvironment() {
154 $this->tsfeBackup = isset($GLOBALS['TSFE']) ? $GLOBALS['TSFE'] : NULL;
155 // set the working directory to the site root
156 $this->workingDirectoryBackup = getcwd();
157 chdir(PATH_site);
158
159 $typoScriptSetup = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
160 $GLOBALS['TSFE'] = new stdClass();
161 $template = t3lib_div::makeInstance('t3lib_TStemplate');
162 $template->tt_track = 0;
163 $template->init();
164 $template->getFileName_backPath = PATH_site;
165 $GLOBALS['TSFE']->tmpl = $template;
166 $GLOBALS['TSFE']->tmpl->setup = $typoScriptSetup;
167 $GLOBALS['TSFE']->config = $typoScriptSetup;
168 }
169
170 /**
171 * Resets $GLOBALS['TSFE'] if it was previously changed by simulateFrontendEnvironment()
172 *
173 * @return void
174 * @see simulateFrontendEnvironment()
175 */
176 protected function resetFrontendEnvironment() {
177 $GLOBALS['TSFE'] = $this->tsfeBackup;
178 chdir($this->workingDirectoryBackup);
179 }
180 }