1

On the products grid in the category view I need to display additional image for some products. When I have a gallery of product images:

$images = $product->getMediaGalleryImages();
foreach ($images as $item)
{
    //...
}

how to retrieve an image by its role in the gallery? For example, I'd like to get the image which is marked with the Thumbnail role (or with some other custom role).

Is there any way to do that?

enter image description here enter image description here

2 Answers 2

3

You can get products image by Type by following Code :

  $baseImage  = $product->getImage();
  $smallImage = $product->getSmallImage();
  $thumbnail  = $product->getThumbnail();

OR You can get types of gallery images of a product by following code:

  $existingMediaGalleryEntries = $product->getMediaGalleryEntries();

  foreach ($existingMediaGalleryEntries as $entry) {
      $entry->getTypes();
  }

OR If you have custom media role then :

  $customIcon = $product->getResource()->getAttribute('custom_media_attribute')->getFrontend()->getValue($product);
2
  • And what if I have a custom role (when I created a custom attribute with Input Type Media Image)? How to get image of custom role?
    – zitix
    Mar 21, 2018 at 13:18
  • @zitix See my updated answer. Mar 22, 2018 at 5:13
0

If you need to get image URL like on frontend then you can use this code. I'm not sure that is a best solution but it works for me.))

use Magento\Store\Model\App\Emulation;
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Catalog\Helper\ImageFactory as ImageHelper;

...

public function __construct(
    StoreManagerInterface     $storeManager,        
    ProductResource           $productResource,
    Emulation                 $appEmulation,
    ImageHelper               $imageHelper,
)
{
    $this->storeManager = $storeManager;        
    $this->productResource = $productResource;
    $this->appEmulation = $appEmulation;
    $this->imageHelper = $imageHelper;
}

/**
 * @param $product
 * @param $imageCustomRole
 * @return string
 * @throws \Magento\Framework\Exception\LocalizedException
 * @throws \Magento\Framework\Exception\NoSuchEntityException
 */
public function getCustomRoleImageUrl($product, $imageCustomRole)
{
    //Start frontend emulation for avoiding wrong path
    $this->appEmulation->startEnvironmentEmulation($this->storeManager->getStore()->getId(), Area::AREA_FRONTEND, true);
    
    //getting image relative path if it is needed
    $img = $product->getResource()->getAttribute($imageCustomRole)->getFrontend()->getValue($product);

    $result = '';
 
    if($img) {
        $productImageAttr = $product->getCustomAttribute($imageCustomRole);
        $productImage = $this->imageHelper->create()->init($product, $imageCustomRole)
            ->setImageFile($productImageAttr->getValue());
        $result = $productImage->getUrl();
    }
    
    //Stop frontend emulation
    $this->appEmulation->stopEnvironmentEmulation();
  
    return $result;
}

Your Answer

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

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