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 returns its relative path.
00018  *
00019  * = Examples =
00020  *
00021  * <code title="Default">
00022  * <f:uri.image src="EXT:myext/Resources/Public/typo3_logo.png" />
00023  * </code>
00024  * <output>
00025  * typo3conf/ext/myext/Resources/Public/typo3_logo.png
00026  * or (in BE mode):
00027  * ../typo3conf/ext/myext/Resources/Public/typo3_logo.png
00028  * </output>
00029  *
00030  * <code title="Inline notation">
00031  * {f:uri.image(src: 'EXT:myext/Resources/Public/typo3_logo.png' minWidth: 30, maxWidth: 40)}
00032  * </code>
00033  * <output>
00034  * typo3temp/pics/[b4c0e7ed5c].png
00035  * (depending on your TYPO3s encryption key)
00036  * </output>
00037  *
00038  * <code title="non existing image">
00039  * <f:uri.image src="NonExistingImage.png" />
00040  * </code>
00041  * <output>
00042  * Could not get image resource for "NonExistingImage.png".
00043  * </output>
00044  */
00045 class Tx_Fluid_ViewHelpers_Uri_ImageViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
00046 
00047     /**
00048      * @var tslib_cObj
00049      */
00050     protected $contentObject;
00051 
00052     /**
00053      * @var Tx_Extbase_Configuration_ConfigurationManagerInterface
00054      */
00055     protected $configurationManager;
00056 
00057     /**
00058      * @param Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager
00059      * @return void
00060      */
00061     public function injectConfigurationManager(Tx_Extbase_Configuration_ConfigurationManagerInterface $configurationManager) {
00062         $this->configurationManager = $configurationManager;
00063         $this->contentObject = $this->configurationManager->getContentObject();
00064     }
00065 
00066     /**
00067      * Resizes the image (if required) and returns its path. If the image was not resized, the path will be equal to $src
00068      * @see http://typo3.org/documentation/document-library/references/doc_core_tsref/4.2.0/view/1/5/#id4164427
00069      *
00070      * @param string $src
00071      * @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.
00072      * @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.
00073      * @param integer $minWidth minimum width of the image
00074      * @param integer $minHeight minimum height of the image
00075      * @param integer $maxWidth maximum width of the image
00076      * @param integer $maxHeight maximum height of the image
00077      * @return string path to the image
00078      * @author Bastian Waidelich <bastian@typo3.org>
00079      * @author Christian Baer <chr.baer@googlemail.com>
00080      */
00081     public function render($src, $width = NULL, $height = NULL, $minWidth = NULL, $minHeight = NULL, $maxWidth = NULL, $maxHeight = NULL) {
00082         if (TYPO3_MODE === 'BE') {
00083             $this->simulateFrontendEnvironment();
00084         }
00085         $setup = array(
00086             'width' => $width,
00087             'height' => $height,
00088             'minW' => $minWidth,
00089             'minH' => $minHeight,
00090             'maxW' => $maxWidth,
00091             'maxH' => $maxHeight
00092         );
00093         if (TYPO3_MODE === 'BE' && substr($src, 0, 3) === '../') {
00094             $src = substr($src, 3);
00095         }
00096         $imageInfo = $this->contentObject->getImgResource($src, $setup);
00097         $GLOBALS['TSFE']->lastImageInfo = $imageInfo;
00098         if (!is_array($imageInfo)) {
00099             throw new Tx_Fluid_Core_ViewHelper_Exception('Could not get image resource for "' . htmlspecialchars($src) . '".' , 1277367645);
00100         }
00101         $imageInfo[3] = t3lib_div::png_to_gif_by_imagemagick($imageInfo[3]);
00102         $GLOBALS['TSFE']->imagesOnPage[] = $imageInfo[3];
00103 
00104         $imageSource = $GLOBALS['TSFE']->absRefPrefix . t3lib_div::rawUrlEncodeFP($imageInfo[3]);
00105         if (TYPO3_MODE === 'BE') {
00106             $imageSource = '../' . $imageSource;
00107             $this->resetFrontendEnvironment();
00108         }
00109 
00110         return $imageSource;
00111     }
00112 
00113     /**
00114      * Prepares $GLOBALS['TSFE'] for Backend mode
00115      * This somewhat hacky work around is currently needed because the getImgResource() function of tslib_cObj relies on those variables to be set
00116      *
00117      * @return void
00118      * @author Bastian Waidelich <bastian@typo3.org>
00119      */
00120     protected function simulateFrontendEnvironment() {
00121         $this->tsfeBackup = isset($GLOBALS['TSFE']) ? $GLOBALS['TSFE'] : NULL;
00122             // set the working directory to the site root
00123         $this->workingDirectoryBackup = getcwd();
00124         chdir(PATH_site);
00125 
00126         $typoScriptSetup = $this->configurationManager->getConfiguration(Tx_Extbase_Configuration_ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
00127         $GLOBALS['TSFE'] = new stdClass();
00128         $template = t3lib_div::makeInstance('t3lib_TStemplate');
00129         $template->tt_track = 0;
00130         $template->init();
00131         $template->getFileName_backPath = PATH_site;
00132         $GLOBALS['TSFE']->tmpl = $template;
00133         $GLOBALS['TSFE']->tmpl->setup = $typoScriptSetup;
00134         $GLOBALS['TSFE']->config = $typoScriptSetup;
00135     }
00136 
00137     /**
00138      * Resets $GLOBALS['TSFE'] if it was previously changed by simulateFrontendEnvironment()
00139      *
00140      * @return void
00141      * @author Bastian Waidelich <bastian@typo3.org>
00142      * @see simulateFrontendEnvironment()
00143      */
00144     protected function resetFrontendEnvironment() {
00145         $GLOBALS['TSFE'] = $this->tsfeBackup;
00146         chdir($this->workingDirectoryBackup);
00147     }
00148 
00149 }