Here is a demo for something that I hope may prove useful for some people. I have written a BitmapExporter class which allows to save PNGs or JPEGs out of swf files. There have been some Flash 8 demos already that show this possibility, so the novelty factors of my class are that the images get compressed before they are sent to the server and that Flash remoting is used to transfer the data. On the server side I’m running this with AMFPHP (I have exchanged that for a regular PHP script which works with loadVars and POST requests) in conjunction GD (which comes usually pre-installed with PHP) to recreate the images.
The demo allows you to doodle some drawings and to save them to your hard drive as a JPEG. Because of its security sandbox Flash cannot access your harddrive directly. It first has to send the image data to the server and then commence a download from there. We are so used to the small filesizes of JPEGs or PNG that it’s quite surprising to see what size an uncompressed image really has. The canvas in this demo for example has a dimension of 500×425 pixels. That’s 212500 pixel x 4 bytes per pixel = 850000 Bytes or about 830K.
The challenge is to create a compression algorithm in ActionScript which will process the image data with reasonable speed without overexerting Flash. I didn’t want to start studying the JPEG or PNG format specifications, so I rolled my own method. It uses an indexed color palette (but without reducing the amount of colors) and a run-length encoded table of the offsets of the colorindices of the consecutive pixels (wow, that sounds complicated). In reality that means that it will compress noisy images not so good as graphics. Therefore I have built in an optional method to reduce the bit-depths of images which does improve the compression ratio for photos and webcam shots.
The image is not necessarily processed in one block – the blocksize can be varied based on package size as well as processing time. Doing this the Flash plugin remains responsive and on top of that it allows for a bit “parallel processing”: whilst the server is receiving data and creating the first part of the image (which takes the longest time in the process), the swf file is already compressing the next data block. Now isn’t that clever?
So here comes the demo. You can vary the brush’s start and end size by using the arrow keys on your keyboard. Clicking in the palette will change the brush color. And one more thing: please be kind with my traffic bill – this is a functional demo, please don’t use it to save hundreds of images. Even though the images do get compressed it still uses up quite some bandwidth if thousands of people do this.
Source file here
[Update:] Here’s the link to the source code: http://www.quasimondo.com/scrapyard/BitmapExporter_v22.zip
Thanks Quasimondo to share his stuff…
More details about him http://www.quasimondo.com/