Get all images of a product by product id magento 2.

You can get all the images assigned to specific products y product id.

You can get the details of products images like filename, media_type, position, and types.

using ProductRepositoryInterface you can get the images data using below code snippet,

<?php
namespace Path\To\Image;

use Exception;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Catalog\Api\ProductRepositoryInterface;

class Images extends Template
{
    /**
     * @var ProductRepositoryInterface
     */
    private $productRepository;

    public function __construct(
        Context $context,
        ProductRepositoryInterface $productRepository,
        array $data = []
    ) {
        $this->productRepository = $productRepository;
        parent::__construct($context,$data);
    }

    /**
     * get image list
     */
    public function getImages()
    {
        $productId = 1;
        try {
            $product = $this->productRepository->getById($productId);
        } catch (Exception $exception) {
            $product = null; // $exception->getMessage()
        }
        $images = $product->getMediaGalleryEntries();
        return $images;
    }

Call  from the template file,

$imagesList = $block->getImages();
    foreach ($imagesList as $image) {
        echo "<pre>";print_r($image->getData());
    }

Output:

Array
(
    [file] => /c/a/cape1.jpg
    [media_type] => image
    [label] => 
    [position] => 1
    [disabled] => 0
    [types] => Array
        (
            [0] => image
            [1] => small_image
            [2] => thumbnail
            [3] => swatch_image
        )

    [id] => 1
)
Array
(
    [file] => /c/a/cape2.jpg
    [media_type] => image
    [label] => 
    [position] => 2
    [disabled] => 0
    [types] => Array
        (
        )

    [id] => 2
)
 Array
(
    [file] => /c/a/cape3.jpg
    [media_type] => image
    [label] => 
    [position] => 3
    [disabled] => 0
    [types] => Array
        (
        )

    [id] => 3
)