

'product_base_image' or any image code from vendor\magento\theme-frontend-luma\etc\view.For Akeneo PIM earlier than 4.0 - Asset Manager = v4.0 System Get an image public function getBaseImageUrl(ProductInterface $product, $storeId) \Magento\Store\Model\App\Emulation $appEmulation \Magento\Catalog\Helper\Image $imageHelper,

* \Magento\Catalog\Helper\Image $imageHelper $smallImage = $this->getImageUrl($productObject, 'product_page_image_small') Otherwise, you will get the error below (or similar): Unable to resolve the source file for 'webapi_rest/_view/en_AU/Magento_Catalog/images/product/placeholder/.jpg'ģ) Call the getImageUrl passing the product object and the type of image you want (based on your view.xml file). Note: The "appEmulation" code is only necessary when you making this call from the admin or for an API. $this->_appEmulation->stopEnvironmentEmulation() $imageUrl = $productImage->getImageUrl() $productImage = $imageBlock->getImage($product, $imageType) $imageBlock = $this->_blockFactory->createBlock('Magento\Catalog\Block\Product\ListProduct') $this->_appEmulation->startEnvironmentEmulation($storeId, \Magento\Framework\App\Area::AREA_FRONTEND, true) $storeId = $this->_storeManager->getStore()->getId() \Magento\Store\Model\App\Emulation $appEmulation)Ģ) Then, create a getImageUrl method with the code below: protected function getImageUrl($product, string $imageType = '') \Magento\Framework\View\Element\BlockFactory $blockFactory, it's quite efficient and clean:ġ) First, you need to inject the following classes: protected $_storeManager You can select other images types besides product_thumbnail_image: see magento/theme-frontend-luma/etc/view.xml for a list of available product images, or create your own in a view.xml file. $this->appEmulation->stopEnvironmentEmulation() >init($product, 'product_thumbnail_image')->getUrl() $imageUrl = $this->imageHelperFactory->create() now the image helper will get the correct URL with the frontend environment emulated

$product = $this->productRepositoryFactory->create()->get($sku) $this->appEmulation->startEnvironmentEmulation($storeId, \Magento\Framework\App\Area::AREA_FRONTEND, true) $storeId = $this->storeManager->getStore()->getId()

get the store ID from somewhere (maybe a specific store?) Then, wherever you need to get the image frontend URL: $sku = "my-sku" $this->imageHelperFactory = $imageHelperFactory $this->productRepositoryFactory = $productRepositoryFactory \Magento\Catalog\Helper\ImageFactory $imageHelperFactory \Magento\Catalog\Api\ProductRepositoryInterfaceFactory $productRepositoryFactory, \Magento\Store\Model\App\Emulation $appEmulation, \Magento\Store\Model\StoreManagerInterface $storeManager, * \Magento\Catalog\Helper\ImageFactory $helperFactory * \Magento\Catalog\Api\ProductRepositoryInterfaceFactory $productRepositoryFactory * \Magento\Store\Model\App\Emulation $appEmulation * \Magento\Store\Model\StoreManagerInterface $storeManager * \Magento\Catalog\Api\ProductRepositoryInterfaceFactory * \Magento\Store\Model\StoreManagerInterface If you want the published/cache frontend URL of an image for a specific store view (like I did) this is working for me: /**
