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”

Versatile content / media loader: ContentLoader.as

ActionScript 3 introduced new ways to load content and new types of content – now Flash can handle raw bytedata. This also increased the complexity of the process. You need a Loader, URLLoader, URLRequest, event listeners…

ContentLoader is a class that simplifies the process into a one class that can fulfill most of the basic needs:

– send and load data

– load bytedata

– load variables

– hande errors

– load images, mp3s, xml, text (JSON too, of course, but parsing is not supported)

Example: Jatka lukemista ”Versatile content / media loader: ContentLoader.as”

Loading and handling external files in Flash: ExternalAssets.as

Here’s another helper class that loads one or multiple file(s) separately or all in a batch. There is one special feature: this class can check the filesize of each file (without fully loading them) and therefore it can show the loading progress of the whole batch not just single file. The loaded data is stored so the file or its size is never loaded twice.

This class uses my ContentLoader as the actual file loader. Read more about the ContentLoader »

Example: Jatka lukemista ”Loading and handling external files in Flash: ExternalAssets.as”

Facebook API – Asking permissions and posting to user’s wall

The Facebook API, one of the most used poorly documented API ever made. Yahoo Maps traumatized me years ago and won the first ”great service killed by poor documentation” trophy. Facebook API is now the new King in this area. The Graph API in much much better, but the documention is still below acceptable

Here is a working FBML example for your Facebook Page on how to ask permission from the user to post to his feed and sending the post after it. Jatka lukemista ”Facebook API – Asking permissions and posting to user’s wall”

Loading an SWF and getting components from its library

Keeping all assets in one SWF causes problems while updating assests and compiling times are longer. Nowadays only crazy people add everything into a one SWF and the Flash platform has improved a lot to support alternative ways to embed, load and add assets.

The flash.utils.getDefinitionByName allows to load assets from the library of another SWF-file. I’ve simplified the process in my Utils.as and here is an example how to use it.

Main thing is to use the ApplicationDomain to keep the loaded SWF in its own domain. ApplicationDomain allows multiple definitions of the same class to exist and allow children to reuse parent definitions.
Jatka lukemista ”Loading an SWF and getting components from its library”

Spotify

It’s now pretty dark in Finland and we’re  is still getting less and less daylight until southern Finland gets about 6 hours of daylight in December. These dark mornings need something to ease the waking up process!!! I’m using Spotify Premium on my phone and this morning I wished I could use it as an alarm clock – setting up the time and a song in the evening and waking up to some great song in the morning. Wouldn’t that be great?

And a second wish to Spotify (Premium or not): rating songs. One star or no stars isn’t enough. I’m getting so many playlists and songs that it’s harder and harder to remember the great ones from the good ones.

TIP: I started to have wayyyyyy too many playlists in the sidebar and it was a mess, so I made album groups by making a new playlist like ”Rock album links” and added only the first song of each album to that playlist. And Spotify makes it easy to jump to albums from there. And because I like order, I also created empty playlists named ”—————” and use them to divide playlists in to groups by genre.

The ultimate VideoLoader with rtmp support

One of the best Flash production houses in Helsinki/Finland made a quite surprising comment while making a project with us: their own Flash video player can’t handle live streams. That functionality is very easily added and I’ve used my VideoLoader class (now version 4.X) in various video projects over the years.

Maybe someone else is missing a versatile video loader too, so here’s one. And I say Loader, because this class loads, plays, pauses, buffers, sends events, handles errors and does everything you need for handling video files, but it does not have any UI. But that’s what extends VideoLoader is for! Jatka lukemista ”The ultimate VideoLoader with rtmp support”

Tip: use bitwise operators to store multiple values in one value

Bitwise operators (AND, OR, NOT, XOR) and especially the AND (&) operator is a wonderful way to store multiple values in to one value. Sounds illogical, but here’s how it works: values are stored as powers of two – 1, 2, 4, 8, 16, 32… This way  only  one byte of a binary presentation of a number changes when numbers are combined. And checking if a byte is 1 or 0 is very easy and fast with bitwise operators.

An example scenario: user management

Users  have different rights in CMS systems. Storing a ”yes”/”no” value for each possible user right is tiresome and adds a lot of fields in the database. It also makes building SQL clauses cumbersome and causes extra pain when a new field should be added. Storing all user rights in one number makes life easier and one of the biggest advantages is that bitwise operators work in SQL clauses too. Jatka lukemista ”Tip: use bitwise operators to store multiple values in one value”

Global data storage: Registry.as

Singleton classes can be accessible from anywhere anytime. It’s like the ”good” old _global. It behaves like a static class, but it is persistent so it can store data that can be retrieved from any class, object or component.

I also added some static functions to get around calling the usual ”getInstance()” everytime the class was needed.

The Registry class is a global data storage accessible with methods ”get” and ”add”. Jatka lukemista ”Global data storage: Registry.as”

AS3 MagicWand

In one concept for a webshop I designed a tool where the user could fit clothes on her own image. One problem – images are not transparent, there was always a background, but usually a solid color. Well, the magic wand in Photoshop would be useful, but too many images to go through then and everytime new items are added. So I studied if Flash could be used and voilá: the MagicWand.as was created. Jatka lukemista ”AS3 MagicWand”