Magento - Remove existing Media Gallery and add new one

Here is a set of functions, you can use to add multiple images after imports.


<?php

class Ayasoftware_SQLupdate_Model_Importconfigurableproducts {

......

/**
	 * Removes existing Media Gallery and add new one
	 *
	 */
	
	public function addImageToMediaGallery() {
		ini_set ( "memory_limit", "1024M" );
		$mediaGallery = $this->getImagesList ();
		
		for($j = 0; $j <= sizeof ( $mediaGallery ) - 1; $j ++) {
			foreach ( $mediaGallery [$j] as $key => $value ) {
				$product = Mage::getModel ( 'catalog/product' );
				$product_id = $product->getIdBySku ( $key );
				$product->load ( $product_id );
				$product->getId ();
				
				/**
				 * BEGIN REMOVE EXISTING MEDIA GALLERY
				 */
				$attributes = $product->getTypeInstance ()->getSetAttributes ();
				if (isset ( $attributes ['media_gallery'] )) {
					$gallery = $attributes ['media_gallery'];
					//Get the images
					$galleryData = $product->getMediaGallery ();
					foreach ( $galleryData ['images'] as $image ) {
						//If image exists
						if ($gallery->getBackend ()->getImage ( $product, $image ['file'] )) {
							$gallery->getBackend ()->removeImage ( $product, $image ['file'] );
						}
					}
					$product->save ();
				}
				/**
				 * END REMOVE EXISTING MEDIA GALLERY
				 */
				try {
					$galleryData = explode ( ';', $value );
					foreach ( $galleryData as $gallery_img ) /**
					 * @param directory where import image reides
					 * @param leave 'null' so that it isn't imported as thumbnail, base, or small
					 * @param false = the image is copied, not moved from the import directory to it's new location
					 * @param false = not excluded from the front end gallery
					 */
					{
						$product->setMediaGallery ( array ('images' => array (), 'values' => array () ) );
						if (file_exists ( Mage::getBaseDir ( 'media' ) . DS . 'import' . $gallery_img )) {
							$product->addImageToMediaGallery ( Mage::getBaseDir ( 'media' ) . DS . 'import' . $gallery_img, array ("thumbnail", "small_image", "image" ), false, false )->save ();
						}
					}
					$this->updateAddImageToMediaGallery ( $key );
				} catch ( Exception $e ) {
					echo $e->getMessage ();
				}
			}
		}
	}
	
	public function mediaGalleryisDone($sku) {
		$done = false;
		$select = "SELECT * FROM " . $this->coreResource->getTableName ( 'import_unique_styles' ) . " WHERE STYLE  = '$sku' ";
		$rows = $this->write->query ( $select );
		foreach ( $rows as $row ) {
			if ($row['MEDIA_GALLERY_DONE'] == 'YES') {
				$done = true;
			}
		
		}
		
		return $done;
	}
	/**
	 * Get the list of all images
	 * 
	 */
	public function getImagesList() {
		
		$mediaGallery = array ();
		$csvForImport = Mage::getStoreConfig ( 'sqlupdate/sqlupdateSettings/csvforimport' );
		// Get feed with full path
		$csvForImportFullPath = Mage::getBaseDir () . DS . "var/import" . DS . $csvForImport;
		// Load basic Configuration
		$this->setDefaultConfiguration ();
		// Read CSV Feed
		$this->readTheCsv ( ",", $csvForImportFullPath );
		$Data = $this->arrOutPut;
		for($i = 1; $i <= sizeof ( $Data ); $i ++) {
			if ($Data [$i] [4] != " " && $Data [$i] [2] == 'configurable') {
				$done = $this->mediaGalleryisDone ( $Data [$i] [10] );
				if (! $done) {
					$mediaGallery [] = array ($Data [$i] [10] => $Data [$i] [4] );
				}
			}
		}
		return $mediaGallery;
	}