Bug #10251 » ImageViewHelper.php

Patched version - Michael Knoll, 2010-10-14 12:29

 
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
 *
18
 * @todo documentation
19
 *
20
 */
21

    
22
/**
23
 *
24
 */
25
class Tx_Fluid_ViewHelpers_ImageViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractTagBasedViewHelper {
26

    
27
	/**
28
	 * @var	tslib_cObj
29
	 */
30
	protected $contentObject;
31

    
32
	/**
33
	 * @var string
34
	 */
35
	protected $tagName = 'img';
36

    
37
	/**
38
	 * @var	t3lib_fe contains a backup of the current $GLOBALS['TSFE'] if used in BE mode
39
	 */
40
	protected $tsfeBackup;
41

    
42
	/**
43
	 * @var	string
44
	 */
45
	protected $workingDirectoryBackup;
46

    
47
	/**
48
	 * Constructor. Used to create an instance of tslib_cObj used by the render() method.
49
	 *
50
	 * @param tslib_cObj $contentObject injector for tslib_cObj (optional)
51
	 * @return void
52
	 */
53
	public function __construct($contentObject = NULL) {
54
		$this->contentObject = $contentObject !== NULL ? $contentObject : t3lib_div::makeInstance('tslib_cObj');
55
		parent::__construct($contentObject);
56
	}
57

    
58
	/**
59
	 * Initialize arguments.
60
	 *
61
	 * @return void
62
	 * @author Bastian Waidelich <bastian@typo3.org>
63
	 */
64
	public function initializeArguments() {
65
		parent::initializeArguments();
66
		$this->registerUniversalTagAttributes();
67
		$this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', TRUE);
68
		$this->registerTagAttribute('ismap', 'string', 'Specifies an image as a server-side image-map. Rarely used. Look at usemap instead', FALSE);
69
		$this->registerTagAttribute('longdesc', 'string', 'Specifies the URL to a document that contains a long description of an image', FALSE);
70
		$this->registerTagAttribute('usemap', 'string', 'Specifies an image as a client-side image-map', FALSE);
71
	}
72

    
73
	/**
74
	 * Render the img tag.
75
	 * @see http://typo3.org/documentation/document-library/references/doc_core_tsref/4.2.0/view/1/5/#id4164427
76
	 *
77
	 * @param string $src
78
	 * @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.
79
	 * @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.
80
	 * @param integer $minWidth minimum width of the image
81
	 * @param integer $minHeight minimum height of the image
82
	 * @param integer $maxWidth maximum width of the image
83
	 * @param integer $maxHeight maximum height of the image
84
	 *
85
	 * @return string rendered tag.
86
	 * @author Sebastian Böttger <sboettger@cross-content.com>
87
	 * @author Bastian Waidelich <bastian@typo3.org>
88
	 */
89
	public function render($src, $width = NULL, $height = NULL, $minWidth = NULL, $minHeight = NULL, $maxWidth = NULL, $maxHeight = NULL) {
90
		if (TYPO3_MODE === 'BE') {
91
			$this->simulateFrontendEnvironment();
92
		}
93
		$setup = array(
94
			'width' => $width,
95
			'height' => $height,
96
			'minW' => $minWidth,
97
			'minH' => $minHeight,
98
			'maxW' => $maxWidth,
99
			'maxH' => $maxHeight
100
		);
101
		if (TYPO3_MODE === 'BE' && substr($src, 0, 3) === '../') {
102
			$src = substr($src, 3);
103
		}
104
		$imageInfo = $this->contentObject->getImgResource($src, $setup);
105
		$GLOBALS['TSFE']->lastImageInfo = $imageInfo;
106
		if (!is_array($imageInfo)) {
107
			throw new Tx_Fluid_Core_ViewHelper_Exception('Could not get image resource for "' . htmlspecialchars($src) . '".' , 1253191060);
108
		}
109
		$imageInfo[3] = t3lib_div::png_to_gif_by_imagemagick($imageInfo[3]);
110
		$GLOBALS['TSFE']->imagesOnPage[] = $imageInfo[3];
111

    
112
		$imageSource = $GLOBALS['TSFE']->absRefPrefix . t3lib_div::rawUrlEncodeFP($imageInfo[3]);
113
		if (TYPO3_MODE === 'BE') {
114
			$imageSource = '../' . $imageSource;
115
			$this->resetFrontendEnvironment();
116
		}
117
		$this->tag->addAttribute('src', $imageSource);
118
		$this->tag->addAttribute('width', $imageInfo[0]);
119
		$this->tag->addAttribute('height', $imageInfo[1]);
120
		if ($this->arguments['title'] === '') {
121
			$this->tag->addAttribute('title', $this->arguments['alt']);
122
		}
123

    
124
		return $this->tag->render();
125
	}
126

    
127
	/**
128
	 * Prepares $GLOBALS['TSFE'] for Backend mode
129
	 * This somewhat hacky work around is currently needed because the getImgResource() function of tslib_cObj relies on those variables to be set
130
	 *
131
	 * @return void
132
	 * @author Bastian Waidelich <bastian@typo3.org>
133
	 */
134
	protected function simulateFrontendEnvironment() {
135
		$this->tsfeBackup = isset($GLOBALS['TSFE']) ? $GLOBALS['TSFE'] : NULL;
136
			// set the working directory to the site root
137
		$this->workingDirectoryBackup = getcwd();
138
		chdir(PATH_site);
139

    
140
		$configurationManager = Tx_Extbase_Dispatcher::getConfigurationManager();
141
		$typoScriptSetup = $configurationManager->loadTypoScriptSetup();
142
		$GLOBALS['TSFE'] = new stdClass();
143
		$template = t3lib_div::makeInstance('t3lib_TStemplate');
144
		$template->tt_track = 0;
145
		$template->init();
146
		$template->getFileName_backPath = PATH_site;
147
		$GLOBALS['TSFE']->tmpl = $template;
148
		$GLOBALS['TSFE']->tmpl->setup = $typoScriptSetup;
149
		$GLOBALS['TSFE']->config = $typoScriptSetup;
150
	}
151

    
152
	/**
153
	 * Resets $GLOBALS['TSFE'] if it was previously changed by simulateFrontendEnvironment()
154
	 *
155
	 * @return void
156
	 * @author Bastian Waidelich <bastian@typo3.org>
157
	 * @see simulateFrontendEnvironment()
158
	 */
159
	protected function resetFrontendEnvironment() {
160
		$GLOBALS['TSFE'] = $this->tsfeBackup;
161
		chdir($this->workingDirectoryBackup);
162
	}
163
}
    (1-1/1)