Add images to products programmatically in Magento

This post is almost the opposite of an earlier post (which you can read here) which outlined how to bulk remove images from products. This time we’re using a CSV upload, along with the files themselves, to add images to products in magento automatically. It’s quite quick and dirty, and assumes that you’ve just one image that you want to add to a product, and that it should be the main image, the small image and the thumbnail image for that product.

The first step is to create your CSV. It just needs two columns (and no header row) – the left hand column should contain the product SKU and the right hand column should contain the name of the image that you want want to be uploaded for that SKU. Upload that to the web root of your site with the name “images.csv” (or whatever you want – just remember what you’ve called it as you’ll need to refer to it in a minute).

Then upload the images – for ease of reference put these in a folder called “incoming” inside the “media” folder in the web root of your site.

Finally the script that does the hard work. You can download it at the foot of this post – the following explains what’s happening where.

Start off by linking into your Magento installation :

<?php 
require_once 'app/Mage.php';
Mage::app();
Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID);

Then set the name of the folder to which you’ve uploaded the images – if you’ve gone with “media / incoming” as outlined above, you won’t need to change this line. If you haven’t, you will..

$importDir = Mage::getBaseDir('media') . DS . 'incoming/';

Now let’s pick up the CSV file we uploaded – if you’ve called it anything other than “images.csv” you need to change this line accordingly :

$file_handle = fopen("images.csv", "r");

Now let’s loop through the CSV :

while (!feof($file_handle) ) {
	$line_of_text = fgetcsv($file_handle, 1024);

Then get the product SKU from the first column, and load the product itself into $ourProduct, then get the image’s filename from the second column – combine that with the directory location we set earlier to get the full path to the image.

	$productSKU = $line_of_text[0];
	$ourProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU);
	$fileName = $line_of_text[1];
	$filePath = $importDir.$fileName;

Then we can check if the file exists – if it does, then set it to be the main, small and thumbnail images for the selected SKU, and save the product :

	if (file_exists($filePath)) {
		$ourProduct->addImageToMediaGallery($filePath, array('image', 'small_image', 'thumbnail'), false, false);
		$ourProduct->save();

If it doesn’t, then output to the screen the SKU of the product so we know which ones it hasn’t worked for :

	} else {
		echo $productSKU . " not done";
		echo "\n";
	}	

Then finish off the file :

}
fclose($file_handle);
?>

Upload this file as add-images.php in the web root of your site, then visit www.yoursite.com/add-images.php in your browser to activate it. All being well, you’ll get a blank output after some thinking – any SKUs that it hasn’t worked for, you’ll be told about it.