by Bryan Matttern Yimu Translation
Introduction to SWF and Flash
SWF is a file format used by Macromedia Flash to deliver pictures, animations and sounds to users on the Internet. Flash is the ability to provide users with a rich and dynamic interface. About 90% of web users can browse SWF content without installing browser plug-ins, and more than 200 million people have downloaded Flash players. Macromedia disclosed the SWF specification in April 1998. Add SWF support to PHP4.
The built-in ability of PHP to generate pictures dynamically is a feature that attracts me. It can generate reports and interfaces that look more professional and comfortable. At the beginning, I used various GD codes that were flooding the internet to create pictures to display data for my different projects. But I was soon annoyed by the uncertainty of the generated pictures and decided to try to see if I could use vector graphics to solve the problem. I think you would agree, and it looks much better. If a picture can represent a thousand words, imagine what a Flash animation represents?
I will try to make this example simpler and only talk about the basics. My purpose is just to create a Drog in that accommodates GIF and PNG images generated by GD. You can add extensions and enhancements to it, such as the various visual effects that Flash relies on. For example, you can make the graphics fade in, fly when loading a page, or dynamically display a few snowflakes. Your imagination is the only limit on the SWF function of PHP.
It is best to leave it to readers for practice how to obtain data that needs to be graphed. Since this post is about creating a Flash file dynamically, I will use a hypothetical table as a dataset in the example to create its graphical view. You need to check your data and decide to take the most suitable chart format. In most cases, pie charts are a suitable choice, which is the chart form my example will take. Line charts, histograms, or area charts can all be created in a similar way.
In this example, suppose we send some parcels to several cities, and we want to see the proportion of parcels received in each city. We decided to store the data in the table "city" of the database "world". Let's create this table first and enter the data required for this example.
#
# Table structure for table 'city'
#
DROP TABLE IF EXISTS city;
CREATE TABLE city (
city_id int(14) NOT NULL auto_increment,
city_name varchar(255) NOT NULL,
city_timestamp timestamp(14),
PRIMARY KEY (city_id)
);
#
# Dumping data for table 'city'
#
INSERT INTO city VALUES( '1', 'London', '20000917122625');
INSERT INTO city VALUES( '2', 'London', '20000917122626');
INSERT INTO city VALUES( '3', 'London', '20000917122626');
INSERT INTO city VALUES( '4', 'London', '20000917122627');
INSERT INTO city VALUES( '5', 'Paris', '20000917122631');
INSERT INTO city VALUES( '6', 'Paris', '20000917122632');
INSERT INTO city VALUES( '7', 'New York', '20000917122644');
INSERT INTO city VALUES( '8', 'New York', '20000917122645');
INSERT INTO city VALUES( '9', 'New York', '20000917122646');
INSERT INTO city VALUES( '10', 'New York', '20000917122646');
INSERT INTO city VALUES( '11', 'New York', '20000917122647');
INSERT INTO city VALUES( '12', '*', '20000917122654');
Configure your system to use SWF
The environment I use is RedHat Linux6.2, Apache 1.3.12, PHP 4.0.2 (compiled as Apache module). Things will be a little different if you use PHP in Windows. You need to download or compile a Flash Dll, but you don't need to modify the code.
PHP provides the ability to create Shockwave Flash files through Paul Haeberli's libswf module. You need to download libswf from /grafica/flash/. Then, you need to configure PHP using the option --with-swf[=DIR], where DIR is the directory where the include and lib directories are located. There must be files in the include directory, and files in the lib directory. When the downloaded libswf release version is unzipped, the two files will be unzipped to the same directory. You need to move these two files to the correct location. After completion, the directory structure should look like the following:
/usr/local/swf/
/include/
/lib/
/fonts
...
In order for the SWF function to work properly, you need to copy the /usr/local/swf/fonts/ directory so that the web server can access the directory (for apache and mod_php, the best way is to use the absolute path and copy the above directory to the apache document root directory.) In addition, there is a very small C program in the release version of libswf, which can convert fonts of type 1 into fonts that can be used by Flash.
Because we want to create and write SWF files dynamically, the web server needs to have write permissions in the directory where the files are stored.
Introduction to SWF and Flash
SWF is a file format used by Macromedia Flash to deliver pictures, animations and sounds to users on the Internet. Flash is the ability to provide users with a rich and dynamic interface. About 90% of web users can browse SWF content without installing browser plug-ins, and more than 200 million people have downloaded Flash players. Macromedia disclosed the SWF specification in April 1998. Add SWF support to PHP4.
The built-in ability of PHP to generate pictures dynamically is a feature that attracts me. It can generate reports and interfaces that look more professional and comfortable. At the beginning, I used various GD codes that were flooding the internet to create pictures to display data for my different projects. But I was soon annoyed by the uncertainty of the generated pictures and decided to try to see if I could use vector graphics to solve the problem. I think you would agree, and it looks much better. If a picture can represent a thousand words, imagine what a Flash animation represents?
I will try to make this example simpler and only talk about the basics. My purpose is just to create a Drog in that accommodates GIF and PNG images generated by GD. You can add extensions and enhancements to it, such as the various visual effects that Flash relies on. For example, you can make the graphics fade in, fly when loading a page, or dynamically display a few snowflakes. Your imagination is the only limit on the SWF function of PHP.
It is best to leave it to readers for practice how to obtain data that needs to be graphed. Since this post is about creating a Flash file dynamically, I will use a hypothetical table as a dataset in the example to create its graphical view. You need to check your data and decide to take the most suitable chart format. In most cases, pie charts are a suitable choice, which is the chart form my example will take. Line charts, histograms, or area charts can all be created in a similar way.
In this example, suppose we send some parcels to several cities, and we want to see the proportion of parcels received in each city. We decided to store the data in the table "city" of the database "world". Let's create this table first and enter the data required for this example.
#
# Table structure for table 'city'
#
DROP TABLE IF EXISTS city;
CREATE TABLE city (
city_id int(14) NOT NULL auto_increment,
city_name varchar(255) NOT NULL,
city_timestamp timestamp(14),
PRIMARY KEY (city_id)
);
#
# Dumping data for table 'city'
#
INSERT INTO city VALUES( '1', 'London', '20000917122625');
INSERT INTO city VALUES( '2', 'London', '20000917122626');
INSERT INTO city VALUES( '3', 'London', '20000917122626');
INSERT INTO city VALUES( '4', 'London', '20000917122627');
INSERT INTO city VALUES( '5', 'Paris', '20000917122631');
INSERT INTO city VALUES( '6', 'Paris', '20000917122632');
INSERT INTO city VALUES( '7', 'New York', '20000917122644');
INSERT INTO city VALUES( '8', 'New York', '20000917122645');
INSERT INTO city VALUES( '9', 'New York', '20000917122646');
INSERT INTO city VALUES( '10', 'New York', '20000917122646');
INSERT INTO city VALUES( '11', 'New York', '20000917122647');
INSERT INTO city VALUES( '12', '*', '20000917122654');
Configure your system to use SWF
The environment I use is RedHat Linux6.2, Apache 1.3.12, PHP 4.0.2 (compiled as Apache module). Things will be a little different if you use PHP in Windows. You need to download or compile a Flash Dll, but you don't need to modify the code.
PHP provides the ability to create Shockwave Flash files through Paul Haeberli's libswf module. You need to download libswf from /grafica/flash/. Then, you need to configure PHP using the option --with-swf[=DIR], where DIR is the directory where the include and lib directories are located. There must be files in the include directory, and files in the lib directory. When the downloaded libswf release version is unzipped, the two files will be unzipped to the same directory. You need to move these two files to the correct location. After completion, the directory structure should look like the following:
/usr/local/swf/
/include/
/lib/
/fonts
...
In order for the SWF function to work properly, you need to copy the /usr/local/swf/fonts/ directory so that the web server can access the directory (for apache and mod_php, the best way is to use the absolute path and copy the above directory to the apache document root directory.) In addition, there is a very small C program in the release version of libswf, which can convert fonts of type 1 into fonts that can be used by Flash.
Because we want to create and write SWF files dynamically, the web server needs to have write permissions in the directory where the files are stored.