Convert images to swfs with JSFL
One of the downsides of SWFMill (probably the only one) is, that it doesn't compress pngs, so things can get big pretty soon. If you need to convert a bunch of images into swfs and need transparency but compression, you can use the following script to achieve that by automating flash.
-
var version = '1.02';
-
var allowedFiletypes = ['png', 'gif', 'jpg'];
-
var separator = '/';
-
var defaultImageQuality = 90;
-
-
-
fl.outputPanel.clear();
-
fl.trace('Welcome to the wonderful convert images to swfs script (version ' + version + ')');
-
-
// create new project
-
fl.createDocument();
-
-
// shortcuts
-
var doc = fl.getDocumentDOM();
-
var library = doc.library;
-
-
// set to player 8
-
doc.setPlayerVersion(8);
-
-
// select images to import
-
var importFolder = fl.browseForFolderURL('Select the folder with images to convert');
-
var importFolderContents = FLfile.listFolder(importFolder);
-
var i;
-
-
// import files into library
-
for (i = 0; i <importFolderContents.length; i++)
-
{
-
var file = importFolderContents[i];
-
var fileURI = importFolder + separator + file;
-
-
if (!isAllowedFiletype(file))
-
{
-
continue;
-
}
-
-
fl.trace('Importing ' + filenameFromURI(fileURI) + ' ...');
-
doc.importFile(fileURI, true);
-
}
-
-
// choose export folder
-
var exportFolder = fl.browseForFolderURL('Select the folder to which you want to export the images to');
-
-
// choose image quality
-
var imageQuality = prompt('Please choose image quality', '90');
-
if (imageQuality == null)
-
{
-
imageQuality = defaultImageQuality;
-
}
-
else
-
{
-
imageQuality = parseInt(imageQuality);
-
imageQuality = Math.max(0, imageQuality);
-
imageQuality = Math.min(100, imageQuality);
-
}
-
-
var items = library.items.concat();
-
var mcsFolder = 'converted_movieclips';
-
i = items.length;
-
-
// convert all images to movieclips and export them
-
while (i--)
-
{
-
var item = items[i];
-
if (item.itemType == 'bitmap')
-
{
-
// set image quality
-
item.quality = imageQuality;
-
-
var imageName = item.name.split('.')[0];
-
var mcName = mcsFolder + '/' + imageName + '_mc';
-
-
// create movieclip in library
-
library.addNewItem('movie clip', mcName);
-
// edit movieclip
-
library.editItem(mcName);
-
// attach image
-
doc.addItem({x:0.0, y:0.0}, item);
-
-
// get reference on mc
-
var mc = library.items[library.findItemIndex(mcName)];
-
// get reference on attached image
-
var image = doc.getTimeline().layers[0].frames[0].elements[0];
-
// we need to reposition the image, otherwise it will be centered
-
image.x = 0;
-
image.y = 0;
-
-
var exportPath = exportFolder + '/' + imageName + '.swf';
-
fl.trace('Exporting ' + filenameFromURI(exportPath) + ' ...');
-
mc.exportSWF(exportPath);
-
}
-
}
-
-
-
-
-
-
-
-
-
/*
-
* Helper methods
-
*/
-
-
function isAllowedFiletype(file)
-
{
-
var suffix = suffixForFile(file).toLowerCase();
-
var i = allowedFiletypes.length;
-
while (i--)
-
{
-
if (allowedFiletypes[i] == suffix)
-
{
-
return true;
-
}
-
}
-
return false;
-
}
-
-
function filenameFromURI(uri)
-
{
-
var parts = uri.split(separator);
-
return parts.pop();
-
}
-
-
function suffixForFile(file)
-
{
-
var parts = file.split('.');
-
return parts[parts.length - 1];
-
}
If you open this script via Commands > Run Command, a file dialog pops up, where you need to choose the image source folder. Every gif, png and jpg in that folder will be imported. After that a new file dialog pops up, to select the target folder for the exported swfs. Finally you can choose the image quality (0-100) in the third dialog. The images will be exported to the target folder as swfs and done.