Tuesday, May 3, 2011

How to export image from swf with ActionScript3/PHP?

Hi there, i have another piece for more advanced guys than me ;)

I am developing a simple flash application to create your own coat of arms. When finished creating, it would be nice to save it as an image to a client's computer or to a database, i am not sure yet. The problem is making the image from swf content - i mean not the whole content, just a part of it, where the COA is. I have found some eamples on the web using some image encoders and PHP, but i cant't understand how it works. Could anybody be so kind to explain me the basic principles of this? I want to make it as simple as possible.

From stackoverflow
  • This seems to be a popular question lately. So I don't completely reiterate all that information, instructions for saving the image data, once you've obtained that as BitmapData, are here:

    http://stackoverflow.com/questions/597947/how-can-i-send-a-bytearray-from-flash-and-some-form-data-to-php/800053#800053

    But, first, to get the image data, you'll need to pull it out of whatever MovieClip/Sprite, whatever you have it in. To do this, you simply do:

    var myBitmapData:BitmapData = new BitmapData(desiredWidth, desiredHeight, isTransparent, backgroundColor); myBitmapData.draw(mcToBeSavedToImage);

    Where mcToBeSavedToImage is the movieclip you want to turn into an image. The BitmapData reference is here, if you need it - it has other examples: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/BitmapData.html

    Hope that helps!

  • First, draw the image (Sprite or MovieClip, etc) into a BitmapData:

    var b: BitmapData = new BitmapData(640, 480, false, 0xffffffff); 
    b.draw(mcToBeSaved);
    

    Then, use e.g. as3corelib to encode the BitmapData into PNG or JPG files.

    var ba: ByteArray = PNGEncoder.encode(b);
    

    Then, for Flash 10, you can immediately get the user to save the image to a file, using FileReference's save method.

    Dungeo : i think i understand, but it seems like saving only a single movieclip to an image, dont you know, how can i make this for a cut-off of the stage, lets say a rectangular area, that may contain more movieclips? something like a screenshot, but not a whole movie area... thanx anyway
    yuku : a stage is also considered as a displayobject, so you can draw the stage to the bitmapdata. use the matrix parameter of draw function to set the offset of the crop rectangle.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.