Ming offers a flexible, easy way to create dynamic Flash with PHP

Flash content can be a great addition to any site, but creating it dynamically with ActionScript is difficult. Thankfully, the Ming PHP library makes it possible to create the content with PHP.

Macromedia Flash has become a widely popular Web presentation tool because users need a little glitz to maintain their interest. Unfortunately, using ActionScript to create dynamic movies can be limiting, and Macromedia has announced plans to discontinue the Flash Generator product in favor of Flash MX support in Cold Fusion. So where does that leave your Web site? Once again, PHP saves the day. You can easily use the Ming PHP library to dynamically generate Flash movies and seamlessly integrate them into your scripts.

Ming was introduced into PHP in version 4.05, replacing the previously supported module, libswf. Written by Dave Haden and friends at, the Ming library is written in C. It comes from a project to support Flash with a variety of scripting languages, with an eye toward PHP in particular.

The library is easy to use and deploy and integrates nicely into any PHP script. While the library is still considered experimental by PHP (its current version is 0.2a), it has been in use for over a year. Online tutorials, examples, and help can get you started. The Ming functions are structured intuitively for both Flash and PHP developers. With Ming and PHP, not only can you give your site the desired panache, but you can also enjoy reduced overhead and cost over deploying other Flash solutions.

Deploying Ming
You can run Ming either built in to PHP (on UNIX) or as a PHP module. If you run Ming built in to PHP, the functions are always available, and there is no need to instantiate the library. If you run Ming as a PHP module, you call Ming specifically by requiring the appropriate library, similar to using the PEAR module, and then accessing the functions within that library. The latter method prevents your site from having to load the module on pages that don't make use of it but creates a slight performance degradation when the module is loaded, compared to building Ming as a native function set.

You can download the Ming libraries for free from Then, depending on your choice of deployment, follow the instructions on the Opaque Web site to build Ming and configure PHP. Additionally, the PHP manual contains detailed instructions.

Once Ming is installed, you can call the Ming functions from any PHP script in your document directory by creating a PHP object and using that object's functions and attributes to define your movie. There are 13 Ming objects in PHP, each providing a portion of the functionality from Flash 4, and a few commands used to control the Publish Settings. Let's take a look at how to put these objects together.

Building a Flash movie
To build an SWF movie using Macromedia Flash, you first create the objects, called symbols, to be used. Flash has three types of symbols:
  • Graphics
  • Movie clips
  • Buttons

Once you've defined the symbols, you can place copies of them onto the movie canvas. Each copy is called an instance, and you may have as many instances of each symbol as you like. Now, you can define what (if anything) happens to these objects in each frame of the movie. Part of this step is to define actions and movement of the symbol instances you've already created. Finally, you'll want to define the movie parameters themselves (such as size and background color), tweak your Publish Settings, if necessary, and output the .swf file.

Creating a Flash movie using the PHP module Ming is similar and provides functionality to handle most aspects of the SWF format. Just as with Macromedia's project, you first create your symbols, which are instances of PHP objects, and then define the location of these objects in the movie's canvas or in relation to each other. Next, you define movements and actions of objects within each frame and, finally, you define the movie itself. You can output your SWF directly to the user's browser or save it in an .swf file for later use.

The advantage of using Macromedia is the graphical user interface. However, using Ming to generate your Flash gives you much simpler control of objects (once you get used to it), and allows the original .swf source to be created on the fly. This, in turn, provides infinite possibilities for reuse and reintegration, as well as more powerful options for conditional inclusion of objects and other dynamic elements of Flash without the limitations of ActionScript.

Putting it together
To better illustrate how to build a movie in SWF format, let's walk through an example PHP script using Ming. Click here to check out the script in action. (The red box on bottom is the button.)

To get started, let's create a new symbol. The SWFShape() object in the Ming module controls the drawing tools, and the various methods in that object control what you draw, in a very LOGO-esque manner.
$square = new SWFShape();
$sqfill = $square->addFill(0, 0, 0xff);

Now we can use that shape in a movie clip and define some actions:
$sqclip = new SWFSprite();
$i = $sqclip->add($square);
$sqclip->add(new SWFAction("stop();"));
$sqclip->add(new SWFAction("play();"));
for($n=0; $n<24; $n++) {

Next we'll create another shape and use it for a button. Rather than create a separate shape for each button action (over, down, up, and release), I've created a function to automate drawing the shapes:
function rect($r, $g, $b)  {
$s = new SWFShape();
$s->setRightFill($s->addFill($r, $g, $b));
return $s;
$b = new SWFButton();
$b->addShape(rect(0xff, 0, 0), SWFBUTTON_UP | SWFBUTTON_HIT);
$b->addShape(rect(0, 0xff, 0), SWFBUTTON_OVER);
$b->addShape(rect(0, 0, 0xff), SWFBUTTON_DOWN);
$b->addAction(new SWFAction("setTarget('/box'); gotoandplay(2);"), SWFBUTTON_MOUSEDOWN);

Next, we define the movie and place the movie clip (sprite) and the button in the movie itself:
$m = new SWFMovie();
$i = $m->add($sqclip);
$i->moveTo(1650, 400);
$i = $m->add($b);

Finally, we send the output to the browser:
header('Content-type: application/x-shockwave-flash');

The result is a movie that will play a clip when the button is pressed. As you can see, the process of creating SWF output with Ming is similar to that of Macromedia Flash. We're merely skipping the GUI, which gives us some latitude in automating creation of the movie.

PHP with Ming gives developers tremendous power over dynamic creation of Flash without a lot of expense. The learning curve is small, especially if you're familiar with Macromedia Flash, as efforts were made to preserve functionality. Dealing with bitmaps and fonts is a bit clumsy, but Flash needed improvement in these areas to begin with. Overall, Ming offers an excellent solution for the growing business in need of Flash.

Editor's Picks