I think I’m in love with Felicia Day

I’ve never met her but I think I have a teenage crush on her anyway. If you haven’t seen The Guild yet, do it now!

Season one

Ps. We’re the same age too. That has to mean something, man! Ds.

  • Share/Bookmark

Create iPhone apps using Flash

I’m so excited about the upcoming Flash Pro CS5. It will let you create native iPhone applications by exporting your ActionScript 3 movie from Flash. The app will, when it’s been exported, behave as any other app and can be uploaded to the AppStore etc.
Read more on Adobe Labs

  • Share/Bookmark

Google Maps Flash API is sooo fun

I’m implementing some Google Maps magic into a RIA for a client. Together with an auto-location thing using IP numbers the results are indeed stunning. I love Google’s API’s!

Google Maps API for Flash Developer Guide

  • Share/Bookmark

phpThumb and imagerotate()

One of the PHP libraries I use the most is phpThumb, because of its ease to use and portability. Simply putting the folder in the web root is usually sufficient to get started. One thing has annoyed me for a while though, and it is that I couldn’t get the rotating commands to work.

After some research I discovered that the imagerotate() function in GD library (a library that handles many of the graphics processing in phpThumb) is only available in the bundled version of GD library (that is: the version that sometimes comes bundled with PHP). That is not the case for me and I have my own server with lots of sites on, and didn’t feel like recompiling PHP just to get the imagerotate function to work.

So, in this new light, I looked for a function replacement using ImageMagick instead (a much better image processing library, but rarely installed by default) and found this one somewhere. Simply put it at the bottom of your phpthumb.functions.php file and *poof* rotating images in phpThumb will start working.

if ( !function_exists( 'imagerotate' ) ) {
  1.     function imagerotate( $source_image, $angle, $bgd_color=null ) {
  2.         $angle = 360-$angle; // GD rotates CCW, imagick rotates CW
  3.         $temp_src = '/tmp/temp_src.png';
  4.         $temp_dst = '/tmp/temp_dst.png';
  5.         if (!imagepng($source_image,$temp_src)){
  6.             return false;
  7.         }
  8.         $imagick = new Imagick();
  9.         $imagick->readImage($temp_src);
  10.         $imagick->rotateImage(new ImagickPixel(), $angle);
  11.         $imagick->writeImage($temp_dst);
  12.         //trigger_error( 'imagerotate(): could not write to ' . $file1 . ', original image returned', E_USER_WARNING );
  13.         $result = imagecreatefrompng($temp_dst);
  14.         unlink($temp_dst);
  15.         unlink($temp_src);
  16.         return $result;
  17.     }
  18. }
  • Share/Bookmark

Maintaining state on Tree Component while updating remote data

I suppose I’m not the only one who’s had problems with maintaining the state of a flex tree component upon updating the data. Recently I encountered the problem again and decided to crack this nut once and for all. As it turned out it was a lot easier than I had anticipated.

The Scenario
In this particular case I had a tree component displaying a hierarchical view of the pages of a web site. Upon making some certain changes, like dragging and dropping pages to reorder them I felt the need to change the order server-side and reload the data, rather than changing the order inside the dataProvider. I just like it that way better.

The Problem
So before I reloaded the data I saved the tree’s open items in a variable called openTreeItems and when I received the new data I tried to reset it by using tree.openItems = openTreeItems.
Nothing happened.

The Research
So I started doing some research and quickly discovered that to make this work the component uses the uid property and on updating the data for the dataProvider Flex reassigns new uid’s to the items in the collection. So, in short, Flex doesn’t recognize the items as the same items after the reload, because of the new uid values.

I encountered this article in the Flex 3 Help pages and started experimenting with creating custom classes that implemented the IUID interface and soon discovered that this was way to complicated for the (actually) quite simple problem I had. The pages in my database had unique ID’s! Why the h*ll couldn’t I use these values as the uid values instead of the built-in values?

The Solution
I suddenly had an idea: what if I simply add the uid property to my data serverside by using the id value I already had? This could be done in many ways but I chose to alter my SQL query like so:
“SELECT pages.id, pages.id uid, … FROM pages…”
. This way the value would be passed on into Flex the same way as all the other values.

And, voilà, it worked!
After this tiny alteration of my SQL query Flex recognized my items as the ones saved in the openTreeItems variable and when using tree.invalidateList() before the update and tree.validateNow() after the update there isn’t even a flicker upon updating the data. Sweetness!

Hope this helps anyone that has had the same problem.

  • Share/Bookmark

Firebug equivalent for Internet Explorer

I guess I’m not the only one who’s been searching for an IE equivalent of Firebug. Being a Mac developer using Visual Studio simply to be able to debug my web sites in IE has never really been an option and the Internet Explorer Developer Toolbar is too simple for my needs. I want to be able to debug JavaScript, not just CSS and HTML DOM. Long have I pulled my hair trying to find out why the h*ll something is working fine in Firefox/Safari but not in IE.

The other day I came across the DebugBar by the French Toulouse-based company Core Services. It’s excellent! Well, not as simple, clean and accurate as Firebug, but still way ahead of all the other debug solutions I have found for Internet Explorer. And it also has some good features that Firebug is missing.

