by Pete Mackie
This article is a companion piece to Pete Mackie's "The SQPhoto App: Rendering Photos Dynamically with Flex and PHP", which was published in Flex Authority Vol. 2 Issue 1. For a complete view of all of the ActionScript code involved in the application, download and install the SQPhoto Flex client Flex Builder project at http://www.flex-authority.com/issues/integrating-flex/code/.
Transferring photos from the PHP server to the Flex client via AMF 3 RemoteObject data streams is currently one of my favorite RIA topics. That's because it took me more than a few days to figure out why I was able to display my photos when they were on the PHP server, but I could no longer display them by the time they were received by my Flex client. I'm anxious to share what I know about this in the hope that others do not have to go through what I did to get this working properly. The subtleties of what happens during the server-to-client photo data transfer are enough to drive you up the wall.
Here's the scenario, what you'll require and the specific solution that helped me.
In the SQPhoto application article in Flex Authority, we were discussing a Flex-based photo rendering RIA named SQPhoto that pulls the photos from a MySQL database with Action Message Format (AMF3) Messaging through the WebORB for PHP application server. The dilemma is that the PHP data types are somewhat restricted in what's available to meet our needs. Principally, the PHP data types are four scalar types: boolean, integer, float (floating-point number, a.k.a. 'double') and string; two compound types: array and object; and finally two special types: resource and NULL.
When the WebORB PHP application server loads photo data — effectively an array of bytes — from the SQPhoto MySQL database, PHP insists on typing photos as a string datatype. Ah, I thought, all sounds well and good. I will simply return the photo PHP string photo byte array to the Flex client, receiving the photo data as an ActionScript 3 (AS3) String object. Then I will cast the String AS3 object to a ByteArray AS3 object so that the photo can be rendered and displayed using the Loader AS3 class.
The above is a great idea, but it did not work. I wondered why. After all, PHP strings are AS3 Strings ... or are they? The AS3 String objects are formatted UTF-8, which means single-byte strings — the same as PHP server-side strings, right? Wrong by 100%. In ActionScript, if the eighth bit (bit 7) of any byte is set (1), then the UTF-8 single byte is extended to a two-byte UTF-8 sequence. And guess what? Many of the photo string (array of bytes) have the eighth bit set. Talk about messing the photo content up! No wonder photos won't display in the SQPhoto Flex client.
I thought of an easy fix. Just cast the server-side PHP photo string to a ByteArray and receive the photos as AS3 ByteArray objects on the Flex client. After all, I know by looking at Adobe's AMF 3 specification that the ByteArray data type is supported.
Two issues broke what I thought was a clever solution. First, while there is no PHP compile error, casting photos to PHP (ByteArray) does not work. PHP still insists that my photo array of bytes is a string. That's because there is no ByteArray data type in the PHP language syntax.
The lack of a ByteArray (or, for that matter, a byte) data type in PHP is the cause of the second issue as well. While the AMF 3 specification says yes to supporting ByteArray data types, WebORB for PHP does not support this data type. One cannot support what does not exist.
So there I was, trapped with a neat server-to-client photo search request to render and display photos with no way to transfer the photo data. Panic set in! Should I just scrap my code and move on to another endeavor-or jump from PHP to using Adobe's ColdFusion or Adobe's BlazeDS application servers? I knew these two server solutions both directly support AMF 3 RemoteObject data transfers.
But wait, there's more! Eureka, at last I came up with a solution, albeit a bit of a kludge, that would surely work. I knew that JPEG and Portable Network Graphic (PNG) photos each have a unique set of identifying header bytes at the start of the photo byte data. Furthermore, the format specifications of these photo header bytes are easily obtainable via a couple of Google searches.
I couldn't find an available AMF 3 ByteArray writer within the WebORB application server. So first I developed an AMF 3 ByteArray writer class in PHP. The writer class is simple and short because all it needs to do is call other WebORB code classes to perform the work. Next, I went inside the WebORB classes to find the AMF 3 String Writer class. (Hint: All the WebORB AMF 3 Writer classes are in the Writer folder.) I located the AMF 3 String Writer in WebORB's MessageWriter class. Next, I added PHP code to intercept the AMF 3 String before it's serialized into an AMF 3 stream and then performed two tests, both of which must be true before moving forward with any translation action:
If the string passed both tests, then I simply transfered the AMF 3 String to my newly-written WebORB and AMF 3 ByteArray writer class, which serializes it into a data stream and sends it on its merry way to the SQPhoto Flex client.
Guess what? My mapping of the AMF 3 Strings to AMF 3 ByteArrays worked the first time I tried it with a server-to-client photo transfers. After nearly a month of on-and-off frustration, I figured out the issues involved and came up with a solution.
The elapsed time to figure out all of the above and get it working was just short of a month. I'm not one who gives up easily when the programming syntax and supporting framework don't do what I want. Needless to say, it was a good feeling when it all finally worked.
When dealing with server-to-client PHP-based AMF 3 transfers, I hope you will remember the code chicanery I came up with and discussed. And please pass this information along to others who may come upon this barrier of PHP server to AMF 3 client photo data transfers.
The specifics of the changes I made to WebORB for PHP are fully discussed and explained in two documents, "WebORB 3.5 AS3 ByteArray Writer Design Notes" and "WebORB 3.5 PHP Class Module ByteArray Writer Update Instructions". You can download these documents with the code for the SQPhoto App article, available at http://www.flex-authority.com/Issues/integrating-flex/code/.
Pete Mackie has long been a software developer and publisher. He first worked for nearly twenty years with a large corporation. About thirty years ago, he founded his own company, Seaquest Software, which is still going strong. His recent development efforts include work as a volunteer code committer for the Eclipse IDE and work on PHP-based RIA web sites for small start-up companies. Pete stumbled upon Adobe's booth while attending EclipseCon 2006. Wondering what Adobe was doing at an open source conference, he stopped at the booth and discovered Flex Builder running on the Eclipse IDE. Pete instantly recognized what Flex could do for RIA client/server applications. Rushing home, he installed the Flex Builder 2 beta and has been hooked on Flex software development ever since.
To get away from the computer keyboard and screen, he spends time hiking in the US Pacific Northwest wilderness or beachcombing along the sandy beaches where Oregon meets the Pacific Ocean. And he also does a bit of exercising effort on his Wii Fit.