Get Product Small Image Url Using Product id. You can retrieve small_image Url of the product.
You need to instantiate the ImageFactory to load image objects using Magento\Catalog\Helper\ImageFactory.
Check the code snippet to retrieve Image Url,
<?php namespace Jesadiya\ProductSmallImage\Model; use Exception; use Psr\Log\LoggerInterface; use Magento\Catalog\Helper\ImageFactory; use Magento\Catalog\Api\ProductRepositoryInterface; class ImageUrl { /** * Small image value. * * @var string */ const SMALL_IMAGE = 'small_image'; /** * @var ImageFactory */ public $imageFactory; /** * @var LoggerInterface */ public $logger; public function __construct( ImageFactory $imageFactory, ProductRepositoryInterface $productRepository, LoggerInterface $logger ) { $this->imageFactory = $imageFactory; $this->productRepository = $productRepository; $this->logger = $logger; } /** * Get Image url * * @return string */ public function getImageUrl() { $productId = 1; $imageUrl = null; try { $product = $this->productRepository->getById($productId); if ($product) { $imageHelper = $this->imageFactory->create()->init($product, self::SMALL_IMAGE); $imageUrl = $imageHelper->getUrl(); } } catch (Exception $exception) { $this->logger->error($exception->getMessage()); } return $imageUrl; } }
You can get Product small_image URL by product id by referring the above code.