Contao\Image::getPath PHP Method

getPath() public static method

Get the relative path to an image
public static getPath ( string $src ) : string
$src string The image name or path
return string The relative path
    public static function getPath($src)
    {
        if ($src == '') {
            return '';
        }
        $src = rawurldecode($src);
        if (strpos($src, '/') !== false) {
            return $src;
        }
        if (strncmp($src, 'icon', 4) === 0) {
            if (pathinfo($src, PATHINFO_EXTENSION) == 'svg') {
                return 'assets/contao/images/' . $src;
            }
            $filename = pathinfo($src, PATHINFO_FILENAME);
            // Prefer SVG icons
            if (file_exists(TL_ROOT . '/assets/contao/images/' . $filename . '.svg')) {
                return 'assets/contao/images/' . $filename . '.svg';
            }
            return 'assets/contao/images/' . $src;
        } else {
            $theme = \Backend::getTheme();
            if (pathinfo($src, PATHINFO_EXTENSION) == 'svg') {
                return 'system/themes/' . $theme . '/icons/' . $src;
            }
            $filename = pathinfo($src, PATHINFO_FILENAME);
            // Prefer SVG icons
            if (file_exists(TL_ROOT . '/system/themes/' . $theme . '/icons/' . $filename . '.svg')) {
                return 'system/themes/' . $theme . '/icons/' . $filename . '.svg';
            }
            return 'system/themes/' . $theme . '/images/' . $src;
        }
    }

Usage Example

 /**
  * Compile global buttons from the table configuration array and return them as HTML
  *
  * @return string
  */
 protected function generateGlobalButtons()
 {
     if (!is_array($GLOBALS['TL_DCA'][$this->strTable]['list']['global_operations'])) {
         return '';
     }
     $return = '';
     foreach ($GLOBALS['TL_DCA'][$this->strTable]['list']['global_operations'] as $k => $v) {
         if (\Input::get('act') == 'select' && !$v['showOnSelect']) {
             continue;
         }
         $v = is_array($v) ? $v : array($v);
         $label = is_array($v['label']) ? $v['label'][0] : $v['label'];
         $title = is_array($v['label']) ? $v['label'][1] : $v['label'];
         $attributes = $v['attributes'] != '' ? ' ' . ltrim($v['attributes']) : '';
         // Custom icon (see #5541)
         if ($v['icon']) {
             $v['class'] = trim($v['class'] . ' header_icon');
             // Add the theme path if only the file name is given
             if (strpos($v['icon'], '/') === false) {
                 $v['icon'] = \Image::getPath($v['icon']);
             }
             $attributes = sprintf('style="background-image:url(\'%s%s\')"', TL_ASSETS_URL, $v['icon']) . $attributes;
         }
         if ($label == '') {
             $label = $k;
         }
         if ($title == '') {
             $title = $label;
         }
         // Call a custom function instead of using the default button
         if (is_array($v['button_callback'])) {
             $this->import($v['button_callback'][0]);
             $return .= $this->{$v['button_callback'][0]}->{$v['button_callback'][1]}($v['href'], $label, $title, $v['class'], $attributes, $this->strTable, $this->root);
             continue;
         } elseif (is_callable($v['button_callback'])) {
             $return .= $v['button_callback']($v['href'], $label, $title, $v['class'], $attributes, $this->strTable, $this->root);
             continue;
         }
         $return .= '<a href="' . $this->addToUrl($v['href']) . '" class="' . $v['class'] . '" title="' . \StringUtil::specialchars($title) . '"' . $attributes . '>' . $label . '</a> ';
     }
     return $return;
 }
All Usage Examples Of Contao\Image::getPath