I attempted to programmatically import additional images from third-party URLs.Working with root script.
However, the URLs are different, and the names are the same. Does anyone have an idea of how we can upload all the images as additional images?
Example :
https:/example.com/100/abc.jpg,
https:/example.com/200/abc.jpg,
https:/example.com/300/abc.jpg
Here I am adding script with whom I am trying to get result
$pImgs = [];
$pImgs = explode(',',$product->getNewAdditionalImages());
$i = 1;
foreach($pImgs as $imgs) {
$imagePathNew = explode("?",$imgs); // image name are like https:/example.com/100/abc.jpg?noimage=logo, so here expload with ?.
enter code here
$imagePath = explode("?",$imgs);
$mediaDirectory = $objectManager->get('\Magento\Framework\Filesystem')
->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
$imagename = explode('.',basename($imagePath[0]));
$subdirectories = [
substr($imagename[0], 0, 1),
substr($imagename[0], 1, 1),
];
$imageUrl = $imagename[0].'_'.$i.'.'.$imagename[1];
$imgInDb = explode('.',basename($imagePathNew[0]));
$imagePath = 'catalog/product/' . implode('/', $subdirectories) . '/' . basename($imageUrl);
$newImagePath = str_replace(basename($imagePathNew[0]), $imgInDb[0].'_'.$i.'.'.$imgInDb[1],$imagePathNew);
print_r($newImagePath);
$imageContents = file_get_contents($newImagePath[0]);
$mediaDirectory->writeFile($imagePath, $imageContents);
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
$product->addImageToMediaGallery($imagePath, null, false, false);
$i++;
}
}
Thanks in advance.