Friday, 3 September 2010

create flash(swf) file using PHP libming

My boss ask me to make a flash website, something like twinkle star. Since we don't have the Adobe Flash software so I think there should be some alternative. After google it, spend a day to search and learn some alternative, finally I choose Ming.

The Ming has support for PHP and it called libming. It make swf(flash) file using PHP. I learned PHP before so it is easy for me.

My server is running Debian Lenny 5.0. The lastest libming version is 0.4.3. In Debian 5.0 libming is 0.3.0 and in Ubuntu 10.04 the libming is 0.4.3.
I don't what to install any unstable package in the Debian server so I install a Ubuntu client.

After install ubuntu, I only install some PHP packages :

$sudo apt-get install php-cli php-common php5-ming

and others necessary packages will be installed automatically. php-cli is command line interface, that means we can run php file in console mode, not in web server.

This is the star.png file with transparency background:






Example ONE :
This php script creates a very simple static swf file. It place a star.png file on movieclips only.
<?php
Ming_useSWFVersion(5); // set flash version 5
$png= new SWFBitmap(file_get_contents("star.png","r"));
$m = new SWFMovie();
$m->add($png);
$m->setDimension(143, 143); // the flash dimension
$m->setBackground(0, 0, 0); // background color is black since star is white
$m->setRate(12.0);
$m->setFrames(20);
header('Content-type: application/x-shockwave-flash');
$m->save("static.swf",9); // save to file, compression rate: 1 - minimun , 9 - maximum
?>

Save the file as static.php In console, type
$php static.php

then it will create a static.swf file at same directory.

If you have a web server installed, you may see the result on browser. At last line, change
$m->save("static.swf",9);
to
$m->output(); // to screen

Let's the server ip is 192.168.1.1. In browser, type http://192.168.1.1/static.php, then the flash file will show on screen. It is a static flash, like a picture display on screen.

To make the star twinkle, we need some transition effect : fade in then fade out so that it look like twinkle.

Here is some tutorial for transition effect,
http://www.opaque.net/ming/tutorial/index9.html

To use transition effect, you need to download the file flashdot.tag or lashdot.zip, extract it(there are 5 files) and copy 2 files : infuncs.php and outfuncs.php.

In infuncs.php you have to comment out the line 8, like this :
// $i->moveTo(600,30);

Example TWO :
It has fade in then fade out effect :
<?php
Ming_useSWFVersion(5); // set flash version 5
include('infuncs.php');
include('outfuncs.php');
$png= new SWFBitmap(file_get_contents("star.png","r"));
$m = new SWFMovie();
$instance = fadein($m, $png);
fadeout($m, $png, $instance);
$m->setDimension(143, 143); // the flash dimension
$m->setBackground(0, 0, 0); // black color since star is white
$m->setRate(12.0);
$m->setFrames(20);
header('Content-type: application/x-shockwave-flash');
$m->save("twinkle.swf",9); // save to file, compression rate: 1 - minimun , 9 - maximum
?>


Save the file as twinkle.php. In console, type
$php twinkle.php

It made a twinkle.swf flash file.

This is the swf file :



Ming core library is under the LGPL license. Ming's command line ActionScript compiler (makeswf) is distributed under the GPL license.

That means, it is free.