How to get product image and URL in Magento 2?
This way may not the best way to get a product image.
Inject \Magento\Catalog\Api\ProductRepositoryInterfaceFactory
in the constructor.
protected $_productRepositoryFactory;
public function __construct(
\Magento\Catalog\Api\ProductRepositoryInterfaceFactory $productRepositoryFactory
) {
$this->_productRepositoryFactory = $productRepositoryFactory;
}
We can get the image:
$product = $this->_productRepositoryFactory->create()
->getById($item->getProductId());
$product->getData('image');
$product->getData('thumbnail');
$product->getData('small_image');
If you need to return a product URL it should look like this:
//todo get product object $product
$objectManager =\Magento\Framework\App\ObjectManager::getInstance();
$helperImport = $objectManager->get('\Magento\Catalog\Helper\Image');
$imageUrl = $helperImport->init($product, 'product_page_image_small')
->setImageFile($product->getSmallImage()) // image,small_image,thumbnail
->resize(380)
->getUrl();
echo $imageUrl;
If you want the published/cache frontend URL of an image for a specific store view (like I did) this is working for me:
/**
* @var \Magento\Store\Model\App\Emulation
*/
protected $appEmulation;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
/**
* @var \Magento\Catalog\Api\ProductRepositoryInterfaceFactory
*/
protected $productRepositoryFactory;
/**
* @var \Magento\Catalog\Helper\ImageFactory
*/
protected $imageHelperFactory;
/**
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Store\Model\App\Emulation $appEmulation
* @param \Magento\Catalog\Api\ProductRepositoryInterfaceFactory $productRepositoryFactory
* @param \Magento\Catalog\Helper\ImageFactory $helperFactory
*/
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Store\Model\App\Emulation $appEmulation,
\Magento\Catalog\Api\ProductRepositoryInterfaceFactory $productRepositoryFactory,
\Magento\Catalog\Helper\ImageFactory $imageHelperFactory
)
{
$this->storeManager = $storeManager;
$this->appEmulation = $appEmulation;
$this->productRepositoryFactory = $productRepositoryFactory;
$this->imageHelperFactory = $imageHelperFactory;
}
Then, wherever you need to get the image frontend URL:
$sku = "my-sku";
// get the store ID from somewhere (maybe a specific store?)
$storeId = $this->storeManager->getStore()->getId();
// emulate the frontend environment
$this->appEmulation->startEnvironmentEmulation($storeId, \Magento\Framework\App\Area::AREA_FRONTEND, true);
// load the product however you want
$product = $this->productRepositoryFactory->create()->get($sku);
// now the image helper will get the correct URL with the frontend environment emulated
$imageUrl = $this->imageHelperFactory->create()
->init($product, 'product_thumbnail_image')->getUrl();
// end emulation
$this->appEmulation->stopEnvironmentEmulation();
You can select other images types besides product_thumbnail_image
: see magento/theme-frontend-luma/etc/view.xml
for a list of available product images, or create your own in a view.xml
file.