0

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

I am able to get media path so far by directoryList class by doing this $this->directory->getPath('media'); how to get product image full path. please note that i dont want image url i want directory image path.

  • Please upload your file code. which you tried to get image. – Rohan Hapani Jan 1 at 8:38
0

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.

<?php
/**
 * 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();
        }
    }
}
0

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')
                                                    ->constrainOnly(TRUE)
                                                    ->keepAspectRatio(TRUE)
                                                    ->keepTransparency(TRUE)
                                                    ->keepFrame(FALSE)
                                                    ->resize(150, 150)->getUrl();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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