9

This question already has an answer here:

I am sending product data as response for the rest api call, I want to get product Image Url, currently using this:

$imagehelper = $objectManager->create('Magento\Catalog\Helper\Image');
$image = $imagehelper->init($product, 'category_page_list')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(400)->getUrl();

but getting following URL :

http://XXXX.com/pub/static/webapi_rest/_view/en_US/Magento_Catalog/images/product/placeholder/.jpg

Not getting any image .

Thanks.

referring the following Question : Here

marked as duplicate by Fabian Schmengler, Raphael at Digital Pianism, Amit Bera, 7ochem, Qaisar Satti Aug 17 '16 at 11:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I am sending product data as response for the rest api call, I want to get product Image Url in our model or block file not in template. So the question is different from the mentioned duplicate question...thanks. – Manish Jan 20 '17 at 11:12
13

You can get product image url using this code:

$productImageUrl = $block->getUrl('pub/media/catalog').'product'.$_product->getImage();
5

Try it

$store = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore();
$imageUrl = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . $product->getImage();
  • Since I didn't have $objectManager, I defined it as \Magento\Framework\App\ObjectManager::getInstance(). But the rest of the things worked like a charm. Thanks. – Ricardo Martins Jun 7 '17 at 6:28
3

you can get product image url using below code:

    $imageBlock =  $block->getLayout()->createBlock('Magento\Catalog\Block\Product\ListProduct');
//get product object...
    $productImage = $imageBlock->getImage($product, 'category_page_list');
    <a href="<?php echo $product->getProductUrl(); ?>"><?php echo $productImage->toHtml()  ?></a>

you can get product image with product url. Thanks.

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