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.
<?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
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?
Feel free to share the solution with Magento Community via social media.
Thank You.