Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以编程方式将多个图像添加到magento中的单个产品_Magento_Magento 1.9_Magento2 - Fatal编程技术网

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

以编程方式将多个图像添加到magento中的单个产品,magento,magento-1.9,magento2,Magento,Magento 1.9,Magento2,在magento中提供了需要添加多个图像的产品id $count = 0; $imgArray = array($fpath.'configurable.png'); foreach ($imgArray as $image){ $imgUrl = _save_image( $image,$objectManager ); if ($count == 0){ $configProduct->addImageToMediaGallery( $i

在magento中提供了需要添加多个图像的产品id

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

下面的代码适合我。

if(sizeof($imgs)>1){
/*为现有产品指定其他图像*/
$product=$\u objectManager->create('Magento\Catalog\Model\product')->load($newProdId);
$productRepository=$\u objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface');
$productRepository->save($product);
对于($i=1;$iaddimagetodiagallery($image\u目录,数组('image','small\u image','thumbnail'),false,false);
$product->save();
}
}
}

以下代码对我很有用。

if(sizeof($imgs)>1){
/*为现有产品指定其他图像*/
$product=$\u objectManager->create('Magento\Catalog\Model\product')->load($newProdId);
$productRepository=$\u objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface');
$productRepository->save($product);
对于($i=1;$iaddimagetodiagallery($image\u目录,数组('image','small\u image','thumbnail'),false,false);
$product->save();
}
}
}
$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++;   
        }
    }
}
 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();

                }
            }

        }