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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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.
1 2 3 4 5 6 7 8 9 10 11 | // 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $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!!
Leave a Reply