Author’s Update, June 1, 2010
This article describes how and why you need to modify the WebORB for PHP internal PHP code so that you can transfer photos from the PHP server to the Flex client, arriving as ActionScript 3 ByteArray data type. This internal PHP code modifications, described later in this article are no longer necessary. I recently identified a new way perform this photo data transfer by replacing the WebORB for PHP application server with Zend’s Zend Framework application server. Zend Framework supports photo data transfers without requiring internal code modification of PHP code within the framework. See my InsideRIA article Flex 4 / PHP Data-Centric Photo Transfers for details on how to replace WebORB for PHP with Zend Framework.
________________________
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 my SQGallery application article in InsideRIA, we were discussing a Flex-based photo rendering RIA named SQGallery 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 data type. 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? You are 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 SQGallery 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 of support 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:
- Is the string an arbitrarily long string-like 300 bytes?
- Do the first few header bytes of the PHP string indicate a JPEG or PNG photo?
If the string passed both of the above tests, then I simply transferred 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 SQGallery 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.
It’s perhaps not the cleanest of solutions, but it does the job. I do welcome suggestion of better solutions to handle ByteArray management of photo string data.
The specifics of the changes I made to WebORB for PHP are fully discussed and explained in two documents, “WebORB 3.6 AS 3 ByteArray Writer Design Notes” and “WebORB 3.6 PHP Class Module ByteArray Writer Update Instructions”. Click here to download these two documents along with the WebORB for PHP server PHP class modifications and additions.
Thank you for your help!
You are most welcome. It took a lot to figure all of this out and I thought it best to share what I learned.
You really make good articles I would say. This is the first time I visited your site and so far I am impressed with the research you made to make this article awesome. Good Job!
My name is Piter Jankovich. Only want to tell, that your blog is really cool
And want to ask you: is this blog your hobby?
P.S. Sorry for my bad english
@PiterJankovich
I have owned and managed Seaquest Software as a software development and publishing company for over 32 years. Now I’m semi-retired and working pro bono to share with others what I’ve learned over all these years.
I suppose one could say its now a hobby, but I don’t think of my endeavors that way.
I would appreciate more visual materials, to make your blog more attractive, but your writing style really compensates it. But there is always place for improvement
Over the last couple of years, Flash has gained a strong foothold in the online video industry. One of the greatest appeals of Flash is that it is compatible across all platforms. All you have to do is install a simple plugin, which usually comes with most browsers anyways. And now, with video becoming so prevalent on the web, the Flash Video format (FLV) is becoming the preferred format. Websites like YouTube.com are now using the FLV format for their videos, because with FLV files, you don’t have the same compatibility issues that you might encounter with AVI, MPEG, or MOV files.
@stuart
I’ll work towards more visuals in the future.
good article, i will add my feeds.
I didn’t understand the concluding part of your article, could you please explain it more?
@stauart,
I would be glad to explain more if you could help me a by being a bit more specific per the conclusion you do not understand.
Pete
I’ve been reading the information on your site for quite some time now, just wanted to do a quick post and say thank you for all of the useful information you have been providing your readers all this time.
@Elvin,
Thanks. I do enjoy helping others with the RIA development issues, even though its a lot of work. I’ll have a new article shortly. Watch for it.
Pete
Because of reading your blog, I decided to start my own. I had never been interested in keeping a blog until I saw how fun yours was, then I was inspired!
@Karole,
A blog is a lot of fun. And getting on deployed is easier than one might realize.
Pete
thanx for your tips..excellent
Hey, great, this is super stuff, hope to see more.Greetings
I’m generally not the guy to post comments on other’s blogs, but for this article I just needed to do it. I’ve been searching around your blog a lot nowadays and I’m super impressed, I think you could really emerge as the main opinions for your niche. Not sure what your schedule is like in life, but if you started committing more effort to writing on this site, I would bet you would begin receiving a bunch of visitors eventually. With affiliate stuff, it could become a nice second income stream. Just an idea to ponder. Good luck!
@April Severy
If you would read my blog About tab page, principally:
o The paragraph the starts with “Our recent interest and endeavors have been towards…”
and
0 My Mission Statement.
From the references above, you might catch on that my main goal is not necessarily to make lot of money from my endeavors. I’ve been in this business for nearly fifty years. I have owned my own software development and publishing company coming up on thirty-five years. The software industry has been very kind to me financially. My purpose now is to mostly give back to the industry, which provided me with financial comfort.
Give this and the noteworthy positive reader comments I am receiving in my blog comments, i.e., my comments are noteworthy and interesting to others, I am going to make an additional effort to post more interesting photo work flow, et al. information here. I will also have a new article Flex 3 / Flex 4 article published on InsideRIA soon. Stay tuned.
This really is a really wonderful resource that you are providing and you also provides it away 100 % free. I enjoy seeing sites which understand the significance of providing a excellent resource for free. I really loved reading your post. Thanks!
@internet business
You are more welcome. I am glad to be above to provide worthwhile development information for others.
Pete
Rather cool post. I recently stumbled upon your weblog and needed to say that I have really loved browsing your blog posts. In any case I am going to be signing up for your information aggregator and I hope you write again in the near future!
@hosting
I will post some interesting new photo gallery topics shortly. Stay tuned.
Pete
Hello, I have been viewing your site for some time now and read most of your entries. Is there any way that I can subscribe so I get updates sent to my email? Regards
@Panic
We do not support the WordPress subscribe to content feature at this time. We may support this feature sometime in the future if noticable need arises.
Nice!
Hello, this is my first time i visit here. I found so many interesting in your blog especially on how to determine the topic. keep up the good work.
@ kopi tribulus
Thank you. I try.
Pete
I would like to say “wow” what a inspiring post. This is really great. Keep doing what you’re doing!!
@ Java Jobs
Okay, will do. See my two recent posts per new articles published on O’Reilly InsideRIA.
Pete
Hey, I’m having a problem viewing your site in my browser. Could you please check this. My browser is Opera 7 btw.
@ splitx bowling
My Web site blog works fine here with Mac OS X Safari and Firefox Web browsers.
Pete
Rather cool post. I recently stumbled upon your weblog and needed to say that I have really loved browsing your blog posts. In any case I am going to be signing up for your information aggregator and I hope you write again in the near future!
@hosting
I will post some interesting new photo gallery topics shortly. Stay tuned.
Pete
Great story, thanks. Could you explain the third paragraph in more detail please?
@ Carey Belser
Sure, I’ll try. To transfer a photo from the Web application server to the Flex client it is desirable to have a PHP array of bytes data type so the the Flex client can received this photo as an ActionScript 3.0 ByteArray data type. PHP does not have a byte array data type. The closes PHP data type is String. Accordingly, internal to the Web application server we watch for a (very long) string data type and if the long string has the right photo data header bytes, the we internally convert the photo data type to a ActionScript 3.0 ByteArray data type. The Flex 3 or 4 built client can conveniently render photos that are received as AS 3.0 ByteArrays.
Pete
Thank you for another informative blog. Where else could one get that kind of information written in such a perfect way? I have a presentation that I am just now working on, and I have been on the look out for such info.
@ windsurfing formula
Good! I hope this information is of help for you. Be sure to also check out my two new articles: Photo Gallery Mate Framework, With Photo Caching and Flex 4 / PHP Data-Centric Photo Transfers
Amazing post! I initially found your blog a week or so ago, and I subscribed to your RSS feed the very same day. I have several cool ideas for some upcoming posts you could write. I’ll send you an email shortly. Keep up the great work.
While this subject can be very touchy for most people, my opinion is that there has to be a middle or common ground that we all can find. I do appreciate that you’ve added relevant and intelligent commentary here though. Thank you!
You have done it again. Superb post!
This is very moving work you have created for us. Some people need to know that these things can occur to anyone. You have given me a better position now.
Really decent post… I love it. Keep ‘em coming…
@ Jim M.
See new posts today, June 1, 2008.
Pete
Have to take pleasure in the effort you add into your blog
A friend of mine recommended me to visit your site. This is exactly the kind of information I was looking for. I wish I have encountered your blog sooner.
@ negotiating skills
Glad that you found us.
Pete
Really decent post… I love it. Keep ‘em coming…
@ Emily
Will do.
Pete
Nice write up. I usually I never reply to these thing but this time I will, Thanks for the great info
@ Snmaster Idx
Nice to hear from the rare comment posters. This means a lot to me.
Pete
Excellent post I must say.. Simple but yet interesting and engaging.. Keep up the awesome work!
@ uspto patent
With the feedback I’m getting, I’m compelled to post some more examples.
Pete
lol ok so here’s exactly how stupid I am, halfway through reading your post I accidentally dropped my computer mouse and closed the site in error and I couldn’t locate your web page again until 5 days later on to finish reading from the point i stopped at due to the fact I forgot how I linked to your blog to begin with haha anyway it was worth the delay..thanks
Brilliant blog posting. I found your post very interesting, I think you are a brilliant writer. I added your blog to my bookmarks and will return in the future.
Amiable fill someone in on and this fill someone in on helped me alot in my college assignement. Thanks you as your information.