The simplest way to send an image from Flash to PHP without user interaction

With the FileReference class, it is quite easy to send an image to the server. But it requires user interaction. What if you need to send an image the user has drawn or captured form the webcam? Then send the data as a ByteArray and let the PHP save the raw data.

The bitmapdata of the image must be encoded to jpg or png and Adobe has classes for that: com.adobe.images.PNGEncoder and com.adobe.images.JPGEncoder. The contentType of the request must be ”application/octet-stream” and if you want to send parameters (file format, username, etc.), add them to the url as parameters: ”simpleSaveImage.php?fileformat=png&filename=cheetach.png”.

In the source files I’m using my ContentLoader to send the data. The sendBytes method  handles everything for you, but here’s also a summary of how to do it without the ContentLoader:
Jatka lukemista ”The simplest way to send an image from Flash to PHP without user interaction”

My new best friend – describeType

Praise to the flash.utils.describeType – a method that became my number one friend when I was validating parameters set by user and solving how to validate proper data types without knowing what they were.

So, what does flash.utils.describeType do? In a nutshell it converts any class into a xml that describes

What’s in your Utils?

The Utils class is a collection of simple methods that carry out small tasks such as data converting and mathematical calculations. They are very general (and usually static) methods and therefore reusable in multiple projects. Actionscript has an inbuilt utils package (flash.utils).  What’s in your own Utils? Or are you using some other Utils package?

My Utils class includes i.a.:

getAsset – gets a class even from externally loaded SWF-files

convertClassToObject – converts a class into an object by collecting all properties that are readable and writable. Appends also new values if assigned and returns data types of all properties. This method uses the flash.utils.describeType method to convert classes to XML objects. Read my praise to this method!

cloneBitmap – clones a bitmap

HEXtoARGB & ARBGtoHEX – these methods converts an ARGB value to  HEX values and vice versa Jatka lukemista ”What’s in your Utils?”