4

How to add images to product gallery programmatically in magento2...

3 Answers 3

12

Use Below code to add images,thumbnail

Use following code to add/remove image from product in Magento2.

// Instance of object manager
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 

// Remove Images From Product
$productId = ; // Id of product
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
$productRepository = $objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface');
$existingMediaGalleryEntries = $product->getMediaGalleryEntries();
foreach ($existingMediaGalleryEntries as $key => $entry) {
    unset($existingMediaGalleryEntries[$key]);
}
$product->setMediaGalleryEntries($existingMediaGalleryEntries);
$productRepository->save($product);

// Add Images To The Product
$imagePath = "sample.png"; // path of the image
$product->addImageToMediaGallery($imagePath, array('image', 'small_image', 'thumbnail'), false, false);
$product->save();

Reference Url: https://stackoverflow.com/questions/40259303/add-remove-image-programmatically-to-product-magento2

2
  • 2
    Note that this code creates product attributes 'image', 'small_image', and 'thumbnail', and does so on the specified store view level. For example: add $product->setStoreId(0) if you want the attributes to work globally. Commented Oct 27, 2017 at 12:53
  • I have tried your solution, But it is not removing the existing images..
    – Jaisa
    Commented Jun 10, 2022 at 16:18
0

for me this solution not working.

I find another solution, working for me but also with bugs.

If my image is not big all is good, but if image is big (2.69MB 4000x2860px) I not have any errors, I not have "return 'XXXXX'" but product is saved and in frontend I have product with image

$om = \Magento\Framework\App\ObjectManager::getInstance();
$product = $om->create('Magento\Catalog\Model\Product')->load($copiedProduct->getID());
$mediaGalleryProcessor = $om->get('Magento\Catalog\Model\Product\Gallery\Processor');
$mediaGalleryProcessor->addImage($product,$path,array('image','thumbnail','small_image'), false, false);
$product->save(); return 'XXXXXX';
0

The following code works for me.

  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();

                }
            }

        }

Please refer the below tutorial for initializing object manager and set the file path.

https://www.pearlbells.co.uk/programmatically-add-multiple-images-magento-2/

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.