How to get logo url, alt text, logo size in Magento 2

Logo URL, ALT Text, and Logo Size (include height and width) are the basic information you totally get when the logo is applied to your store. In specific, the logo URL is the direct link that will show the page whenever there is any person clicking on the logo. And the logo size is the height and width of the logo image. What about the ALT Text? That is the content which is visible in the logo. Those parameters are open for you to get via this tutorial article if you are running Magento 2 platform on the store.

Overview

  • Step 1: Declare in Mageplaza_HelloWorld
  • Step 2: Declare function in template (.phtml) file

Step 1: Declare in Mageplaza_HelloWorld

You will use a block class of the module Mageplaza_HelloWorld, then possibly inject the object of Logo class in the constructor of the module’s block class.

app/code/Mageplaza/HelloWorld/Block/HelloWorld.php

<?php
namespace Mageplaza\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{
    protected $_logo;    
    
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Theme\Block\Html\Header\Logo $logo,
        array $data = []
    )
    {        
        $this->_logo = $logo;
        parent::__construct($context, $data);
    }
    
    /**
     * Get logo image URL
     *
     * @return string
     */
    public function getLogoSrc()
    {    
        return $this->_logo->getLogoSrc();
    }
    
    /**
     * Get logo text
     *
     * @return string
     */
    public function getLogoAlt()
    {    
        return $this->_logo->getLogoAlt();
    }
    
    /**
     * Get logo width
     *
     * @return int
     */
    public function getLogoWidth()
    {    
        return $this->_logo->getLogoWidth();
    }
    
    /**
     * Get logo height
     *
     * @return int
     */
    public function getLogoHeight()
    {    
        return $this->_logo->getLogoHeight();
    }    
}
?>

You can see more functions in vendor/magento/module-theme/Block/Html/Header/Logo.php.

Step 2: Declare function in template (.phtml) file

Run the below function in your template (.phtml) file

echo $block->getLogoSrc() . '<br />';
echo $block->getLogoAlt() . '<br />';
echo $block->getLogoWidth() . '<br />';
echo $block->getLogoHeight() . '<br />';

You can see more functions in vendor/magento/module-theme/Block/Html/Header/Logo.php.

The topic How to get logo url, alt text, and logo size in magento 2 ends here. I hope this is the helpful article when you want to fetch the logo information. In case that you have any queries about the article or any questions in general, use the comment section below!



Next tutorial:

Module Development Series



Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
get
logo-url
alt-text
logo-size
logo-height-width

Module Development

Comments for How to get logo url, alt text, logo size in Magento 2

Please leave comments if you have any questions, feedbacks.

You also may like these Magento 2 Extensions

One Step Checkout

$199
10 reviews

Layered Navigation

$99
12 reviews

SEO

FREE
no review

Shop By Brand

$99
3 reviews

People also searched for:

  • magento 2 get logo url alt text logo size
  • get logo url alt text logo size in magento 2
  • magento 2 product logo url alt text logo size
  • magento 2 get logo url alt text logo height and width
  • how to get magento 2 logo url alt text logo size
  • how to get logo url alt text logo size in magento 2