0

in my Custom Graphql Query, I am trying to get product image Url using Magento\Catalog\Helper\Image in this way

$this->imageHelper->init($product, 'product_thumbnail_image')->getUrl();

I am not getting Url correctly I am getting something like this

https://baseurl/static/version1627894233/graphql/_view/en_US/Magento_Catalo[…]roduct/placeholder/.jpg

Anyone Help me to get the correct URL need Help

1
  • Can you please upload full code? Aug 2, 2021 at 9:07

2 Answers 2

1

You need to set store emulation inside your code. Like this below way :

/**
 * @var \Magento\Store\Model\App\Emulation
 */
protected $appEmulation;

/**
 * @param \Magento\Store\Model\App\Emulation $appEmulation
 */
public function __construct(
      ....
      \Magento\Store\Model\App\Emulation $appEmulation
      ....
) {
      ....
      $this->appEmulation = $appEmulation;
      ....
}

public function getImageUrl()
{
      $this->appEmulation->startEnvironmentEmulation($storeId, \Magento\Framework\App\Area::AREA_FRONTEND, true);
      $this->imageHelper->init($product, 'product_thumbnail_image')->getUrl();
      $this->appEmulation->stopEnvironmentEmulation();
}

Clean cache and check it.

2
  • 1
    Thank you, sir! it worked, you never disappoint us
    – Waqar Ali
    Aug 2, 2021 at 11:20
  • Happy to solve !! Happy Coding !! Aug 2, 2021 at 11:45
1

I would suggest :

1 - Check where is really located your images.

2 - Go into the imageHelper and debug it, check if the issue comes from the recording of your image location

After that you may want to create a new Helper overriding the magento one to fix your issue by adapting the process (probably the graph ql root media file isn't properly registered if i had to guess without seing anything).

===========================

That being said; why you don't just get the image using your $product object ? $product->getThumbnail()

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.