以编程方式将多个图像添加到magento中的单个产品中

时间:2016-08-08 12:22:48

标签: magento magento-1.9 magento2

产品ID指定需要在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++;  
}

2 个答案:

答案 0 :(得分:3)

$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++;   
        }
    }
}

答案 1 :(得分:0)

以下代码对我有用。 https://www.pearlbells.co.uk/programmatically-add-multiple-images-magento-2/

 if(sizeof($imgs) > 1) {
            /* Assign additional images to existing products */
            $product = $_objectManager->create('Magento\Catalog\Model\Product')->load($newProdId);
            $productRepository = $_objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface');
            $productRepository->save($product);

            for ( $i=1; $i<sizeof($imgs); $i++ ) {

                echo 'Add Images :' . $prdbasepath.basename(trim($imgs[$i])) . PHP_EOL;
                $image_directory = $prdbasepath.'data'.DS.basename(trim($imgs[$i]));

                if (file_exists($image_directory) && getimagesize($image_directory)) {

                    echo 'File exists'.PHP_EOL;
                    $product->addImageToMediaGallery($image_directory, array('image', 'small_image', 'thumbnail'), false, false);
                    $product->save();

                }
            }

        }