2

I've written the following function

public function getImg($sku){
    $product=$this->productRepository->get($sku);
    $image=$product->getImage();

    return $image;
}

as an api method and it returns me the name of the image. how can I get an image to be used and viewed in mobile app by rest api

Hitesh
  • 1,569
  • 1
  • 10
  • 27
Yomna Mansour
  • 593
  • 7
  • 23

1 Answers1

2

You need to get media url first then concat this before image url. You can get media url from below code

public function __construct(
    \Magento\Store\Model\StoreManagerInterface $storeManager

) {
    $this->storeManager = $storeManager;
}
public function getMediaUrl() {
   $mediaUrl = $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
}

Let me know if you need further assistance.

Ramkishan Suthar
  • 3,972
  • 5
  • 32
  • 56