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

media
filesystem

(Dmitry Fedyuk) #1
/**
 * @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);
}

How to get Magento 2 base directory programmatically
How to delete an image from the "media" directory?
How to get a module's filesystem path programmatically
How to create or delete a file in the temporary directory
How to write a file programmatically
Should I consider an image path with or without the «pub» prefix?
(Dmitry Fedyuk) #2

See also other filesystem topics:


(Dmitry Fedyuk) #3

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


(Jean Paul Cahuana Pinto) #4

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