JustPaste.it

<?php
require_once('./app/Mage.php');
Mage::app();

$productId = 19;
//load the product
$product = Mage::getModel('catalog/product')->load($productId);
//get all images
$mediaGallery = $product->getMediaGallery();

 

//if there are images
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')->updateAttributes(array($product->getId()), array('image'=>$image['file']), 0);
//stop
break;
}
foreach ($mediaGallery['images'] as $small_image){
//set the first image as the small_image
Mage::getSingleton('catalog/product_action')->updateAttributes(array($product->getId()), array('small_image'=>$small_image['file']), 0);
//stop
break;
}

foreach ($mediaGallery['images'] as $thumbnail){
//set the first image as the thumbnail
Mage::getSingleton('catalog/product_action')->updateAttributes(array($product->getId()), array('thumbnail'=>$thumbnail['file']), 0);
//stop
break;
}
}


?>