/**
* Add an image to a template
*
* @param object $objTemplate The template object to add the image to
* @param array $arrItem The element or module as array
* @param integer $intMaxWidth An optional maximum width of the image
* @param string $strLightboxId An optional lightbox ID
*/
public static function addImageToTemplate($objTemplate, $arrItem, $intMaxWidth = null, $strLightboxId = null)
{
try {
$objFile = new \File($arrItem['singleSRC']);
} catch (\Exception $e) {
$objFile = new \stdClass();
$objFile->imageSize = false;
}
$imgSize = $objFile->imageSize;
$size = deserialize($arrItem['size']);
if ($intMaxWidth === null) {
$intMaxWidth = TL_MODE == 'BE' ? 320 : \Config::get('maxImageWidth');
}
$arrMargin = TL_MODE == 'BE' ? array() : deserialize($arrItem['imagemargin']);
// Store the original dimensions
$objTemplate->width = $imgSize[0];
$objTemplate->height = $imgSize[1];
// Adjust the image size
if ($intMaxWidth > 0) {
// Subtract the margins before deciding whether to resize (see #6018)
if (is_array($arrMargin) && $arrMargin['unit'] == 'px') {
$intMargin = $arrMargin['left'] + $arrMargin['right'];
// Reset the margin if it exceeds the maximum width (see #7245)
if ($intMaxWidth - $intMargin < 1) {
$arrMargin['left'] = '';
$arrMargin['right'] = '';
} else {
$intMaxWidth = $intMaxWidth - $intMargin;
}
}
if ($size[0] > $intMaxWidth || !$size[0] && !$size[1] && $imgSize[0] > $intMaxWidth) {
// See #2268 (thanks to Thyon)
$ratio = $size[0] && $size[1] ? $size[1] / $size[0] : $imgSize[1] / $imgSize[0];
$size[0] = $intMaxWidth;
$size[1] = floor($intMaxWidth * $ratio);
}
}
// Disable responsive images in the back end (see #7875)
if (TL_MODE == 'BE') {
unset($size[2]);
}
try {
$src = \Image::create($arrItem['singleSRC'], $size)->executeResize()->getResizedPath();
$picture = \Picture::create($arrItem['singleSRC'], $size)->getTemplateData();
if ($src !== $arrItem['singleSRC']) {
$objFile = new \File(rawurldecode($src));
}
} catch (\Exception $e) {
\System::log('Image "' . $arrItem['singleSRC'] . '" could not be processed: ' . $e->getMessage(), __METHOD__, TL_ERROR);
$src = '';
$picture = array('img' => array('src' => '', 'srcset' => ''), 'sources' => array());
}
// Image dimensions
if (($imgSize = $objFile->imageSize) !== false) {
$objTemplate->arrSize = $imgSize;
$objTemplate->imgSize = ' width="' . $imgSize[0] . '" height="' . $imgSize[1] . '"';
}
$picture['alt'] = specialchars($arrItem['alt']);
$picture['title'] = specialchars($arrItem['title']);
$objTemplate->picture = $picture;
// Provide an ID for single lightbox images in HTML5 (see #3742)
if ($strLightboxId === null && $arrItem['fullsize']) {
$strLightboxId = 'lightbox[' . substr(md5($objTemplate->getName() . '_' . $arrItem['id']), 0, 6) . ']';
}
// Float image
if ($arrItem['floating'] != '') {
$objTemplate->floatClass = ' float_' . $arrItem['floating'];
}
// Do not override the "href" key (see #6468)
$strHrefKey = $objTemplate->href != '' ? 'imageHref' : 'href';
// Image link
if ($arrItem['imageUrl'] != '' && TL_MODE == 'FE') {
$objTemplate->{$strHrefKey} = $arrItem['imageUrl'];
$objTemplate->attributes = '';
if ($arrItem['fullsize']) {
// Open images in the lightbox
if (preg_match('/\\.(jpe?g|gif|png)$/', $arrItem['imageUrl'])) {
// Do not add the TL_FILES_URL to external URLs (see #4923)
if (strncmp($arrItem['imageUrl'], 'http://', 7) !== 0 && strncmp($arrItem['imageUrl'], 'https://', 8) !== 0) {
$objTemplate->{$strHrefKey} = TL_FILES_URL . \System::urlEncode($arrItem['imageUrl']);
}
$objTemplate->attributes = ' data-lightbox="' . substr($strLightboxId, 9, -1) . '"';
} else {
$objTemplate->attributes = ' target="_blank"';
}
}
} elseif ($arrItem['fullsize'] && TL_MODE == 'FE') {
$objTemplate->{$strHrefKey} = TL_FILES_URL . \System::urlEncode($arrItem['singleSRC']);
$objTemplate->attributes = ' data-lightbox="' . substr($strLightboxId, 9, -1) . '"';
}
// Do not urlEncode() here because getImage() already does (see #3817)
$objTemplate->src = TL_FILES_URL . $src;
$objTemplate->alt = specialchars($arrItem['alt']);
$objTemplate->title = specialchars($arrItem['title']);
$objTemplate->linkTitle = $objTemplate->title;
$objTemplate->fullsize = $arrItem['fullsize'] ? true : false;
$objTemplate->addBefore = $arrItem['floating'] != 'below';
$objTemplate->margin = static::generateMargin($arrMargin);
$objTemplate->caption = $arrItem['caption'];
$objTemplate->singleSRC = $arrItem['singleSRC'];
$objTemplate->addImage = true;
}