How to get product image url in Magento 2?

In the e-Commerce Site, Every Product has associated with unique images like Small Image, Base Image, thumbnail images.

Many times in custom development we need to required get product URL in Magento 2. If you are developing some features related to images and want to retrieve the image URL, You can achieve the image URL by referring to this article.

You can fetch all the media gallery by link, Gallery Images Collection. We can get product URL using Magento way by just simple way,

public function __construct(
    \Magento\Catalog\Helper\Image $imageHelper,
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
) {
    $this->imageHelper = $imageHelper;
    $this->productRepository = $productRepository;

 * @param int $id
 * @return string
public function getItemImage($productId)
    try {
        $_product = $this->productRepository->getById($productId);
    } catch (NoSuchEntityException $e) {
        return 'product not found';
    $image_url = $this->imageHelper->init($_product, 'product_base_image')->getUrl();
    return $image_url;

You can call any image type at the above function in the second argument, There are many default image_types available.
Few of Example,
product_page_image_large (700×700)
product_page_image_medium (700×700)

You can get image type from vendor/magento/theme-frontend-blank/etc/view.xml as per your requirements.

In phtml file call like this, by just pass product id to get an image of the specific product,

$productId = 10;
$imgUrl = $this->getItemImage($productId);
<div class="image-box"><img src="<?= $imgUrl; ?>" alt="product-image"/></div>

Image URL with HeightxWidth equals 265×265 got as a response to the above example.

You can read the article Resize Images and Retrieve the Placeholder of the image by Programmatically fetch the placeholder image.