How Importing Images with Labels using addImageToMediaGallery in magento2? Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?How to programatically import product and set images to default?Adding images with CSV importMagmi import extra images, how?Importing Images with Labels using addImageToMediaGalleryAvs FastSimpleImport - Import all imagesStoreviews not showing the same product image after importBase Image, Small Image, Thumbnail Image radio buttons are not selected after product images CSV importhow to uncheck exclude image checkbox at mass image importMagento 2.2 adding images to product programmatically does not set small_image

Are my PIs rude or am I just being too sensitive?

Is it possible to ask for a hotel room without minibar/extra services?

Replacing HDD with SSD; what about non-APFS/APFS?

3 doors, three guards, one stone

Windows 10: How to Lock (not sleep) laptop on lid close?

Was credit for the black hole image misattributed?

Stars Make Stars

Mortgage adviser recommends a longer term than necessary combined with overpayments

How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time

Classification of bundles, Postnikov towers, obstruction theory, local coefficients

Need a suitable toxic chemical for a murder plot in my novel

How many things? AとBがふたつ

Can a non-EU citizen traveling with me come with me through the EU passport line?

Direct Experience of Meditation

What LEGO pieces have "real-world" functionality?

How is simplicity better than precision and clarity in prose?

90's book, teen horror

Is 1 ppb equal to 1 μg/kg?

Understanding this description of teleportation

Stop battery usage [Ubuntu 18]

How does the Nova's Burn power work at the 7-9 level?

Sorting inherited template fields

Stopping real property loss from eroding embankment

How does modal jazz use chord progressions?



How Importing Images with Labels using addImageToMediaGallery in magento2?



Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?How to programatically import product and set images to default?Adding images with CSV importMagmi import extra images, how?Importing Images with Labels using addImageToMediaGalleryAvs FastSimpleImport - Import all imagesStoreviews not showing the same product image after importBase Image, Small Image, Thumbnail Image radio buttons are not selected after product images CSV importhow to uncheck exclude image checkbox at mass image importMagento 2.2 adding images to product programmatically does not set small_image



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















Magento2.1.x



I had a problem adding a product Image Label programatically. Here's the code I used:



 $imageLabel1 = 'Test1';
$imageLabel2 = 'Test2';
$imageLabel3 = 'Test3';
$product->setMediaGallery (array('images'=>array (), 'values'=>array ()))
->addImageToMediaGallery('/test1.jpg', array('image'), false, false);
->addImageToMediaGallery('/test2.jpg', array('thumbnail'), false, false);
->addImageToMediaGallery('/test3.jpg', array('small_image'), false, false);


Where i can set $imageLabel1 - 2 - 3?










share|improve this question
















bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.





















    0















    Magento2.1.x



    I had a problem adding a product Image Label programatically. Here's the code I used:



     $imageLabel1 = 'Test1';
    $imageLabel2 = 'Test2';
    $imageLabel3 = 'Test3';
    $product->setMediaGallery (array('images'=>array (), 'values'=>array ()))
    ->addImageToMediaGallery('/test1.jpg', array('image'), false, false);
    ->addImageToMediaGallery('/test2.jpg', array('thumbnail'), false, false);
    ->addImageToMediaGallery('/test3.jpg', array('small_image'), false, false);


    Where i can set $imageLabel1 - 2 - 3?










    share|improve this question
















    bumped to the homepage by Community yesterday


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

















      0












      0








      0








      Magento2.1.x



      I had a problem adding a product Image Label programatically. Here's the code I used:



       $imageLabel1 = 'Test1';
      $imageLabel2 = 'Test2';
      $imageLabel3 = 'Test3';
      $product->setMediaGallery (array('images'=>array (), 'values'=>array ()))
      ->addImageToMediaGallery('/test1.jpg', array('image'), false, false);
      ->addImageToMediaGallery('/test2.jpg', array('thumbnail'), false, false);
      ->addImageToMediaGallery('/test3.jpg', array('small_image'), false, false);


      Where i can set $imageLabel1 - 2 - 3?










      share|improve this question
















      Magento2.1.x



      I had a problem adding a product Image Label programatically. Here's the code I used:



       $imageLabel1 = 'Test1';
      $imageLabel2 = 'Test2';
      $imageLabel3 = 'Test3';
      $product->setMediaGallery (array('images'=>array (), 'values'=>array ()))
      ->addImageToMediaGallery('/test1.jpg', array('image'), false, false);
      ->addImageToMediaGallery('/test2.jpg', array('thumbnail'), false, false);
      ->addImageToMediaGallery('/test3.jpg', array('small_image'), false, false);


      Where i can set $imageLabel1 - 2 - 3?







      magento-2.1 import product-images programmatically






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 31 '18 at 18:40









      sv3n

      9,96762557




      9,96762557










      asked Aug 30 '17 at 18:12









      Magento2 DevloperMagento2 Devloper

      9431135




      9431135





      bumped to the homepage by Community yesterday


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community yesterday


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Try this:



          $imageLabel1 = 'Test1';
          $existingMediaGalleryEntries = $product->getMediaGalleryEntries();
          foreach ($existingMediaGalleryEntries as $key => $entry)
          $entry->setLabel($imageLabel1);

          $product->setStoreId(0);
          $product->setMediaGalleryEntries($existingMediaGalleryEntries);


          UPDATED



          The code above is the only way to set label for a media gallery entry, because there's no direct API from Product to set them.



          It also means, you still using your current code to set new media gallery, then use the code above to update label for it after $product->save() (another mean, you have to save() product twice)



          Pseudo code



          public function addImageToMediaGallery(Product $product) 
          // your code
          $product->save();


          public function updateImageLabel(Product $product, $label)
          // my code
          $product->save();


          public function updateProduct()
          // ....

          $this->addImageToMediaGallery($product);
          foreach ($labels as $label)
          $this->updateImageLabel($label);


          // ....






          share|improve this answer

























          • Thanx, but where can I set this code? I mean below my code? Is it working with multiple image set? see my updated question@ Toan

            – Magento2 Devloper
            Aug 31 '17 at 5:34












          • @Magento2Devloper I updated my question. Hope it makes more sense to you now :)

            – Toan Nguyen
            Aug 31 '17 at 7:00











          • It's working but image label not set image wise. ex - test1.jpg in set imageLabel3. any idea regarding this?

            – Magento2 Devloper
            Aug 31 '17 at 8:17











          • @Magento2Devloper I don't understand what you mean. Can you update it in your question again?

            – Toan Nguyen
            Aug 31 '17 at 8:33











          • I need to set image label like ex - test1.jpg in set 'Test1' Label. in your code set test1.jpg in 'Test3' label. now understand?

            – Magento2 Devloper
            Aug 31 '17 at 8:42











          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "479"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f191269%2fhow-importing-images-with-labels-using-addimagetomediagallery-in-magento2%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Try this:



          $imageLabel1 = 'Test1';
          $existingMediaGalleryEntries = $product->getMediaGalleryEntries();
          foreach ($existingMediaGalleryEntries as $key => $entry)
          $entry->setLabel($imageLabel1);

          $product->setStoreId(0);
          $product->setMediaGalleryEntries($existingMediaGalleryEntries);


          UPDATED



          The code above is the only way to set label for a media gallery entry, because there's no direct API from Product to set them.



          It also means, you still using your current code to set new media gallery, then use the code above to update label for it after $product->save() (another mean, you have to save() product twice)



          Pseudo code



          public function addImageToMediaGallery(Product $product) 
          // your code
          $product->save();


          public function updateImageLabel(Product $product, $label)
          // my code
          $product->save();


          public function updateProduct()
          // ....

          $this->addImageToMediaGallery($product);
          foreach ($labels as $label)
          $this->updateImageLabel($label);


          // ....






          share|improve this answer

























          • Thanx, but where can I set this code? I mean below my code? Is it working with multiple image set? see my updated question@ Toan

            – Magento2 Devloper
            Aug 31 '17 at 5:34












          • @Magento2Devloper I updated my question. Hope it makes more sense to you now :)

            – Toan Nguyen
            Aug 31 '17 at 7:00











          • It's working but image label not set image wise. ex - test1.jpg in set imageLabel3. any idea regarding this?

            – Magento2 Devloper
            Aug 31 '17 at 8:17











          • @Magento2Devloper I don't understand what you mean. Can you update it in your question again?

            – Toan Nguyen
            Aug 31 '17 at 8:33











          • I need to set image label like ex - test1.jpg in set 'Test1' Label. in your code set test1.jpg in 'Test3' label. now understand?

            – Magento2 Devloper
            Aug 31 '17 at 8:42















          0














          Try this:



          $imageLabel1 = 'Test1';
          $existingMediaGalleryEntries = $product->getMediaGalleryEntries();
          foreach ($existingMediaGalleryEntries as $key => $entry)
          $entry->setLabel($imageLabel1);

          $product->setStoreId(0);
          $product->setMediaGalleryEntries($existingMediaGalleryEntries);


          UPDATED



          The code above is the only way to set label for a media gallery entry, because there's no direct API from Product to set them.



          It also means, you still using your current code to set new media gallery, then use the code above to update label for it after $product->save() (another mean, you have to save() product twice)



          Pseudo code



          public function addImageToMediaGallery(Product $product) 
          // your code
          $product->save();


          public function updateImageLabel(Product $product, $label)
          // my code
          $product->save();


          public function updateProduct()
          // ....

          $this->addImageToMediaGallery($product);
          foreach ($labels as $label)
          $this->updateImageLabel($label);


          // ....






          share|improve this answer

























          • Thanx, but where can I set this code? I mean below my code? Is it working with multiple image set? see my updated question@ Toan

            – Magento2 Devloper
            Aug 31 '17 at 5:34












          • @Magento2Devloper I updated my question. Hope it makes more sense to you now :)

            – Toan Nguyen
            Aug 31 '17 at 7:00











          • It's working but image label not set image wise. ex - test1.jpg in set imageLabel3. any idea regarding this?

            – Magento2 Devloper
            Aug 31 '17 at 8:17











          • @Magento2Devloper I don't understand what you mean. Can you update it in your question again?

            – Toan Nguyen
            Aug 31 '17 at 8:33











          • I need to set image label like ex - test1.jpg in set 'Test1' Label. in your code set test1.jpg in 'Test3' label. now understand?

            – Magento2 Devloper
            Aug 31 '17 at 8:42













          0












          0








          0







          Try this:



          $imageLabel1 = 'Test1';
          $existingMediaGalleryEntries = $product->getMediaGalleryEntries();
          foreach ($existingMediaGalleryEntries as $key => $entry)
          $entry->setLabel($imageLabel1);

          $product->setStoreId(0);
          $product->setMediaGalleryEntries($existingMediaGalleryEntries);


          UPDATED



          The code above is the only way to set label for a media gallery entry, because there's no direct API from Product to set them.



          It also means, you still using your current code to set new media gallery, then use the code above to update label for it after $product->save() (another mean, you have to save() product twice)



          Pseudo code



          public function addImageToMediaGallery(Product $product) 
          // your code
          $product->save();


          public function updateImageLabel(Product $product, $label)
          // my code
          $product->save();


          public function updateProduct()
          // ....

          $this->addImageToMediaGallery($product);
          foreach ($labels as $label)
          $this->updateImageLabel($label);


          // ....






          share|improve this answer















          Try this:



          $imageLabel1 = 'Test1';
          $existingMediaGalleryEntries = $product->getMediaGalleryEntries();
          foreach ($existingMediaGalleryEntries as $key => $entry)
          $entry->setLabel($imageLabel1);

          $product->setStoreId(0);
          $product->setMediaGalleryEntries($existingMediaGalleryEntries);


          UPDATED



          The code above is the only way to set label for a media gallery entry, because there's no direct API from Product to set them.



          It also means, you still using your current code to set new media gallery, then use the code above to update label for it after $product->save() (another mean, you have to save() product twice)



          Pseudo code



          public function addImageToMediaGallery(Product $product) 
          // your code
          $product->save();


          public function updateImageLabel(Product $product, $label)
          // my code
          $product->save();


          public function updateProduct()
          // ....

          $this->addImageToMediaGallery($product);
          foreach ($labels as $label)
          $this->updateImageLabel($label);


          // ....







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 31 '17 at 7:00

























          answered Aug 31 '17 at 4:27









          Toan NguyenToan Nguyen

          2,0021138




          2,0021138












          • Thanx, but where can I set this code? I mean below my code? Is it working with multiple image set? see my updated question@ Toan

            – Magento2 Devloper
            Aug 31 '17 at 5:34












          • @Magento2Devloper I updated my question. Hope it makes more sense to you now :)

            – Toan Nguyen
            Aug 31 '17 at 7:00











          • It's working but image label not set image wise. ex - test1.jpg in set imageLabel3. any idea regarding this?

            – Magento2 Devloper
            Aug 31 '17 at 8:17











          • @Magento2Devloper I don't understand what you mean. Can you update it in your question again?

            – Toan Nguyen
            Aug 31 '17 at 8:33











          • I need to set image label like ex - test1.jpg in set 'Test1' Label. in your code set test1.jpg in 'Test3' label. now understand?

            – Magento2 Devloper
            Aug 31 '17 at 8:42

















          • Thanx, but where can I set this code? I mean below my code? Is it working with multiple image set? see my updated question@ Toan

            – Magento2 Devloper
            Aug 31 '17 at 5:34












          • @Magento2Devloper I updated my question. Hope it makes more sense to you now :)

            – Toan Nguyen
            Aug 31 '17 at 7:00











          • It's working but image label not set image wise. ex - test1.jpg in set imageLabel3. any idea regarding this?

            – Magento2 Devloper
            Aug 31 '17 at 8:17











          • @Magento2Devloper I don't understand what you mean. Can you update it in your question again?

            – Toan Nguyen
            Aug 31 '17 at 8:33











          • I need to set image label like ex - test1.jpg in set 'Test1' Label. in your code set test1.jpg in 'Test3' label. now understand?

            – Magento2 Devloper
            Aug 31 '17 at 8:42
















          Thanx, but where can I set this code? I mean below my code? Is it working with multiple image set? see my updated question@ Toan

          – Magento2 Devloper
          Aug 31 '17 at 5:34






          Thanx, but where can I set this code? I mean below my code? Is it working with multiple image set? see my updated question@ Toan

          – Magento2 Devloper
          Aug 31 '17 at 5:34














          @Magento2Devloper I updated my question. Hope it makes more sense to you now :)

          – Toan Nguyen
          Aug 31 '17 at 7:00





          @Magento2Devloper I updated my question. Hope it makes more sense to you now :)

          – Toan Nguyen
          Aug 31 '17 at 7:00













          It's working but image label not set image wise. ex - test1.jpg in set imageLabel3. any idea regarding this?

          – Magento2 Devloper
          Aug 31 '17 at 8:17





          It's working but image label not set image wise. ex - test1.jpg in set imageLabel3. any idea regarding this?

          – Magento2 Devloper
          Aug 31 '17 at 8:17













          @Magento2Devloper I don't understand what you mean. Can you update it in your question again?

          – Toan Nguyen
          Aug 31 '17 at 8:33





          @Magento2Devloper I don't understand what you mean. Can you update it in your question again?

          – Toan Nguyen
          Aug 31 '17 at 8:33













          I need to set image label like ex - test1.jpg in set 'Test1' Label. in your code set test1.jpg in 'Test3' label. now understand?

          – Magento2 Devloper
          Aug 31 '17 at 8:42





          I need to set image label like ex - test1.jpg in set 'Test1' Label. in your code set test1.jpg in 'Test3' label. now understand?

          – Magento2 Devloper
          Aug 31 '17 at 8:42

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Magento Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f191269%2fhow-importing-images-with-labels-using-addimagetomediagallery-in-magento2%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

          Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

          Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form