TYPO3 API  SVNRelease
ImageViewHelper.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*                                                                        *
00004  * This script is part of the TYPO3 project - inspiring people to share!  *
00005  *                                                                        *
00006  * TYPO3 is free software; you can redistribute it and/or modify it under *
00007  * the terms of the GNU General Public License version 2 as published by  *
00008  * the Free Software Foundation.                                          *
00009  *                                                                        *
00010  * This script is distributed in the hope that it will be useful, but     *
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
00012  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
00013  * Public License for more details.                                       *
00014  *                                                                        */
00015 
00016 /**
00017  * Resizes a given image (if required) and renders the respective img tag
00018  *
00019  * = Examples =
00020  *
00021  * <code title="Default">
00022  * <f:image src="EXT:myext/Resources/Public/typo3_logo.png" alt="alt text" />
00023  * </code>
00024  * <output>
00025  * <img alt="alt text" src="typo3conf/ext/myext/Resources/Public/typo3_logo.png" width="396" height="375" />
00026  * or (in BE mode):
00027  * <img alt="alt text" src="../typo3conf/ext/viewhelpertest/Resources/Public/typo3_logo.png" width="396" height="375" />
00028  * </output>
00029  *
00030  * <code title="Inline notation">
00031  * {f:image(src: 'EXT:viewhelpertest/Resources/Public/typo3_logo.png', alt: 'alt text', minWidth: 30, maxWidth: 40)}
00032  * </code>
00033  * <output>
00034  * <img alt="alt text" src="../typo3temp/pics/f13d79a526.png" width="40" height="38" />
00035  * (depending on your TYPO3s encryption key)
00036  * </output>
00037  *
00038  * <code title="non existing image">
00039  * <f:image src="NonExistingImage.png" alt="foo" />
00040  * </code>
00041  * <output>
00042  * Could not get image resource for "NonExistingImage.png".
00043  * </output>
00044  */
00045 class Tx_Fluid_ViewHelpers_ImageViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractTagBasedViewHelper {
00046 
00047     /**
00048      * @var tslib_cObj
00049      */
00050     protected $contentObject;
00051 
00052     /**
00053      * @var string
00054      */
00055     protected $tagName = 'img';
00056 
00057     /**
00058      * @var t3lib_fe contains a backup of the current $GLOBALS['TSFE'] if used in BE mode
00059      */
00060     protected $tsfeBackup;
00061 
00062     /**
00063      * @var string
00064      */
00065     protected $workingDirectoryBackup;
00066 
00067     /**
00068      * @var Tx_Extbase_Configuration_ConfigurationManagerInterface
00069      */
00070     protected $configurationManager;
00071 
00072     /**
00073      * @param Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager
00074      * @return void
00075      */
00076     public function injectConfigurationManager(Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager) {
00077         $this->configurationManager = $configurationManager;
00078         $this->contentObject = $this->configurationManager->getContentObject();
00079     }
00080 
00081     /**
00082      * Initialize arguments.
00083      *
00084      * @return void
00085      * @author Bastian Waidelich <bastian@typo3.org>
00086      */
00087     public function initializeArguments() {
00088         parent::initializeArguments();
00089         $this->registerUniversalTagAttributes();
00090         $this->registerTagAttribute('alt', 'string', 'Specifies an alternate text for an image', TRUE);
00091         $this->registerTagAttribute('ismap', 'string', 'Specifies an image as a server-side image-map. Rarely used. Look at usemap instead', FALSE);
00092         $this->registerTagAttribute('longdesc', 'string', 'Specifies the URL to a document that contains a long description of an image', FALSE);
00093         $this->registerTagAttribute('usemap', 'string', 'Specifies an image as a client-side image-map', FALSE);
00094     }
00095 
00096     /**
00097      * Resizes a given image (if required) and renders the respective img tag
00098      * @see http://typo3.org/documentation/document-library/references/doc_core_tsref/4.2.0/view/1/5/#id4164427
00099      *
00100      * @param string $src
00101      * @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.
00102      * @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.
00103      * @param integer $minWidth minimum width of the image
00104      * @param integer $minHeight minimum height of the image
00105      * @param integer $maxWidth maximum width of the image
00106      * @param integer $maxHeight maximum height of the image
00107      *
00108      * @return string rendered tag.
00109      * @author Sebastian Böttger <sboettger@cross-content.com>
00110      * @author Bastian Waidelich <bastian@typo3.org>
00111      */
00112     public function render($src, $width = NULL, $height = NULL, $minWidth = NULL, $minHeight = NULL, $maxWidth = NULL, $maxHeight = NULL) {
00113         if (TYPO3_MODE === 'BE') {
00114             $this->simulateFrontendEnvironment();
00115         }
00116         $setup = array(
00117             'width' => $width,
00118             'height' => $height,
00119             'minW' => $minWidth,
00120             'minH' => $minHeight,
00121             'maxW' => $maxWidth,
00122             'maxH' => $maxHeight
00123         );
00124         if (TYPO3_MODE === 'BE' && substr($src, 0, 3) === '../') {
00125             $src = substr($src, 3);
00126         }
00127         $imageInfo = $this->contentObject->getImgResource($src, $setup);
00128         $GLOBALS['TSFE']->lastImageInfo = $imageInfo;
00129         if (!is_array($imageInfo)) {
00130             throw new Tx_Fluid_Core_ViewHelper_Exception('Could not get image resource for "' . htmlspecialchars($src) . '".' , 1253191060);
00131         }
00132         $imageInfo[3] = t3lib_div::png_to_gif_by_imagemagick($imageInfo[3]);
00133         $GLOBALS['TSFE']->imagesOnPage[] = $imageInfo[3];
00134 
00135         $imageSource = $GLOBALS['TSFE']->absRefPrefix . t3lib_div::rawUrlEncodeFP($imageInfo[3]);
00136         if (TYPO3_MODE === 'BE') {
00137             $imageSource = '../' . $imageSource;
00138             $this->resetFrontendEnvironment();
00139         }
00140         $this->tag->addAttribute('src', $imageSource);
00141         $this->tag->addAttribute('width', $imageInfo[0]);
00142         $this->tag->addAttribute('height', $imageInfo[1]);
00143         if ($this->arguments['title'] === '') {
00144             $this->tag->addAttribute('title', $this->arguments['alt']);
00145         }
00146 
00147         return $this->tag->render();
00148     }
00149 
00150     /**
00151      * Prepares $GLOBALS['TSFE'] for Backend mode
00152      * This somewhat hacky work around is currently needed because the getImgResource() function of tslib_cObj relies on those variables to be set
00153      *
00154      * @return void
00155      * @author Bastian Waidelich <bastian@typo3.org>
00156      */
00157     protected function simulateFrontendEnvironment() {
00158         $this->tsfeBackup = isset($GLOBALS['TSFE']) ? $GLOBALS['TSFE'] : NULL;
00159             // set the working directory to the site root
00160         $this->workingDirectoryBackup = getcwd();
00161         chdir(PATH_site);
00162 
00163         $typoScriptSetup = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
00164         $GLOBALS['TSFE'] = new stdClass();
00165         $template = t3lib_div::makeInstance('t3lib_TStemplate');
00166         $template->tt_track = 0;
00167         $template->init();
00168         $template->getFileName_backPath = PATH_site;
00169         $GLOBALS['TSFE']->tmpl = $template;
00170         $GLOBALS['TSFE']->tmpl->setup = $typoScriptSetup;
00171         $GLOBALS['TSFE']->config = $typoScriptSetup;
00172     }
00173 
00174     /**
00175      * Resets $GLOBALS['TSFE'] if it was previously changed by simulateFrontendEnvironment()
00176      *
00177      * @return void
00178      * @author Bastian Waidelich <bastian@typo3.org>
00179      * @see simulateFrontendEnvironment()
00180      */
00181     protected function resetFrontendEnvironment() {
00182         $GLOBALS['TSFE'] = $this->tsfeBackup;
00183         chdir($this->workingDirectoryBackup);
00184     }
00185 }