Contao\Controller::addImageToTemplate PHP Method

addImageToTemplate() public static method

Add an image to a template
public static addImageToTemplate ( object $objTemplate, array $arrItem, integer $intMaxWidth = null, string $strLightboxId = null )
$objTemplate object The template object to add the image to
$arrItem array The element or module as array
$intMaxWidth integer An optional maximum width of the image
$strLightboxId string 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 = \StringUtil::deserialize($arrItem['size']);
        if (is_numeric($size)) {
            $size = array(0, 0, (int) $size);
        } elseif (!is_array($size)) {
            $size = array();
        }
        $size += array(0, 0, 'crop');
        if ($intMaxWidth === null) {
            $intMaxWidth = TL_MODE == 'BE' ? 320 : \Config::get('maxImageWidth');
        }
        $arrMargin = TL_MODE == 'BE' ? array() : \StringUtil::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 = \System::getContainer()->get('contao.image.image_factory')->create(TL_ROOT . '/' . $arrItem['singleSRC'], $size)->getUrl(TL_ROOT);
            $picture = \System::getContainer()->get('contao.image.picture_factory')->create(TL_ROOT . '/' . $arrItem['singleSRC'], $size);
            $picture = array('img' => $picture->getImg(TL_ROOT), 'sources' => $picture->getSources(TL_ROOT));
            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'] = \StringUtil::specialchars($arrItem['alt']);
        // Only add the title if the image is not part of an image link
        if (empty($arrItem['imageUrl']) && empty($arrItem['fullsize'])) {
            $picture['title'] = \StringUtil::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 = \StringUtil::specialchars($arrItem['alt']);
        $objTemplate->title = \StringUtil::specialchars($arrItem['title']);
        $objTemplate->linkTitle = \StringUtil::specialchars($arrItem['linkTitle'] ?: $arrItem['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;
    }

Usage Example

Beispiel #1
0
 /**
  * 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)
 {
     if (stristr($arrItem['singleSRC'], '.svg') !== FALSE) {
         $size = deserialize($arrItem['size']);
         $size = self::_getSvgSize($arrItem['singleSRC'], $size);
         $arrItem['size'] = serialize($size);
         @parent::addImageToTemplate($objTemplate, $arrItem, $intMaxWidth, $strLightboxId);
         $imgSize = '';
         $imgSize .= ' width="' . $size[0] . '"';
         $imgSize .= ' height="' . $size[1] . '"';
         $objTemplate->imgSize = $imgSize;
         $objTemplate->svgImage = TRUE;
         if ($width > 0 && $height > 0) {
             $objTemplate->width = $imgSize[0];
             $objTemplate->height = $imgSize[1];
         }
     } else {
         parent::addImageToTemplate($objTemplate, $arrItem, $intMaxWidth, $strLightboxId);
     }
 }
All Usage Examples Of Contao\Controller::addImageToTemplate