I have created a widget. I am trying to show product image in widget. I have successfully got product name, product url and image url using object manager. But the Image is not loaded (404 - not found).
public function getCollection()
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create()->setPageSize(1)
->addAttributeToSelect('*')
->load();
return $collection;
}
public function getPImage()
{
$col=$this->getCollection();
foreach ($col as $product)
{
echo $product->getImage();
}
}
getPImage() method returns string "/m/b/mb01-blue-0.jpg"
I use this string in img src
<img style="height:100%;width:100%;" src="<?php echo $this->getViewFileUrl('images/m/b/mb01-blue-0.jpg'); ?>" />
or without images prefix
<img style="height:100%;width:100%;" src="<?php echo $this->getViewFileUrl('/m/b/mb01-blue-0.jpg'); ?>" />
Yet the image not found, broken image URL path. LumaChange is my custom theme.
http://magentodev.gworks.mobi/magento2/pub/static/frontend/LumaChange/Color/en_US/m/b/mb01-blue-0.jpg
when I found specific product by search store, image url look like below(valid)
http://magentodev.gworks.mobi/magento2/pub/media/catalog/product/cache/1/small_image/240x300/beff4985b56e3afdbeabfc89641a4582/m/b/mb01-blue-0.jpg
Page widget div tag looks like below
Any suggestion to overcome this issue?