1

I am using Magento's image helper class to try to get a product's image url in a CLI command: $this->image->init($product, 'product_page_main_image_default')->getUrl().

When I use this code in a block in a frontend page, it returns https://... but when used in a CLI command it instead returns http://.... I was having the same issues with the Magento\Framework\UrlInterface where getting the base url also returned the insecure version. I had to explicitly specific $this->url->getBaseUrl(array('_secure' => true)) to get the secure version.

For the CLI command, I am setting the area code to frontend and I am also emulating the frontend store view $this->emulator->startEnvironmentEmulation(1, \Magento\Framework\App\Area::AREA_FRONTEND, true); My admin has the correct Secure Base Url and Base Link Url in place.

I am wondering if I am doing something wrong or if anyone knowns a fix to get the secure product image url rather than doing a str_replace on the insecure url.

Edit: added relevant code snippet:

Where I am attempting to get secure product image url, using Magento\Catalog\Model\ProductFactory and Magento\Catalog\Helper\Image:

$product = $this->productFactory->create()
                ->setId($id)
                ->setImage($imageLink);

$this->image->init($product, 'product_page_main_image_default')->getUrl();
  • Show the code which you done for custom command CLI? – Dhiren Vasoya May 3 '19 at 14:15
  • added relevant code, entire command is very large – A.B. May 3 '19 at 14:29
0

Try this code.

$product = $this->productFactory->create()
                ->setId($id)
                ->setImage($imageLink);

$store = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore();
$imageUrl = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product' . $product->getImage();
  • Unfortunately this did not work, it still returns an unsecure url – A.B. May 3 '19 at 14:47
  • Have you check, into secure url you have enter proper URL from admin. – Dhiren Vasoya May 6 '19 at 4:02
  • Urls are properly set in admin. – A.B. May 6 '19 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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