3 April 2008

Using AS3 to Upload and Encode Images

Since launching Logobama, we’ve had a number of requests for the project source code. The process of saving images from Flash turns out to be a relatively simple one with Actionscript 3, and after some searching, uploading Flash encoded JPEGs to a web server was pretty simple as well.

With the Logobama project, we had three main objectives:

  1. Upload an image into a Flash file
  2. Encode a JPEG with AS3
  3. Post the encoded image to a web server

In the case of Logobama, we also posted the saved image to a Flickr gallery, which was done using the phpFlickr library created by Dan Coulter. Thumbnails are automatically created with phpThumb.

The main engine behind this project is a combination of elements from the Adobe AS3 Corelib, and the very handy UploadPostHelper Class created by Jonathan Marston. There is also a small PHP upload script included in the source, along with TweenLite, which has quickly become our favorite tweening engine.

The basic system within the code nearly mirrors the Logobama project objectives:

1. Use the FileReference Class to upload an image:

var file : FileReference = new FileReference();
file.browse( new Array( new FileFilter( "Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png" ) ) );

2. Create a snapshot of the flash movie with the BitmapData.draw() method:

// set up a new bitmapdata object that matches the dimensions of the captureContainer;
var bmd : BitmapData = new BitmapData( captureContainer.width, captureContainer.height, true, 0xFFFFFFFF );

// draw the bitmapData from the captureContainer to the bitmapData object;
bmd.draw( captureContainer, new Matrix(), null, null, null, true );

3. Use the Adobe AS3 Corelib to encode a JPEG:

// create a new JPEG byte array with the adobe JPEGEncoder Class;
var byteArray : ByteArray = new JPGEncoder( 90 ).encode( bmd );

4. Post the encoded image to a web server with the help of the UploadPostHelper Class and a basic PHP Upload Script (included in the source files):

// set up the request & headers for the image upload;
var urlRequest : URLRequest = new URLRequest();
urlRequest.url = 'image.php?path=images';
urlRequest.contentType = 'multipart/form-data; boundary=' + UploadPostHelper.getBoundary();
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = UploadPostHelper.getPostData( 'image.jpg', byteArray );
urlRequest.requestHeaders.push( new URLRequestHeader( 'Cache-Control', 'no-cache' ) );

// create the image loader & send the image to the server;
var urlLoader : URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.load( urlRequest );

So there you have it, an easy way to upload images into flash, edit them, and post the encoded JPEG result to a web server without the need for any fancy remoting or server-side computation.

Since all the above code is available under a Creative Commons Share Alike license (or similar), we’ve decided to make our source code for this project available as well. The functionality of this example was intentionally minimized to showcase the features outlined in this post. Enjoy, and please feel free to reply with comments, questions, or revisions.

posted by Shaun Tinney

thinking about… AS3, Flash, PHP

10 Comments…

  1. Dane said…

    Thanks for sharing Shaun, I’m sure this will come in handy some day.

    10:51 am / April 3rd, 2008

  2. Steve Jones said…

    Thanks a lot for this - very neat stuff indeed, and my just be the post that saves my lily white ass.

    I owe you one.

    5:42 am / April 10th, 2008

  3. Jonathan said…

    You are the man! This is the single best post I’ve found for image upload/export using flash. Thanks so much.
    I’ve been trying to do all of this in AS2 and PHP. It’s hard to believe you can do all of this inside AS3.

    5:34 am / April 26th, 2008

  4. […] http://labs.findsubstance.com/2008/04/03/as3-upload-encode-images/ […]

    3:11 pm / April 30th, 2008

  5. We have used this process and had great success.

    One caveat for using this approach in Internet Explorer and any ActiveX wrapper… In these instances, the player doesn’t seem to like some basic response headers. Specifically we were trying to return a RESTful ‘201 (Created)’ header and Flash Player would throw a #2032 IOError. It was solved by using the basic ‘200 (OK)’ response header.

    Probably not a common issue, but it took a long time for us to figure out what was going on.

    Thanks, Shaun

    4:51 pm / May 2nd, 2008

  6. cosmoz said…

    Thank you very much, the code work like a dream. Again thanks for shared.

    A

    12:08 am / May 6th, 2008

  7. Sly said…

    Good example !
    Is it possible to add a “loading bar” to show the progress of the upload ? I suppose that yes. I will try.

    6:47 am / May 22nd, 2008

  8. […] so of course it didn’t work and I ended up having to piece together (very helpful) bits and bobs from various sources until eventually something somewhere clicked into […]

    9:23 am / June 4th, 2008

  9. Thank you for posting these extremely helpful classes and tips. I thought I would point out, however, that the line starting with “file.browse…” should have 3 closing parens.

    Thanks,
    -Dan

    6:11 am / July 3rd, 2008

  10. Shaun Tinney said…

    Thanks Daniel, updated.

    6:29 pm / July 4th, 2008

Have a comment?