Vive la France!

  • Share/Bookmark

Google Analytics and Flex using ExternalInterface

A big issue for people creating flash sites is getting the site to work well with Google Analytics. “The page doesn’t refresh. How can I track the clicks?”

Well, it is actually very easy. If you look at the trace script Google Analytics gives you to add to your HTML code you can find a call to a method that actually records the event. This method is simple to call using JavaScript.

I have solved it like this in my latest Flex App (which is a public site). NOTE: This is for the new trace code version.

1. Paste the Google Analytics trace code as usual just before the </BODY> tag.
Check your Google Analytics account for the correct code.

2. See to it that your embedded flash works with ExternalInterface.
This can be a bit tricky, but in my experience the things that do the trick are to change allowScriptAccess to always and inside the Flex App call a custom JavaScript function on creationComplete like so: ExternalInterface.call(’initFlash’). In my html this initFlash function creates a variable reference to the embedded flash. This sort of “creates the connection” between them. I’m not sure why this is so, but for me it works, so I’m happy with that. If there is a need I would be glad to create a more thorough tutorial on the use of ExternalInterface. Just let me know.

3. Create a custom JavaScript that passes the URL you want to register to the Google Analytics script.
This is not necessary, but I have found it easier to work with, as you don’t need to edit you call from inside of Flex if something changes in the Google code or such.

function trackURL(url)
  1. {
  2.     pageTracker._trackPageview(url);
  3. }

4. Call your custom javascript from within Flex.
I created a static class for this. (I love static classes). I named it Analytics.as and placed it in the root source folder in the Flex App. It looks like this. All it does really is call the JavaScript using ExternalInterface, but putting it within a static class lets you call it from anywhere in your application without having to pass on references to this or that object or function.

package
  1. {
  2.    public class Analytics
  3.    {
  4.       import flash.external.ExternalInterface;
  5.  
  6.       public static function track(url:String) : void
  7.       {
  8.          ExternalInterface.call("trackURL", url);
  9.       }
  10.    }
  11. }

And anywhere in your app write:

  1. Analytics.track('/path_to_tha_page_you_want_to_track/');

(NOTE: You have to start your path with a slash).

There you go. It now should track the URL:s you want and give you nice statistics.

  • Share/Bookmark

MySQL Sample Data Creator

A year or two back I was in a real need of a lot of sample data for testing a web tool I was working on, so I wrapped up a small tool to generate sample data by defining a few parameters and choosing how many rows to add, using Flex 2 (or perhaps 1.5, don’t remember). Well today the need arose again, so I went through my old backup files and found it again. I thought I’d put it up on the web so as maybe someone else could be helped by it. I know it’s not perfect and only spent a few hours creating it, but if you have any suggestions on how to improve it please write a comment and let me know what you think. I would be glad to update it and eventually make it a really useful tool.

Go to the MySQL Sample Data Creator 1.0

  • Share/Bookmark

Passing on optional arguments in ActionScript 3

The other day I ran into a small problem with optional arguments in Flex. While writing my tutorial on AMFPHP and Flex I decided to create a small static class that would take care of all the boring stuff with calling AMFPHP. This class enables me to simply use AMFPHP.send(”ServiceName”, and so on).

However, writing the class I realized I wouldn’t be able to pass on the optional parameters. If I would use:

  1. function send(anArgument:Object, …args)

…I wouldn’t be able to send the args argument to the NetConnection.call method, because actionscript would send a “bunched up” array with all the arguments as ONE parameter to the call method, as opposed to a series of parameters, which was what I wanted.

The solution to this is using the Function.apply() method. What I did was to create an empty array and add a series of arguments I wanted to pass on to a function, create a reference to the function and then use the apply method. This is what my finished static method looks like:

public static function send(serviceFunction:String, resultHandler:Function, faultHandler:Function, … args:*) : void
  1. {
  2.  trace("AMFPHP("+serviceFunction+")");
  3.  // Create responder
  4.  var responder:Responder = new Responder(resultHandler, faultHandler);
  5.  // Create an array that will temporarily store all the arguments
  6.  var collectArgs:Array = new Array;
  7.  // Add the fixed arguments
  8.  collectArgs.push(serviceFunction);
  9.  collectArgs.push(responder);
  10.  // Loop through the optional arguments and add them too
  11.  for (var i:uint=0; i<args.length; i++)
  12.  {
  13.   collectArgs.push(args[i]);
  14.  }
  15.  // Create a reference to the function we will call
  16.  var callFunction:Function = connection.call;
  17.  // Call the function using the arguments
  18.  callFunction.apply(connection,collectArgs);
  19. }
  • Share/Bookmark

AMFPHP and Flex

Being a LAMP (Linux Apache MySQL PHP) guy as well as a Flex developer it didn’t take me long to discover AMFPHP. If you haven’t heard of it I can tell you in short it’s a framework for sending and receiving data between Actionscript and PHP, without having to use an XML layer or similar in between, so to speak. It’s really neat, because lets say you extract an array of rows from your MySQL database, then you can just pass the entire array back to Actionscript and it will be received as an Actionscript array in the other end.

