PMA\libraries\Util::getImage PHP Method

getImage() public static method

Returns an HTML IMG tag for a particular image from a theme, which may be an actual file or an icon from a sprite
public static getImage ( string $image, string $alternate = '', array $attributes = [] ) : string
$image string The name of the file to get
$alternate string Used to set 'alt' and 'title' attributes of the image
$attributes array An associative array of other attributes
return string an html IMG tag
    public static function getImage($image, $alternate = '', $attributes = array())
    {
        static $sprites;
        // cached list of available sprites (if any)
        if (defined('TESTSUITE')) {
            // prevent caching in testsuite
            unset($sprites);
        }
        $is_sprite = false;
        $alternate = htmlspecialchars($alternate);
        // If it's the first time this function is called
        if (!isset($sprites)) {
            $sprites = array();
            // Try to load the list of sprites
            if (isset($_SESSION['PMA_Theme'])) {
                $sprites = $_SESSION['PMA_Theme']->getSpriteData();
            }
        }
        // Check if we have the requested image as a sprite
        //  and set $url accordingly
        $class = str_replace(array('.gif', '.png'), '', $image);
        if (array_key_exists($class, $sprites)) {
            $is_sprite = true;
            $url = (defined('PMA_TEST_THEME') ? '../' : '') . 'themes/dot.gif';
        } elseif (isset($GLOBALS['pmaThemeImage'])) {
            $url = $GLOBALS['pmaThemeImage'] . $image;
        } else {
            $url = './themes/pmahomme/' . $image;
        }
        // set class attribute
        if ($is_sprite) {
            if (isset($attributes['class'])) {
                $attributes['class'] = "icon ic_{$class} " . $attributes['class'];
            } else {
                $attributes['class'] = "icon ic_{$class}";
            }
        }
        // set all other attributes
        $attr_str = '';
        foreach ($attributes as $key => $value) {
            if (!in_array($key, array('alt', 'title'))) {
                $attr_str .= " {$key}=\"{$value}\"";
            }
        }
        // override the alt attribute
        if (isset($attributes['alt'])) {
            $alt = $attributes['alt'];
        } else {
            $alt = $alternate;
        }
        // override the title attribute
        if (isset($attributes['title'])) {
            $title = $attributes['title'];
        } else {
            $title = $alternate;
        }
        // generate the IMG tag
        $template = '<img src="%s" title="%s" alt="%s"%s />';
        $retval = sprintf($template, $url, $title, $alt, $attr_str);
        return $retval;
    }

Usage Example

Example #1
0
 /**
  * Initialises the class
  *
  * @param string $name     An identifier for the new node
  * @param int    $type     Type of node, may be one of CONTAINER or OBJECT
  * @param bool   $is_group Whether this object has been created
  *                         while grouping nodes
  */
 public function __construct($name, $type = Node::OBJECT, $is_group = false)
 {
     parent::__construct($name, $type, $is_group);
     $this->icon = PMA\libraries\Util::getImage('b_events.png');
     $this->links = array('text' => 'db_events.php?server=' . $GLOBALS['server'] . '&amp;db=%2$s&amp;item_name=%1$s&amp;edit_item=1', 'icon' => 'db_events.php?server=' . $GLOBALS['server'] . '&amp;db=%2$s&amp;item_name=%1$s&amp;export_item=1');
     $this->classes = 'event';
 }
All Usage Examples Of PMA\libraries\Util::getImage
Util