[TYPO3-mvc] Uri.ImageViewHelper
Christian Baer
chr.baer at googlemail.com
Mon Jun 14 14:23:06 CEST 2010
Hi everybody,
I implemented a ViewHelper that works like ImageViewHelper, but just
renders the URI, not the whole <img>-Tag. I just copied the
ImageViewHelper and return the imageResource.
Any ideas where to put it in forge? Haven't done this yet.
Cheers,
Christian
<?php
/* *
* This script is part of the TYPO3 project - inspiring people to share! *
* *
* TYPO3 is free software; you can redistribute it and/or modify it under *
* the terms of the GNU General Public License version 2 as published by *
* the Free Software Foundation. *
* *
* This script is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
* TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
* Public License for more details. *
*
*/
/*
* renders a Image according to the given dimensions
*
* @return relative path to the image (typo3temp/...)
*/
class Tx_MyExtension_ViewHelpers_Uri_ImageViewHelper extends
Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
/**
* @var tslib_cObj
*/
protected $contentObject;
/**
* Constructor. Used to create an instance of tslib_cObj used by the
render() method.
*
* @param tslib_cObj $contentObject injector for tslib_cObj (optional)
* @return void
*/
public function __construct($contentObject = NULL) {
$this->contentObject = $contentObject !== NULL ? $contentObject :
t3lib_div::makeInstance('tslib_cObj');
if (TYPO3_MODE === 'BE') {
throw new Tx_Fluid_Core_ViewHelper_Exception('ImageViewHelper does
not (yet) work in backend mode' , 1253191784);
}
}
/**
* Render the img tag.
* @see
http://typo3.org/documentation/document-library/references/doc_core_tsref/4.2.0/view/1/5/#id4164427
*
* @param string $src
* @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.
* @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.
* @param integer $minWidth minimum width of the image
* @param integer $minHeight minimum height of the image
* @param integer $maxWidth maximum width of the image
* @param integer $maxHeight maximum height of the image
*
* @return string path to the image (typo3temp/...)
* @author Sebastian Böttger <sboettger at cross-content.com>
* @author Bastian Waidelich <bastian at typo3.org>
* @author Christian Baer <chr.baer at googlemail.com>
*/
public function render($src, $width = NULL, $height = NULL, $minWidth =
NULL, $minHeight = NULL, $maxWidth = NULL, $maxHeight = NULL) {
$setup = array(
'width' => $width,
'height' => $height,
'minW' => $minWidth,
'minH' => $minHeight,
'maxW' => $maxWidth,
'maxH' => $maxHeight
);
$imageInfo = $this->contentObject->getImgResource($src, $setup);
$GLOBALS['TSFE']->lastImageInfo = $imageInfo;
if (!is_array($imageInfo)) {
throw new Tx_Fluid_Core_ViewHelper_Exception('Could not get image
resource for "' . htmlspecialchars($src) . '".' , 1253191060);
}
$imageInfo[3] = t3lib_div::png_to_gif_by_imagemagick($imageInfo[3]);
$GLOBALS['TSFE']->imagesOnPage[] = $imageInfo[3];
$imageSource = $GLOBALS['TSFE']->absRefPrefix .
t3lib_div::rawUrlEncodeFP($imageInfo[3]);
return $imageSource;
}
}
More information about the TYPO3-project-typo3v4mvc
mailing list