0

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.

5
  • which import are you using ?
    – Msquare
    Nov 20, 2023 at 8:56
  • I am working with product import root script.
    – Krishna
    Nov 20, 2023 at 9:12
  • Could you please share the script here?
    – Msquare
    Nov 20, 2023 at 9:35
  • I apologize for missing your message, but the script is messed up. I have no idea how this could have happened.
    – Krishna
    Nov 28, 2023 at 7:03
  • @Msquare I have updated with a script.
    – Krishna
    Nov 28, 2023 at 7:14

1 Answer 1

0

This is done by following code

$productCollection->addAttributeToSelect('new_additional_images'); // You can specify the attributes you want to select

foreach ($productCollection as $product) {
   
    if($product->getNewAdditionalImages()) {
        $pImgs = [];
        $pImgs = explode(',',$product->getNewAdditionalImages());
        
        $i = 1;
        foreach($pImgs as $imgs) {
            
            $imagePathNew = explode("?",$imgs); // here my image with http://example.com?noimage=logo, so exploded string ? to get actual image url.
            $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]));
        
            $imagePathTest = 'catalog/product/' . implode('/', $subdirectories) . '/' . basename($imagePath[0]);
            
            $imageContents = file_get_contents($imagePathNew[0]);
            $mediaDirectory->writeFile($imagePathTest, $imageContents);
            
            $product = $objectManager->create('Magento\Catalog\Model\Product')->load($product->getId()); 
            $product->addImageToMediaGallery($imagePathTest, array('image'), false, true);
            $i++;

            
            $product->save();
        }
    }
}

Thanks @Msquare for your response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.