I am not able to get the absolute path of the image by product id in Magento 2. Can anybody help me?

I am able to get media path so far by directoryList class by doing this:


How to get product image full path?

Please note that I don't want an image URL, I want the image path directory.

  • Please upload your file code. which you tried to get image. Jan 1, 2019 at 8:38

2 Answers 2


You can Product image path from Magento\Catalog\Api\ProductAttributeMediaGalleryManagementInterface If you have sku then from this interface's getList() you get media image.

from media image you get image path.

 * User: amit-pc
 * Date: 1/1/19

namespace Devbera\Test;

class Test

     * @var \Magento\Catalog\Api\ProductAttributeMediaGalleryManagementInterface
    private $productAttributeMediaGalleryManagement;

    public function __construct(
        \Magento\Catalog\Api\ProductAttributeMediaGalleryManagementInterface $productAttributeMediaGalleryManagement

        $this->productAttributeMediaGalleryManagement = $productAttributeMediaGalleryManagement;
    public function getMediaPath()
        $media = $this->productAttributeMediaGalleryManagement->getList($sku = 'ab2');
        foreach ($media as $image)
            print_r(['value_id' => $image->getId(),
            'file' => $image->getFile(),
            'label' => $image->getLabel(),
            'position' => $image->getPosition(),
            'disabled' => $image->isDisabled(),
            'types' => $image->getTypes(),
            'media_type' => $image->getMediaType()
            echo $image->getFile();

try the below code

     $itemsid = 2;
     $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $product = $objectManager->create('Magento\Catalog\Model\Product')->load($itemsid);
        echo $imageUrl = $this->helper('Magento\Catalog\Helper\Image')
                                                    ->init($product, 'product_base_image')
                                                    ->resize(150, 150)->getUrl();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.