Ich habe die Lösung gefunden:
Man kann weitere Twig filter registrieren und diese können dann die Contao Image-Klasse verwenden, z.B. so:
PHP-Code:
// Register additional filters for Avisota twig templates
$GLOBALS['TL_HOOKS']['initializeTwig'][] = array('ExtendedTwig', 'hookInitializeTwig');
if(!class_exists('ExtendedTwig')) {
class ExtendedTwig {
public function hookInitializeTwig(ContaoTwig $contaoTwig) {
/** @var Twig_Environment $twig */
$twig = $contaoTwig->getEnvironment();
// add a new image resize filter
$twig->addFilter(new \Twig_SimpleFilter('resize', function($imagePath, $width, $height, $mode='proportional') {
return \Image::get($imagePath, $width, $height, $mode);
}));
// methods to retrieve the image width and height
$twig->addFilter(new \Twig_SimpleFilter('width', function($imagePath) {
if(substr($imagePath, 0, strlen(TL_ROOT)) != TL_ROOT) {
// the TL_ROOT in fron is missing, add it
$imagePath = TL_ROOT . (substr($imagePath, 0, 1)=='/' ? '' : '/') . $imagePath;
}
list($width, $height, $type, $attr) = getimagesize($imagePath);
return $width;
}));
$twig->addFilter(new \Twig_SimpleFilter('height', function($imagePath) {
if(substr($imagePath, 0, strlen(TL_ROOT)) != TL_ROOT) {
// the TL_ROOT in fron is missing, add it
$imagePath = TL_ROOT . (substr($imagePath, 0, 1)=='/' ? '' : '/') . $imagePath;
}
list($width, $height, $type, $attr) = getimagesize($imagePath);
return $height;
}));
}
}
}
Beispiel-Nutzung im Template:
HTML-Code:
><img src="{{myImage|resize(288,500)}}" width="{{myImage|resize(288,500)|width}}" height="{{myImage|resize(288,500)|height}}" alt="Lala" />
Liebe Grüße,
Roland
Lesezeichen