How to Get Logo URL, Alt Text, Logo Height and Width in Magento 2
A logo is the visual representation of your brand or store. It can communicate a message about who you are, what you do, and what your values are!
After installing Magento 2, one of the essential things you need to do is change the logo on the website.
When we upload or change a logo in our store, it sets logo-related information like logo size, logo URL, alt text and saved in various file formats such as PNG, GIF, JPG, or JPEG.
While designing our Magento 2 store, we often need a perfect-sized logo that matches the size of our web page. For instance, the size of a logo must enlarge when our site transforms to the larger screen and reduce when the store opens on the mobile screen. To craft a logo of perfect dimension, we recommend using an image resizer tool.
In that scenario, we have to dynamically get logo URL, alt text, logo height and width in Magento 2 to compare the updated logo with the existing one and validate logo information.
Get the logo information dynamically and use it in further operations like comparing, validating logo details, designing purpose, etc. using the below solution:
Method to Get Logo URL, Alt Text, Logo Height and Width in Magento 2
Use the below code in your block file.
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 36 37 38 39 40 41 |
<?php namespace Vendor\Module\Block; class LogoDetail 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); } public function getLogoWidth() { return $this->logo->getLogoWidth(); // To get Logo image width } public function getLogoHeight() { return $this->logo->getLogoHeight(); // To get Logo image height } public function getLogoSrc() { return $this->logo->getLogoSrc(); // To get Url of Logo image } public function getLogoAlt() { return $this->_logo->getLogoAlt(); // To get Url of Logo image alternet text } } ?> |
You can get all block functions by printing like below in your .phtml file
1 2 3 4 |
echo $block->getLogoWidth();//Print logo Width echo $block->getLogoHeight();//Print logo Height echo $block->getLogoSrc();//Print logo path echo $block->getLogoAlt();//Print logo alt text |
That’s easy, right?
If you have any doubts regarding this post, just mention them in the Comments section below.
I would be happy to help.
Feel free to share the solution with Magento Community via social media.
Thank You.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Create a Tab and Load Grid in Magento 2 Customer Admin Edit Page
Meetanshi Magento Extensions Launches and Updates April[2021]
Next