<?php
$productId = Mage::getModel('catalog/product')->getIdBySku('sku');
$product = Mage::getModel('catalog/product')->load($productId);
$mediaAttribute = array (
'thumbnail',
'small_image',
'image'
);
$imageUrl = '';
$destinationFilepath = '';
$mediaGalleryAttribute = Mage::getModel('catalog/resource_eav_attribute')->loadByCode('catalog_product', 'media_gallery');
foreach ($product->getMediaGallery('images') as $image) {
$mediaGalleryAttribute->getBackend()->removeImage($product, $image->getFile());
}
try{
$adapterGd2 = new Varien_Image_Adapter_Gd2();
$adapterGd2->open($imageUrl);
$adapterGd2->save($destinationFilepath);
$product->addImageToMediaGallery($destinationFilepath, $mediaAttribute, false, false);
unlink($destinationFilepath);
}
catch(Exception $e)
{
}
?>