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.

  1. var version = '1.02';
  2. var allowedFiletypes = ['png', 'gif', 'jpg'];
  3. var separator = '/';
  4. var defaultImageQuality = 90;
  5.  
  6.  
  7. fl.outputPanel.clear();
  8. fl.trace('Welcome to the wonderful convert images to swfs script (version ' + version + ')');
  9.  
  10. // create new project
  11. fl.createDocument();
  12.  
  13. // shortcuts
  14. var doc = fl.getDocumentDOM();
  15. var library = doc.library;
  16.  
  17. // set to player 8
  18. doc.setPlayerVersion(8);
  19.  
  20. // select images to import
  21. var importFolder = fl.browseForFolderURL('Select the folder with images to convert');
  22. var importFolderContents = FLfile.listFolder(importFolder);
  23. var i;
  24.  
  25. // import files into library
  26. for (i = 0; i <importFolderContents.length; i++)
  27. {
  28.     var file = importFolderContents[i];
  29.     var fileURI = importFolder + separator + file;
  30.    
  31.     if (!isAllowedFiletype(file))
  32.     {
  33.         continue;
  34.     }
  35.    
  36.     fl.trace('Importing ' + filenameFromURI(fileURI) + ' ...');
  37.     doc.importFile(fileURI, true);
  38. }
  39.  
  40. // choose export folder
  41. var exportFolder = fl.browseForFolderURL('Select the folder to which you want to export the images to');
  42.  
  43. // choose image quality
  44. var imageQuality = prompt('Please choose image quality', '90');
  45. if (imageQuality == null)
  46. {
  47.     imageQuality = defaultImageQuality;
  48. }
  49. else
  50. {
  51.     imageQuality = parseInt(imageQuality);
  52.     imageQuality = Math.max(0, imageQuality);
  53.     imageQuality = Math.min(100, imageQuality);
  54. }
  55.  
  56. var items = library.items.concat();
  57. var mcsFolder = 'converted_movieclips';
  58. i = items.length;
  59.  
  60. // convert all images to movieclips and export them
  61. while (i--)
  62. {
  63.     var item = items[i];
  64.     if (item.itemType == 'bitmap')
  65.     {
  66.         // set image quality
  67.         item.quality = imageQuality;
  68.        
  69.         var imageName = item.name.split('.')[0];
  70.         var mcName = mcsFolder + '/' + imageName + '_mc';
  71.        
  72.         // create movieclip in library
  73.         library.addNewItem('movie clip', mcName);
  74.         // edit movieclip
  75.         library.editItem(mcName);
  76.         // attach image
  77.         doc.addItem({x:0.0, y:0.0}, item);
  78.        
  79.         // get reference on mc
  80.         var mc = library.items[library.findItemIndex(mcName)];
  81.         // get reference on attached image
  82.         var image = doc.getTimeline().layers[0].frames[0].elements[0];
  83.         // we need to reposition the image, otherwise it will be centered
  84.         image.x = 0;
  85.         image.y = 0;
  86.                
  87.         var exportPath = exportFolder + '/' + imageName + '.swf';
  88.         fl.trace('Exporting ' + filenameFromURI(exportPath) + ' ...');
  89.         mc.exportSWF(exportPath);
  90.     }
  91. }
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. /*
  101. * Helper methods
  102. */
  103.  
  104. function isAllowedFiletype(file)
  105. {
  106.     var suffix = suffixForFile(file).toLowerCase();
  107.     var i = allowedFiletypes.length;
  108.     while (i--)
  109.     {
  110.         if (allowedFiletypes[i] == suffix)
  111.         {
  112.             return true;
  113.         }
  114.     }
  115.     return false;
  116. }
  117.  
  118. function filenameFromURI(uri)
  119. {
  120.     var parts = uri.split(separator);
  121.     return parts.pop();
  122. }
  123.  
  124. function suffixForFile(file)
  125. {
  126.     var parts = file.split('.');
  127.     return parts[parts.length - 1];
  128. }

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.


About this entry