Most of magento products have more than one product image, i've read this post on how to retrieve product image url, but it seems it only retrieve one image, i want to to load the product images url only by using product id and when i copy the one of images url to browser window the image will show up , is there any chance to do this?


First you need to load a product by Its ID, for this, use below code in your block file : Reference

namespace Vendor\Module\Block;
class ProductImages extends \Magento\Framework\View\Element\Template
    protected $_productRepository;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,       
        \Magento\Catalog\Model\ProductRepository $productRepository,
        array $data = []
        $this->_productRepository = $productRepository;
        parent::__construct($context, $data);

    public function getProductById($id)
        return $this->_productRepository->getById($id);

Then use below in your phtml to display images.

$_productId = 1;
$product = $this->getProductById($_productId);
$images = $product->getMediaGalleryImages();
foreach($images as $child){
    echo $child->getUrl()."<br>"; // Your Images URL
  • when i try to access the image url i got 404:page not found – Shell Suite Apr 12 '17 at 6:16
  • Can you share your image url path here? just only after media folder. – Jaimin Sutariya Apr 12 '17 at 6:28
  • .../catalog/product/w/s/wsh12-green_alt1.jpg – Shell Suite Apr 12 '17 at 6:29
  • The path seems to be correct. It should display you image. Please make sure you have the image available for the product and also check your folder if the image exist at above path. – Jaimin Sutariya Apr 12 '17 at 6:32
  • look up in pub/media/catalog/product folder there is no w folder – Shell Suite Apr 12 '17 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.