title: update product thumbnail ,small image and base image programatically Magento
body: <blockquote>
    <p>need to update the product thumbnail , small image and base image
    programatically</p>
    
    <p>for this i a using a below code</p>
  </blockquote>
  
  <pre><code>$collection1-&gt;addImageToMediaGallery($importDir,$mediaArray,false);
  
      //$collection1-&gt;setStoreId(0);
          $collection1-&gt;save();
  if (isset($mediaGallery['images'])){
              //loop through the images
              foreach ($mediaGallery['images'] as $image){
              //set the first image as the base image
              Mage::getSingleton('catalog/product_action')-&gt;updateAttributes(array($collection1-&gt;getId()), array('image'=&gt;$image['file'],'thumbnail'=&gt;$image['file'],'small_image'=&gt;$image['file']), 0);
              //stop
              break;
              }
              }
  </code></pre>
  
  <p>with this code a</p>
  
  <blockquote>
    <p>image adding to the gallery</p>
    
    <p>but i need to set the latest image to set as image and base image</p>
  </blockquote>
  
  <p>.Please suggest how can i achieve this thanks </p>