In this post I am going to explain how to get logo height, width, image url and alt text in Magento 2.
1. Using Dependency Injection
Add below code snippet in Block class.
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 width
public function getLogoWidth()
{
return $this->_logo->getLogoWidth();
}
// Get logo image height
public function getLogoHeight()
{
return $this->_logo->getLogoHeight();
}
// Get logo image url
public function getLogoImageUrl()
{
return $this->_logo->getLogoSrc();
}
// Get logo image alt text
public function getLogoAltText()
{
return $this->_logo->getLogoAlt();
}
Add below code snippet in template file.
// Get logo image width echo $block->getLogoWidth() . '<br />'; // Get logo image height echo $block->getLogoHeight() . '<br />'; // Get logo image url echo $block->getLogoImageUrl() . '<br />'; // Get logo image alt text echo $block->getLogoAltText() . '<br />';
2. Using Object Manager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$logo = $objectManager->get('\Magento\Theme\Block\Html\Header\Logo');
// Get logo image width
echo $logo->getLogoWidth() . '<br />';
// Get logo image height
echo $logo->getLogoHeight() . '<br />';
// Get logo image url
echo $logo->getLogoSrc() . '<br />';
// Get logo image alt text
echo $logo->getLogoAlt() . '<br />';Thats it. Enjoy Magento 2!!
Write an article about ecommerce that help people to grow their ecommerce business. You’ll find best ecommerce guide, news, tips & more!


Leave a Reply