How To Import Product Images From URL In Magento 2
Magento 2, being popular, often hosts stores migrating from other platforms. It requires to import product images from URL in Magento 2. By default, Magento 2 handles image imports well but they should be located in your server only. But when it comes to importing the product image from an external URL, you can use the below solution.
The below service can be included anywhere required and its execute method will be called with the following params:
- $product – loaded product instance, the image will be added to it
- $imageUrl – external image URL
- $visible – an image will be hidden by default. You may make it visible, simply by passing the boolean value “true”;
- $imageType – an array, optional param, where you can specify whether to set an image as the main image, a small image or a thumbnail or any combination of those.
The post offers the solution to easily import product images from the external URL in Magento 2 store.
Method To Import Product Images From URL In Magento 2:
Create ImportImageService.php Class in app/code/[Vendor]/[Module]/Service folder and add the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php namespace [Vendor]\[Module]\Service; use Magento\Catalog\Model\Product; use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Filesystem\Io\File; class ImportImageService { protected $directoryList; protected $file; public function __construct(DirectoryList $directoryList,File $file) { $this->directoryList = $directoryList; $this->file = $file; } public function execute($product, $imageUrl, $visible = false, $imageType = []) { $tmpDir = $this->getMediaDirTmpDir(); $this->file->checkAndCreateFolder($tmpDir); $newFileName = $tmpDir . baseName($imageUrl); $result = $this->file->read($imageUrl, $newFileName); if ($result) { $product->addImageToMediaGallery($newFileName, $imageType, true, $visible); } return $result; } protected function getMediaDirTmpDir() { return $this->directoryList->getPath(DirectoryList::MEDIA) . DIRECTORY_SEPARATOR . 'tmp'; } } |
The above solution proves as a time-saver while carrying out the data migration to Magento 2.
Any doubts on the topic can be mentioned in the Comments section below. I’d be happy to help.
Do share the post with fellow developers via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
8 Comments
Please help me
when i tried its return false result value and if condition not working .
Hey Mueez, Make sure If The file is present and is readable in the given file path.
For that put the log and check file path.
Hello,
Does it work for gallery images?
Thank you!!
Hello Shine,
Yes, it will work for gallery images
Thank You
Hi, I am trying to replicate your solution on my Magento ver. 2.1.0 but I can’t locate the code folder inside my app folder. Is it possible that I have changed the structure? Please, I need to implement this solution and I don’t know where to put the importimageservice.php file.
Hello Daniel,
Please create a custom module and use the code
https://meetanshi.com/blog/magento-2-module-development/
https://meetanshi.com/blog/create-module-in-magento-2/
Thank You
Hello.i’m using this code but i’m getting blank in result variable
my image url is “https://img.discogs.com/KsuCqu0nVMAbFpy_Xdtohj9r0vs=/fit-in/600×606/filters:strip_icc():format(jpeg):mode_rgb():quality(90)/discogs-images/R-170932-1489501295-6197.jpeg.jpg”
Can you please check and let me know
Thank you.
Hello, The image URL that you are passing in the browser must be correct. The given URL shows 400 error.
Thanks.