Contao\Image::getHtml PHP Method

getHtml() public static method

Generate an image tag and return it as string
public static getHtml ( string $src, string $alt = '', string $attributes = '' ) : string
$src string The image path
$alt string An optional alt attribute
$attributes string A string of other attributes
return string The image HTML tag
    public static function getHtml($src, $alt = '', $attributes = '')
    {
        $src = static::getPath($src);
        if ($src == '') {
            return '';
        }
        if (!is_file(TL_ROOT . '/' . $src)) {
            // Handle public bundle resources
            if (file_exists(TL_ROOT . '/web/' . $src)) {
                $src = 'web/' . $src;
            } else {
                return '';
            }
        }
        $objFile = new \File($src);
        // Strip the web/ prefix (see #337)
        if (strncmp($src, 'web/', 4) === 0) {
            $src = substr($src, 4);
        }
        $static = strncmp($src, 'assets/', 7) === 0 ? TL_ASSETS_URL : TL_FILES_URL;
        return '<img src="' . $static . \System::urlEncode($src) . '" width="' . $objFile->width . '" height="' . $objFile->height . '" alt="' . \StringUtil::specialchars($alt) . '"' . ($attributes != '' ? ' ' . $attributes : '') . '>';
    }

Usage Example

示例#1
0
 /**
  * @param $row
  * @param $href
  * @param $label
  * @param $title
  * @param $icon
  * @param $attributes
  * @return string
  */
 public function toggleIcon($row, $href, $label, $title, $icon, $attributes)
 {
     if (strlen(Input::get('tid'))) {
         $this->toggleVisibility(Input::get('tid'), Input::get('state') == 1, @func_get_arg(12) ?: null);
         $this->redirect($this->getReferer());
     }
     // Check permissions AFTER checking the tid, so hacking attempts are logged
     if (!$this->User->hasAccess('tl_api_client::disable', 'alexf')) {
         return '';
     }
     $href .= '&amp;tid=' . $row['id'] . '&amp;state=' . $row['disable'];
     if ($row['disable']) {
         $icon = 'invisible.gif';
     }
     return '<a href="' . $this->addToUrl($href) . '" title="' . specialchars($title) . '"' . $attributes . '>' . Image::getHtml($icon, $label, 'data-state="' . ($row['disable'] ? 0 : 1) . '"') . '</a> ';
 }
All Usage Examples Of Contao\Image::getHtml