HummingbirdUK main logo

HummingbirdUK

Coding solutions to business problems

About us

We use code to create solutions to business challenges, bottle-necks and headaches.

If you think your business has a problem that can be solved through code, we are happy to chat things through without any obligation.

Get in touch

Add images to products programmatically in Magento

Home / Blog / Add images to products programmatically in Magento

Written by Giles Bennett

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] <?php require_once 'app/Mage.php'; Mage::app(); Mage::app()->getStore()->setId(Mage_Core_Model_App::ADMIN_STORE_ID); [/php] 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.. [php]$importDir = Mage::getBaseDir('media') . DS . 'incoming/'; [/php] 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 : [php]$file_handle = fopen("images.csv", "r"); [/php] Now let's loop through the CSV : [php]while (!feof($file_handle) ) { $line_of_text = fgetcsv($file_handle, 1024); [/php] 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. [php] $productSKU = $line_of_text[0]; $ourProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$productSKU); $fileName = $line_of_text[1]; $filePath = $importDir.$fileName; [/php] 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 : [php] if (file_exists($filePath)) { $ourProduct->addImageToMediaGallery($filePath, array('image', 'small_image', 'thumbnail'), false, false); $ourProduct->save(); [/php] If it doesn't, then output to the screen the SKU of the product so we know which ones it hasn't worked for : [php] } else { echo $productSKU . " not done"; echo "\n"; } [/php] Then finish off the file : [php] } fclose($file_handle); ?> [/php] 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.
Author : Giles Bennett

About the author

Giles Bennett built his first website in 1996, and is old enough to miss Netscape Navigator. Initially a lawyer, he jumped ship to IT in 2008, and after 5 years as a freelancer, he founded HummingbirdUK in 2013. He can be reached by email at giles@hummingbirduk.com.