1

How to get all images of a specific product in Magento 2?

4
  • You just want to dump all the images somewhere in the web? Be more specific please. :D
    – Raül
    Aug 8, 2018 at 11:38
  • In which file you want to get all Images? Aug 8, 2018 at 11:53
  • 1
    $product->getMediaGalleryEntries() Aug 8, 2018 at 13:52
  • $product->getMediaGalleryEntries() returns all files by ->getFile() saved in a product gallery, including hidden images.
    – MSQ
    Mar 4, 2020 at 9:45

5 Answers 5

10
<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->create('Magento\Catalog\Model\Product')->load($_product->getId());        
    $images = $product->getMediaGalleryImages();
    foreach($images as $child){ ?>
        <img src="<?php echo $child->getUrl(); ?>" >
<?php } ?>
3
  • Can I actually do this without objectManager?
    – glafche
    Aug 17, 2018 at 12:20
  • $_product is undefined...
    – lky
    Dec 3, 2018 at 13:32
  • $productid=12; $product = $objectManager->create('Magento\Catalog\Model\Product')->load($productid); You want to give product id. You want to fetch products and pass product ids.
    – P Ramulu
    Dec 4, 2018 at 6:24
2

Your question is too broad. you didn't specify where or how do you want to get images. if you want to show all images in custom slider then try below code:

<ul id="image-gallery" class="gallery list-unstyled cS-hidden">

            <?php $images = $product->getMediaGalleryImages(); ?>
            <?php foreach ($images as $_image) : ?>

                <li data-src="">
                    <a href=""><img alt="Rootways Extension" class="img-responsive" src="<?= $_image->getUrl();?>"></a>
                </li>
            <?php endforeach ?>
</ul>
0
1

You can get all images by below code to default gallery.phtml file

<?php
foreach($block->getGalleryImages() as $image){ ?>
    <img src="<?php echo $image->getData('medium_image_url') ?>">
<?php } 
?>
1
  • but I need only the images of specific product, not all images of all products
    – glafche
    Aug 17, 2018 at 12:40
1

Try this:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$directory = $objectManager->get('\Magento\Framework\Filesystem\DirectoryList');
$mediaPath = $directory->getPath('media');

$product_id = 10;
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id);

$galleryReadHandler = $objectManager->get('Magento\Catalog\Model\Product\Gallery\ReadHandler');
$galleryReadHandler->execute($product);
$images = $product->getMediaGalleryImages();

foreach($images as $image){
 $temp = $mediaPath."/catalog/product".$image->getFile();
 echo "<pre>"; print_r($temp);
}
0

Try below code. If you want to show product images category wise then use below code.

<?php $catid = $block->getCurrentCategory()->getId(); ?>
<?php 
$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        
$appState = $objectManager->get('\Magento\Framework\App\State');
//$appState->setAreaCode('frontend');
$categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory');
$categoryHelper = $objectManager->get('\Magento\Catalog\Helper\Category');
$categoryRepository = $objectManager->get('\Magento\Catalog\Model\CategoryRepository');
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$mediaUrl =  $storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
if ($catid == 3) 
  {
    $categoryId = $catid;
    $category = $categoryFactory->create()->load($categoryId);
    $categoryProducts = $category->getProductCollection()
                             ->addAttributeToSelect('*');
 ?>
 <div id="Coverflow">
     <ul class="flip-items">
        <?php foreach($categoryProducts as $product): ?>
         <li>
            <a href="<?php echo $product->getProductUrl() ?>">
                 <img src="<?php echo $mediaUrl.'catalog/product/'.$product->getImage() ?>" alt=""/>
             </a>
         </li>
        <?php endforeach; ?>
     </ul>
</div>
<?php } ?>

I hope it will help you.

2
  • 1
    At least 70% of the code here you don't need. Aug 8, 2018 at 13:55
  • this is work for me
    – Devidas
    Aug 9, 2018 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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