magentomagento-1.9magento2

Programmatically add multiple images to a single product in magento


The product id is given to which multiple images need to be added in magento.

$count = 0;
$imgArray  = array($fpath.'configurable.png');
foreach ($imgArray as $image){
    $imgUrl = _save_image( $image,$objectManager );       
    if ($count == 0){
        $configProduct->addImageToMediaGallery( $imgUrl , $mediaAttribute, true, false ); 
    }else {
        $configProduct->addImageToMediaGallery( $imgUrl , null, true, false );
    }
    $count++;  
}

Solution

  • $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId); // Load product object
    $mediaAttribute = array ('thumbnail','small_image','image');
    
    $mediaDir = $objectManager->get('Magento\Framework\App\Filesystem\DirectoryList')->getPath('media');// Im Magento 2
    $mediaDir = Mage::getBaseDir('media');// In Magento 1
    
    
    
    
    
    //Case 1: When image files are alredy in your server
    $fpath = 'product/images/';// path to your file
    $count = 0;
    $imgArray  = array('image1.png','image2.png','image3.png','image4.png');
    foreach ($imgArray as $image){
        $imgUrl = _save_image( $fpath.$image,$objectManager,$mediaDir );// copies the file from your local storage to  your-magento-root-path/pub/media   
        if ($count == 0){
            $product->addImageToMediaGallery( $imgUrl , $mediaAttribute, true, false ); 
        }else {
            $product->addImageToMediaGallery( $imgUrl , null, true, false );
        }
        $count++;  
    }
    
    function _save_image($img,$objectManager,$mediaDir) {
        $imageFilename      = basename($img);    
        $image_type         = substr(strrchr($imageFilename,"."),1); //find the image extension
        $filename           = md5($img . strtotime('now')).'.'.$image_type; //give a new name, you can modify as per your requirement
    
    
        if (!file_exists($mediaDir)) mkdir($mediaDir, 0777, true);
        else chmod($mediaDir, 0777);
    
        $filepath           = $mediaDir . '/'. $filename; //path for temp storage folder: pub/media
        file_put_contents($filepath, file_get_contents(trim($img))); //store the image from external url to the temp storage folder
        return $filepath;
    }   
    
    
    
    
    
    
    //Case 2: When you have to browse images from a form. (Then save into your server and then )
    if(!empty($imageFile)){
        $count = 0;
        if (!file_exists($mediaDir)) mkdir($mediaDir, 0777, true);
        else chmod($mediaDir, 0777);
    
        foreach($imageFile['name'] as $k2=>$v2){    
            if($imageFile['error'][$k2] == 0 && file_exists($imageFile['tmp_name'][$k2])){
                $ext[$k2] = pathinfo($imageFile['name'][$k2], PATHINFO_EXTENSION);
                $filename[$k2] = md5(strtotime('now')).'.'.$ext[$k2];   
                $filepath[$k2] = $mediaDir .'/'. $filename[$k2];
    
                $bin_string[$k2] = file_get_contents($imageFile['tmp_name'][$k2]);
                file_put_contents($filepath[$k2], $bin_string[$k2]);
    
                if ($count == 0) :
                    $product[$k]->addImageToMediaGallery( $filepath[$k2] , $mediaAttribute, true, false ); 
                else :
                    $product[$k]->addImageToMediaGallery( $filepath[$k2] , null, true, false );
                endif;
                $count++;   
            }
        }
    }