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
-
Can you please upload full code?Rohan Hapani– Rohan Hapani2021年08月02日 09:07:56 +00:00Commented Aug 2, 2021 at 9:07
2 Answers 2
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.
-
1Thank you, sir! it worked, you never disappoint usWaqar Ali– Waqar Ali2021年08月02日 11:20:46 +00:00Commented Aug 2, 2021 at 11:20
-
Happy to solve !! Happy Coding !!Rohan Hapani– Rohan Hapani2021年08月02日 11:45:39 +00:00Commented Aug 2, 2021 at 11:45
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()