{"id":705,"date":"2019-12-17T06:59:18","date_gmt":"2019-12-17T06:59:18","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/12\/17\/import-product-images-from-url-in-magento-2\/"},"modified":"2025-05-22T15:13:22","modified_gmt":"2025-05-22T09:43:22","slug":"import-product-images-from-url-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/import-product-images-from-url-in-magento-2\/","title":{"rendered":"How To Import Product Images From URL In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2, being popular, often hosts stores migrating from other platforms. It requires to <strong>import product images from URL in Magento 2<\/strong>. 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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The below service can be included anywhere required and its execute method will be called with the following params:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>$product<\/em>&nbsp;\u2013 loaded product instance, the image will be added to it<\/li>\n\n\n\n<li><em>$imageUrl<\/em>&nbsp;\u2013 external image URL<\/li>\n\n\n\n<li><em>$visible<\/em>&nbsp;\u2013 an image will be hidden by default. You may make it visible, simply by passing the boolean value \u201ctrue\u201d;<\/li>\n\n\n\n<li><em>$imageType<\/em>&nbsp;\u2013 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.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The post offers the solution to easily import product images from the external URL in Magento 2 store.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method To Import Product Images From URL In Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create <em><strong>ImportImageService.php<\/strong><\/em> Class in <strong>app\/code\/[Vendor]\/[Module]\/Service<\/strong> folder and add the following code:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace [Vendor]\\[Module]\\Service;\n    \n    use Magento\\Catalog\\Model\\Product;\n    use Magento\\Framework\\App\\Filesystem\\DirectoryList;\n    use Magento\\Framework\\Filesystem\\Io\\File;\n    \n    class ImportImageService\n    {\n        protected $directoryList;\n        protected $file;\n\n        public function __construct(DirectoryList $directoryList,File $file)\n        {\n            $this->directoryList = $directoryList;\n            $this->file = $file;\n        }\n\n        public function execute($product, $imageUrl, $visible = false, $imageType = [])\n        {\n            $tmpDir = $this->getMediaDirTmpDir();\n            $this->file->checkAndCreateFolder($tmpDir);\n            $newFileName = $tmpDir . baseName($imageUrl);\n            $result = $this->file->read($imageUrl, $newFileName);\n            if ($result) {\n                $product->addImageToMediaGallery($newFileName, $imageType, true, $visible);\n            }\n            return $result;\n        }\n\n        protected function getMediaDirTmpDir()\n        {\n            return $this->directoryList->getPath(DirectoryList::MEDIA) . DIRECTORY_SEPARATOR . 'tmp';\n        }\n    }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The above solution proves as a time-saver while carrying out the data <a href=\"https:\/\/meetanshi.com\/magento-2-migration-service.html\" target=\"_blank\" rel=\"noreferrer noopener\">migration to Magento 2<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do share the post with fellow developers via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-705","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=705"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/705\/revisions"}],"predecessor-version":[{"id":15148,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/705\/revisions\/15148"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}