Automated Flash Adverts from a CSV
Published: April 8th 2014
One of those moments where you are handed a project and you're not entirely sure it's possible happened to me last week. In the course of my R&D-esque role I had to look at the possibility of automatically generating Flash adverts for hundreds of products ready for online advert campaigs
Putting the ground-work in
Having a scour around the Internet with one of the bosses at work we stumbled across the swfmill project which aims to bring command-line compilation to the masses. This looked like it could be a possibility so I set out trying to compile all the necessary components into the operating system. It turns out you need a lot of the development versions of the packages in order to get the software up and running but after a certain amount of fiddling around and apt-get install tomfoolery I finally had a working compilation engine (all-be-it rough around the edges at this stage).
CSV to SWF
The primary focus of the project was to take a CSV file with a list of adverts, images to be used in the advert and text to place on it and turn it into a group of adverts. The next steps involved taking a CSV and associated image resources and running some processing on it before passing data to the compilation engine using a shell exec command.
Dealing with the CSV in PHP is pretty straightforward nowadays (certainly not the chaos it used to be!) so I set about creating the code to read the CSV, draw the data it needed to and then start to generate the XML needed by Swfmill./*
* Convert CSV to associative array for ease of manipulation
function csv_to_array($filename='', $delimiter=',')
// Check to see if I can open the damn thing
if(!file_exists($filename) || !is_readable($filename))
// Start pulling the data and populating the associative array
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
$header = $row;
$data = array_combine($header, $row);
For each advert (each line of the CSV) I created a folder on the file system and copied the correct image resources into the folder. I then wrote the XML file to the folder before moving on to the next advert. Once at the end of the file I worked through each folders, creating the SWF file using Swfmill.$CreateSWF = "swfmill simple ".$FullSystemLocation."/advert.xml ".$FullSystemLocation."/".$filename.".swf";
At the end of all of the creation process I created a new zip file and worked through each folder to add the SWF file only to the zip file. This zip file was then provided as a link to the user and the temporary files I generated removed from the system.
The system is still pretty rough and ready at the moment but there is scope to bring in templates for flash files and add in frame-by-frame animation or text/image changes as necessary.
Not a bad couple of days worth of hacking and made possible through Open Source tools and libraries. Proof of concept is there now and it's just a case of tying up the loose ends!
Next Item.. Automated Text Summarisation
Previous Item... Baking a ServerGrid Raspberry Pi