{"id":520,"date":"2019-07-23T21:30:01","date_gmt":"2019-07-23T21:30:01","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/07\/23\/add-images-to-product-gallery-in-magento-2\/"},"modified":"2025-05-22T16:15:01","modified_gmt":"2025-05-22T10:45:01","slug":"add-images-to-product-gallery-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-images-to-product-gallery-in-magento-2\/","title":{"rendered":"How To Programmatically Add Images to Product Gallery in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Perfect pictures encourage more clicks and drive more sales!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As you plan the design of your Magento 2 store, you might think that images are those \u201cnice to have\u201d elements that don\u2019t serve much of a purpose beyond looking good. But images do so much more than paint a pretty picture.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, to add multiple images to the product at once, you need to add images for each product manually in the default Magento 2. To overcome such manual tasks, you can&nbsp;<em><strong>add images to product gallery in Magento 2<\/strong><\/em>&nbsp;via CSV bulk upload.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With this programmatic method, add multiple images to the product gallery at once and leverage the benefits of having images in an E-commerce store such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Stores with more images get more views<\/li>\n\n\n\n<li>Images capture attention<\/li>\n\n\n\n<li>Convenience in online shopping<\/li>\n\n\n\n<li>Improved SERPs<\/li>\n\n\n\n<li>Improved click-through rate<\/li>\n\n\n\n<li>Boost in social media<\/li>\n\n\n\n<li>Boosts SEO value<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Now that you know what you\u2019d miss if you do not fill your product gallery with suitable images, get on implementing the below method!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Add Images to Product Gallery in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create a new php file in the&nbsp;<strong>ROOT<\/strong>&nbsp;directory and place the below code to assign images:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php \n    use Magento\\Framework\\App\\Bootstrap;\n    require __DIR__ . '\/app\/bootstrap.php';\n\n    $bootstrap = Bootstrap::create(BP, $_SERVER);\n\n    $obj = $bootstrap->getObjectManager();\n\n    $state = $obj->get('Magento\\Framework\\App\\State');\n    $state->setAreaCode('frontend');\n\n    try\n    {\n        $objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance();\n        $importDir = __DIR__ . '\/pub\/media\/catalog\/product'; \/\/This is the directory path from where you have to take the images\n        \n        $i = '12'; \/\/  It must be product ID for which product it is to be assigned\n        \n        $product = $objectManager->get('Magento\\Catalog\\Model\\Product')->load($i);\n            \n        $id = $product->getId();\n        $url = $importDir . $product->getImage();\n        $product->addImageToMediaGallery($url, array('image', 'small_image', 'thumbnail'), true, false);\n        $product->save();\n        echo \"&lt;br \/>&lt;br \/> $id Product Save Succefully\";\n    \n    }\n    catch(\\Exception $e)\n    {\n        echo $e->getMessage();\n        exit;\n    }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I\u2019d be very grateful if you helped share this helpful post on social media to fellow developers!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Perfect pictures encourage more clicks and drive more sales! As you plan the design of your Magento 2 store, you might think that images are&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-520","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/520","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=520"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/520\/revisions"}],"predecessor-version":[{"id":15331,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/520\/revisions\/15331"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}