Regarding your first question, I reckon you should use Magento\Catalog\Api\ProductAttributeMediaGalleryManagementInterface
Usual process you need to first inject the class in your constructor:
protected $_productMediaGalleryInterface;
public __construct(
...
\Magento\Catalog\Api\ProductAttributeMediaGalleryManagementInterface $productMediaGalleryInterface
...
) {
...
$this->_productMediaGalleryInterface = $productMediaGalleryInterface;
...
}
Then in your class, based on the product sku you can retrieve the list of gallerie entries associated with the given product:
$galleryEntries = $this->_productMediaGalleryInterface->getList($sku);
However, I'm pretty sure this won't give you all the data and maybe just a list of gallery entries.
To get information about a gallery entry you can call:
$galleryEntryData = this->_productMediaGalleryInterface->get($sku, $entryId);
Regarding your second question, I should you should use plugins.
Indeed, the getImageUrl
is a public method for both Magento\Catalog\Helper\Product
and Magento\Catalog\Block\Product\Gallery
Thus using a plugin with after
statement you should be able to change the product image URL in a clean way.
I'm not too sure how service contracts would be relevant in that case. Service contracts are relevant when you add new type of database entities via a module and you need to do simple CRUD operations.