1

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

asked Aug 2, 2021 at 9:01
1
  • Can you please upload full code? Commented 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.

answered Aug 2, 2021 at 9:14
2
  • 1
    Thank you, sir! it worked, you never disappoint us Commented Aug 2, 2021 at 11:20
  • Happy to solve !! Happy Coding !! Commented 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()

answered Aug 2, 2021 at 9:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.