<?php /** * file location: * app/code/VendorName/ExtensionName/Service/ImportImageService.php */ namespace VendorNameExtensionNameService; use MagentoCatalogModelProduct; use MagentoFrameworkAppFilesystemDirectoryList; use MagentoFrameworkFilesystemIoFile; /** * Class ImportImageService * assign images to products by image URL */ class ImportImageService { /** * Directory List * * @var DirectoryList */ protected $directoryList; /** * File interface * * @var File */ protected $file; /** * ImportImageService constructor * * @param DirectoryList $directoryList * @param File $file */ public function __construct( DirectoryList $directoryList, File $file ) { $this->directoryList = $directoryList; $this->file = $file; } /** * Main service executor * * @param Product $product * @param string $imageUrl * @param array $imageType * @param bool $visible * * @return bool */ public function execute($product, $imageUrl, $visible = false, $imageType = []) { /** @var string $tmpDir */ $tmpDir = $this->getMediaDirTmpDir(); /** create folder if it is not exists */ $this->file->checkAndCreateFolder($tmpDir); /** @var string $newFileName */ $newFileName = $tmpDir . baseName($imageUrl); /** read file from URL and copy it to the new destination */ $result = $this->file->read($imageUrl, $newFileName); if ($result) { /** add saved file to the $product gallery */ $product->addImageToMediaGallery($newFileName, $imageType, true, $visible); } return $result; } /** * Media directory name for the temporary file storage * pub/media/tmp * * @return string */ protected function getMediaDirTmpDir() { return $this->directoryList->getPath(DirectoryList::MEDIA) . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR; } }
Using above fucntion Images can be imported directly from external url.