How to get Logo url, Image Url, Alt text, Logo size in Magento 2 Programmatically

There are the steps to get logo url, image url, Alt text and logo size in magento 2

  • create the block class
  • Declare function in template (.phtml) file

Create the block class

Directory

app/code/VendorName/ModuleName/Block/Demo.php

Content of Demo.php

<?php
namespace VendorName\ModuleName\Block;
class Demo 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();
}
}
?>

Declare function in template (.phtml) file

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

 

If you want any kind of help please visit our Magento 2 Development Service and get a Quote.