
How to get product image URL in Magento 2?
Images allow customers to pay more attention and when it comes to an eCommerce store, showing product images to customers gives then more confident and it is a must do activity for an online store. In today’s blog we will illustrate how you can fetch product image url in Magento 2.
<?php
namespace WebbyTroops\Product\Block;
class ProductImage
{
/**
*
* @var \Magento\Catalog\Api\ProductRepositoryInterfaceFactory
*/
protected $productRepositoryFactory;
/**
*
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
/**
*
* @param \Magento\Authorization\Model\CompositeUserContext $userContext
* @param \Magento\Store\Model\StoreManagerInterface $storemanager
*/
public function __construct(
\Magento\Catalog\Api\ProductRepositoryInterfaceFactory $productRepositoryFactory,
\Magento\Store\Model\StoreManagerInterface $storemanager
) {
$this->productRepositoryFactory = $productRepositoryFactory;
$this->storeManager = $storemanager
}
/**
*
* @return int
*/
public function getProductImageUrl()
{
$store = $this->storeManager->getStore();
$productId = 1;
$product = $this->productRepositoryFactory->create()->getById($productId);
// This will give you all images with different size
$product->getData('image');
$product->getData('thumbnail');
$product->getData('small_image');
// This will provide image URL
$productImageUrl = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' .$product->getImage();
return $productImageUrl;
}
/**
* fetch all Product images
*/
public function getAllImages()
{
$productId = 1;
$product = $this->productRepository->create()->getById($productId);
$images = $product->getMediaGalleryEntries();
$allImages = [];
foreach ($images as $image) {
if($image->getMediaType() == 'image') {
$allImages[] = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' .$image->getFile();
}
}
return $allImages;
}
}
If you follow steps as defined above you will be able to get product image and url. If you face any issue please comment below. Happy Coding !!
Related Posts
Leave a Reply Cancel reply
Categories
- Digital Marketing (7)
- eCommerce (34)
- Laravel (1)
- Latest News (5)
- Magento 2 Extensions (5)
- Magento2 (38)
- Shopify (5)
- Shopware (7)
- Wordpress (2)
Recent Posts
Advanced custom links extension (1) auto cancel order (1) banners (1) cancel order (1) cart attributes (1) Customer (7) Custom order invoice number module (1) custom shipping method (1) E-Commerce (7) eCommerce (41) full page cache magento 2 (1) hcaptcha (1) hcaptcha vs recaptcha (1) How to add customer attribute in Magento 2 (1) Laravel 10 (1) Magento2 (34) magento 2 back in stock notification (1) magento 2 cancel order (1) Magento2 Extension (3) magento 2 extensions (1) magento 2 layered navigation (1) magento 2 recaptcha (2) magento 2 robots.txt (1) magento 2.4.6 (1) magento headless commerce (1) new features (1) online store (13) openmaze (1) order management (1) Product (4) product attributes (1) recaptcha (1) search engine optimization (6) seo (10) shipping rule (1) shopify (3) shopware (6) Shopware-plugin (3) Shopware 6 (5) shopware 6 released (1) store switcher (1) structured data (1) upcoming update (1) wordpress (1) wordpress vs shopify (1)