PHP DevCenter
oreilly.comSafari Books Online.Conferences.


Getting Flashy With PHP

by W.J. Gilmore

Often in my articles I rave about the sheer number of functions available to the PHP programmer. This assertion has been once again enforced with the release of the Ming Flash Generation Library. This package, which also offers a PHP extension, provides PHP users with a rather convenient and simple way to create cool Flash animations.

In this article, I'll introduce you to Ming as it applies to PHP, discuss installation procedures for both Windows and non-Windows operating systems, and guide you through several examples intended on jumpstarting you toward Flash stardom as a PHP programmer.

Downloading and installing Ming

Dave Hayden ( created Ming, which is available for download from his web site. I would also suggest taking a look at the examples, tutorial, and discussion forum that are all available on the site. Before running to the site and downloading the first distribution you see, take a moment to read through the remainder of this section, applying what you read to your own situation.


Ming is only compatible with PHP 4.0.2 and above. If your PHP distribution is older than this, you will need to upgrade. I would always suggest upgrading to the most recent stable version regardless of your intention, as new features are always being added and bugs are being fixed.

Non-Windows users

Non-Windows users have a variety of options for implementing Ming support. Perhaps the easiest way to use Ming is simply to download the Ming PHP module.

Here's the process:

  1. Go to
  2. Download
  3. Uncompress and copy it to the PHP extensions directory.
  4. Add to your php.ini file or add dl('') to the top of the script in which you would like to use Ming.

Note that you will not be able to use the dl() function if the enable_dl configuration directive is turned off in the php.ini file. Furthermore, this function will not work with certain servers, such as Zeus or IIs.

I followed this process, installing the Ming module on a FreeBSD machine using Apache 1.3.12 and PHP 4.0.4 and experienced no problems at all. However, if you do run into problems using the module, you could alternatively download and compile the Ming source and recompile PHP using -with-ming=[dir]. Check out the Ming web site for more information regarding installation methods.

If you are new to the world of configuring and installing PHP on a Unix-type environment, I would suggest checking out Darrell Brogdon's excellent tutorial, "Basic Installation of PHP on a Unix System."

Windows users

Perhaps the most painless way to install Ming on your Windows system is to download precompiled PHP distribution, compliments of Andreas Otto and Daniel Beulshausen. This distribution, currently in version 4.05-dev, not only offers Ming support, but also includes many other hard-to-find Windows PHP extensions. Just download the most recent distribution and follow the directions for activating extensions within the php.ini file.

Essential information

Unit measurements -- All values specifying measurements such as size, length, and distance are in unit "twips" (twenty units per pixel). Therefore, if I were to specify a screen size of 6,400 x 4,800, this would actually translate to 320 x 230 pixels.

Object-oriented programming -- Ming's functionality is carried out using OOP (object-oriented programming). If you aren't familiar with OOP and how PHP implements it, I'll take the opportunity to shamelessly plug Chapter 6, "Object-Oriented PHP," of my new textbook A Programmer's Introduction to PHP 4.0 (Apress, January 2001). It's available at finer bookstores everywhere.

Animation dimensions -- Although Ming offers a function that sets the dimensions (setDimension()) of the Flash animation, the movie will be scaled in accordance to:

  • The pixel size set in the embed tag.
  • The entire frame dimension if the animation is not embedded.

Fonts -- Ming can currently use only those fonts created using its FDB utility. Check out the Ming documentation for more information regarding this.

Output -- There are essentially two choices for displaying output. The first is to output it directly after it is created using the Ming functionality. The second is to save the output to a .swf file and embed it within an HTML script. Throughout the examples shown in the remainder of this article, I simply output the results directly to the browser. However, I also explain an alternative method at the conclusion of the article.

Some examples

In this section, I'll introduce you to Ming's functionality, beginning with a few very basic examples and working upward in difficulty. At the conclusion of this section you should have a pretty good idea of just what Ming is capable of.

Building a movie screen and object

Perhaps above all else, you need to know how to create the canvas upon which the Flash movie will be viewed. Although this won't necessarily be the first lines of code in your script (you'll see why later), it is of course, rather important.

Creating a new movie
You can create a new "movie" object using the following command:

$movie = new SWFMovie();

Of course, you can name the resulting object anything you wish. Just remember that you'll need to continue to use that name for later object manipulation.

Setting the background color
The background color is set using the setBackground() method. There are three required parameters, the red, green, and blue hexadecimal colors, respectively. The fourth parameter, which specifies the alpha color, is optional.

$movie->setBackground(0x00, 0x00, 0xff);

The above settings would change the background color to a royal blue. Incidentally, a complete "cheatsheet" of hexadecimal color codes is available at Webmonkey.

Setting the frames-per-second (fps)
You can also control the fps speed of the animation by specifying the frame rate within the setRate() method. An example of setting this speed to 15 fps is shown here:


Although you could potentially set these parameters toward the end of the script (you'll soon learn why), I typically like to keep them at the top of the page. I'll follow this protocol in subsequent examples.

In the next section, I'll show you how to create simple shapes. As you know, shapes are the basis for the majority of Flash animations. I think you'll be surprised at just how easy it is to populate your animations with shapes of all types and sizes.

Simple shapes

Creating and displaying a simple shape is well, simple. Perhaps the most important method is the one that instantiates a new shape, SWFShape(). It's used as follows:

$square = new SWFShape();

Therefore, $square is an object that has all of the SWFShape class methods and attributes at its disposal. Once you've created the object, you can begin designing its features with these methods and attributes. Because $square is likely to represent the object for which it is named, I'll need to draw four lines. However, before drawing these lines, I would like to designate a few properties regarding that line, accomplished using the method setLine():

// setLine(line width, red, green, blue [, alpha])
$square->setLine(25, 0xff, 0xcc, 0x33);

Once I've set these properties, I can proceed with creating the lines:

$square->drawLine(500, -500);
$square->drawLine(500, 500);
$square->drawLine(-500, 500);
$square->drawLine(-500, -500);

Listing 1 offers a complete example of drawing a square to the canvas.

Listing 1-1: Drawing a simple shape to the Flash canvas.


// Create movie and set parameters.

$movie = new SWFMovie();
$movie->setBackground(0x00, 0x00, 0x00);
$movie->setDimension(5400, 3600);

// Create a new shape.

$square = new SWFShape();

// Set a line width of 15, and rgb color of gold.

$square->setLine(15, 0xff, 0xcc, 0x33);

// Move the drawing pen toward the middle of the screen.

$square->movePenTo(2000, 2000);

// Draw the square, 'standing' it on a point.

$square->drawLine(500, -500);
$square->drawLine(500, 500);
$square->drawLine(-500, 500);
$square->drawLine(-500, -500);

// Add the square to the canvas.

$addedSquare = $movie->add($square);

// Output the $movie to the canvas.

header('Content-type: application/x-shockwave-flash'); 


Relative vs. absolute positioning

At first glance, you may find some of the Ming function names rather confusing; I'll use drawLine() and drawLineTo() as an example. While both are rather similar in name, they have very distinct roles. The method drawLine() uses relative positioning to draw lines, while drawLineTo() uses absolute positioning. Several similar parallels can be drawn between various function sets, such as movePen() and movePenTo(), scale(), and scaleTo(), and rotate() and RotateTo(). Be sure to keep this in mind when creating your animations, or you will likely achieve unexpected results.

Pages: 1, 2

Next Pagearrow

Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!

Sponsored by: