How to get an absolute media (image) path programmatically by a relative path

/**
 * @param string $relativeMediaPath
 * @return string
 */
function getAbsoluteMediaPath($relativeMediaPath) {
	/** @var \Magento\Framework\App\ObjectManager $om */
	$om = \Magento\Framework\App\ObjectManager::getInstance();
	/** @var \Magento\Framework\Filesystem $filesystem */
	$filesystem = $om->get('Magento\Framework\Filesystem');
	/** @var \Magento\Framework\Filesystem\Directory\ReadInterface|\Magento\Framework\Filesystem\Directory\Read $reader */
	$reader = $filesystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
	return $reader->getAbsolutePath($relativeMediaPath);
}

See also other filesystem topics:

A post was split to a new topic: Is «catalog/product/cache/1/image/265*265» a full filesystem path?

Is it posible to get an absolute path of cached product image?

1 Like