I would like to display more related products of a product in my Magento 2 product page. with the following code, I can successfully get part of the path of a product image.

$related_products = $objectManager->get('Magento\Catalog\Model\ResourceModel\Product\Collection')
                   ->addAttributeToFilter('sku', array('like' => substr($_product->getSku(),0,8) .'%'))

foreach($replated_products as $relatedproduct){
   echo $relatedproduct->getImage();

this code will output something like /2/0/2050-101-9884.jpg, But how can I display it as a image? I haven't been able to find out how to get the full image path.

2 Answers 2


You can use full image URL as below.

$store = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore();
$imageUrl = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . $relatedproduct->getImage();

To get thumbnail URL

$imageHelper  = $objectManager->get('\Magento\Catalog\Helper\Image');
$image_url = $imageHelper->init($relatedproduct, 'product_page_image_small')->setImageFile($relatedproduct->getFile())->resize($imagewidth, $imageheight)->getUrl();
  • Great, I got it. What if I want to get the thumbnail image of a product? Commented Feb 16, 2018 at 9:53
  • Thanks, I accepted your awanwer because it helped my question. But regarding the thumbnail image, your code got me this path Magento_Catalog/images/product/placeholder/thumbnail.jpg, just a placeholder. I couldn't get the actual thumbnail of the product. Commented Feb 16, 2018 at 10:33
  • Did you set thumbnail image for those products ? Commented Feb 16, 2018 at 10:41
  • I did set a thumbnail image. So far I used your first method to get the product image, and then resized it by width and height attributes in ima tag. I think it's ok for now Commented Feb 16, 2018 at 11:00

Try this,

$productHelper = $objectManager->get('Magento\Catalog\Helper\Product')
foreach($replated_products as $relatedproduct){
   $thumbnailUrl = $productHelper->getThumbnailUrl(relatedproduct)
   $imageUrl = $productHelper->getImageUrl(relatedproduct)
   $smallImageUrl = $productHelper->getSmallImageUrl(relatedproduct)
  • It got me this one, Magento_Catalog/images/product/placeholder/thumbnail.jpg. A placeholder but not the actual image of the product. Commented Feb 16, 2018 at 10:05

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.