How To

How to Get Product Image URL in Magento 2

Hello Magento Pals ?,

How you all doing? Today’s illustration is on How to Get Product Image URL in Magento 2. In case you missed out on our previous blog, take a look, Magento 2: How To Redirect 404 Page Found To Search Page.

Images are an easy option to improve the user experience of any E-commerce website. It helps to grab the customer’s attention and get them engaged. Every product of an E-commerce website holds different images.

While implementing any new feature related to images you may require a product image URL. Moreover, at the time of migrating your Magento 2 store, you need to get Image URLs of all the products. The below code will help you to Get Product Image URL in Magento 2.

So let’s get started ?

How to Get Product Image URL in Magento 2:

One can retrieve the Image URL using two methods:

Without Object Manager Using Class

protected $_productloader;
protected $_storeManager;
 
public function __construct(
   \Magento\Catalog\Api\ProductRepositoryInterface $productrepository,
               \Magento\Store\Model\StoreManagerInterface $storemanager)
{
         $this->productrepository = $productrepository;
         $this->_storeManager =  $storemanager;
}
Public function getProductImageUsingCode()
{
         $store = $this->_storeManager->getStore();
         $productId = 95;
         $product = $this->productrepository->getById($productid);
 
         $productImageUrl = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' .$product->getImage();
         $productUrl = $product->getProductUrl();
         return $productUrl;
}

Using Object Manager (Not Recommended Method)

$prdId = 35;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Catalog\Api\ProductRepositoryInterface')->getById($productid);
$store = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore();
 
$productImageUrl = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . $product->getImage();
$productUrl  = $product->getProductUrl();

 

Conclusion:

I hope you have successfully applied the above-mentioned steps and you are able to Get Product Image URL in Magento 2. By chance, if you face any difficulties write to me in the comment section below. Do not forget to rate and share the article.

Happy Coding ?

Click to rate this post!
[Total: 40 Average: 3.2]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

2 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

3 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

4 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

6 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago