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();