Would you like to try it? This is how to do it:

1. Download the latest AMFPHP package from www.amfphp.org

2. Unpack and (preferably) place the folder called amfphp in the document root on your web server.

3. By default the AMFPHP gateway is set to converting all UTF-8 data to Latin 1. Big NO NO for “international” developers like me, so you should open the gateway.php file in the amfphp folder for editing and change the following line (it should be around line 127):

  1. $gateway->setCharsetHandler("utf8_decode", "ISO-8859-1", "ISO-8859-1");

to

  1. $gateway->setCharsetHandler("iconv","UTF-8","UTF-8");

If you don’t do this all UTF-8 special characters from the database will be distorted on arriving at the Flex application.

4. Now create a simple PHP class file, call it HelloWorld.php, and place it in the /amfphp/services folder. You should use a commenting pattern like that of PHPDoc. Here is a simple HelloWorld example:

/**
  1.  * A simple amfphp service.
  2.  */
  3. class HelloWorld
  4. {
  5.  /**
  6.   * A simple HelloWorld function
  7.   * @returns A string containing the phrase 'Hello World!'
  8.   */
  9.  function SayHello()
  10.  {
  11.   return "Hello World!";
  12.  }
  13.  
  14.  /**
  15.   * A simple HelloWorld function that bounces back the given string
  16.   * @returns A string containing the supplied string
  17.   */
  18.  function SayWhat($string)
  19.  {
  20.   return $string;
  21.  }
  22. }

In this class I’ve put two functions; one that returns the string ‘Hello World!’ and another that returns whatever string you send to it. Okay, so let’s get on with the show…

5. Now you can test your service by browsing to the browser folder. It should have a URL similar to this: yourserver.com/amfphp/browser. In the service browser you can test whether your class is working as it should.

6. Flex
To simplify things I’ve created a small class for the implementation of this in Flex. There is a number of ways of doing this, but I’ve chosen the one way that I am most comfortable with.

For ease of use, create an actionscript document in your Flex Projects root directory called AMFPHP.as and past the following code into the file:

package {
  1.  import flash.net.NetConnection;
  2.  import flash.net.Responder;
  3.  
  4.  public class AMFPHP
  5.  {
  6.   private static var gateway:String = "http://www.yourserver.com/amfphp/gateway.php";
  7.   private static var connection:NetConnection = new NetConnection;
  8.   connection.connect(gateway);
  9.  
  10.   public function AMFPHP() : void
  11.   {
  12.    // Static class
  13.   }
  14.  
  15.   public static function send(serviceFunction:String, resultHandler:Function, faultHandler:Function, … args:*) : void
  16.   {
  17.    trace("AMFPHP("+serviceFunction+")");
  18.    // Create responder
  19.    var responder:Responder = new Responder(resultHandler, faultHandler);
  20.    // Create an array that will temporarily store all the arguments
  21.    var collectArgs:Array = new Array;
  22.    // Add the fixed arguments
  23.    collectArgs.push(serviceFunction);
  24.    collectArgs.push(responder);
  25.    // Loop through the optional arguments and add them too
  26.    for (var i:uint=0; i<args.length; i++)
  27.    {
  28.     collectArgs.push(args[i]);
  29.    }
  30.    // Create a reference to the function we will call
  31.    var callFunction:Function = connection.call;
  32.    // Call the function using the arguments
  33.    callFunction.apply(connection,collectArgs);
  34.   }
  35.  }
  36. }

The only thing you have to change here is the URL to the gateway.php file on your server at line 7 in the file.

7. Usage

To use this in your code you will need three things:

• Calling the service using the static AMFPHP.send() function
• A function that handles the result coming from amfphp
• A function that handles errors

This is the first HelloWorld example (without the argument). The first argument for the send() function is a string describing the path to the function, with this structure: [directory.]class.function, within the services folder. A directory is not necessary, but can be convenient if you have packages of many classes.

private function helloWorld() : void
  1. {
  2.  AMFPHP.send("HelloWorld.SayHello",onResult,onFault);
  3. }
  4.  
  5. private function onResult(result:String) : void
  6. {
  7.  trace(result);
  8. }
  9.  
  10. private function onFault(result:Object) : void
  11. {
  12.  trace(String(result.description));
  13. }

Easy huh!?

And the example with the argument is pretty much the same, but with an argument added to the end of the function call. Like this:

private function helloWorld() : void
  1. {
  2.  AMFPHP.send("HelloWorld.SayWhat", onResult, onFault, "Hello to you!");
  3. }
  4.  
  5. private function onResult(result:String) : void
  6. {
  7.  trace(result);
  8. }
  9.  
  10. private function onFault(result:Object) : void
  11. {
  12.  trace(String(result.description));
  13. }

Note that I changed the path to the function from HelloWorld.SayHello to HelloWorld.SayWhat to use the other function in the class.

Well, that’s it. Hope it was of some use. Good luck!

  • Share/Bookmark