Flash ActionScript 3.0 Flash to PNG Exporter Class (Ver 0.2)

Export PNG images from Flash with ActionScript 3.0 in the easiest way possible.

Author: Muhammad bin Yusrat (www.muhammadbinyusrat.com)

Description: Makes an easy to export PNG Images from with in Flash. You can take a snapshot of any MovieClip (or the entire stage) and tell the class to export it to PHP which then saves it on the server.

Important: The class has now shifted to the Flash ActionScript to PNG Export Class page on Flashiology. The page you are reading may be out dated.

Functions:


This function accepts two arguments. One is the target MovieClip that needs to be snapped. Second is an optional Rectangle (flash.geom.Rectangle) which, if passed, will crop the image to that rectangle.


Once the movieclip has been snapped you can simply call this second function and give it the URL to the PHP Page (or any other server side language, sample PHP included in the files) and it will send the image to it. Basically it encodes the image to Base64 which can easily be decoded using PHP, ASP or any other language without having to install any specific component on the server. This DOES NOT require GD Library with PHP. If you do not know what I am saying, don't worry.. just read on.

This function has 1 primary and 3 optional arguments.

1. PageURL: The data will be sent to this page. Pass it as a string. Required.
2. onComplete: A function to be invoked once the image has been send successfully. Optional.
3. ProgressFunction: In case you need to show the user some progress of how much image has been uploaded, this parameter can be used. It passes a ProgressEvent to the function. Optional.
4. vars: Just in case you need to send some additional POST variables (Session IDs, etc..) to the receiving page, you can use this parameter. See below examples. This is also optional.

Here is the easiest way to export PNG image with ActionScript 3 using this class:

import com.muhammadbinyusrat.PNGExport;

var exporter:PNGExport = new PNGExport();
exporter.Snap(testClip);
exporter.SendTo("getImage.php");

Crop and onComplete Example:

import com.muhammadbinyusrat.PNGExport;

var exporter:PNGExport = new PNGExport();
var cropArea:Rectangle = new Rectangle(50,50,100,150);
exporter.Snap(MovieClip(root),cropArea); //This will Snap the entire page and crop it according to the rectangle.
exporter.SendTo("getImage.php",ShowWhenImageSaved);

function ShowWhenImageSaved(e:Event):void{
	trace("The image has been saved..");
  trace("PHP Said:" + e.target.data);
}

ProgressEvent Example:

import com.muhammadbinyusrat.PNGExport;

var exporter:PNGExport = new PNGExport();
exporter.Snap(SomeMovieClip);

exporter.SendTo("getImage.php",null,ShowProgress); //second argument is null because I don't want to specify a onComplete function in this example.

function ShowProgress(e:ProgressEvent):void{
	trace(int((e.bytesLoaded/e.bytesTotal)*100)); //this should trace from 0 to 100 while the image is loading..
}

Custom Variable Example:

import com.muhammadbinyusrat.PNGExport;

var exporter:PNGExport = new PNGExport();
exporter.Snap(SomeMovieClip);
var ExtraVariables:Array = new Array();
ExtraVariables[0] = {define:"Username",value:"ibnYusrat"};
ExtraVariables[1] = {define:"SessionID",value:"FFWEIE23920800012343JIFWK9V932CC"});

exporter.SendTo("getImage.php",null,null,ExtraVariables); //This will send two extra variables called "Username" and "Password" with the values as specified above.

Download:
PNGExport.zip


If you guys have any questions, email me. I'll see you next time.