Let's bake a cookie (pie chart).
After successfully installing PHP Shockwave Flash support, you can create a shockwave file with PHP. The best way to learn is to jump directly to the program, so let's look at the program below. The first file includes sample code for how to use the class, and also shows how to embed a Flash file in an HTML document.
Include class needed for flash graph
Include ("class.pie.flash.php");
mysql_connect ("localhost", "root", "");
$query = "Select DISTINCT city_name, COUNT (city_id)
From city
GROUP by City_name; ";
$result = Mysql_db_query ("Hermes", $query);
while ($row = Mysql_fetch_array ($result)) {
$city _counts[] = $row ["COUNT (city_id)"];
$city _names[] = $row ["City_name"];
}
Mysql_free_result ($result);
Instantiate new Object
$graph = new Flash_pie ($city _counts, "city.swf");
Set graph title (should not exceed about characters)
$graph->pie_title ("City Results", 30);
Set graph legend
$graph->pie_legend ($city _names);
Show graph
$graph->show ();
Free resources
$graph->close ();
?>
Type= "Application/x-shockwave-flash" width=600 height=300>
Class Flash_pie {
class variables
Setup some global colors
var $r _arr = Array (0.1, 1, 0, 1, 0, 1, 0.388235294, 0.4, 0.388235294, 0.929411765);
var $g _arr = Array (1, 0, 0, 1, 1, 0, 0.8, 0.4, 0.8, 0.439215686);
var $b _arr = Array (0.25, 0, 1, 0, 1, 1, 1, 0.4, 1, 0.043137255);
var $percents;
function Flash_pie ($values, $this _file) {//begin constructor
To write out code directly to browser, set content header and use "Php://stdout"
Swf_openfile ("Php://stdout", 700, 250, 30, 1, 1, 1);
Header ("Content-type:application/x-shockwave-flash");
Swf_openfile ($this _file, 1000, 450, 30, 1, 1, 1);
Set up viewport for Flash movie
Swf_ortho2 (-400, 300,-90, 250);
Choose the font we'll use for pie graph
Swf_definefont ("Mod");
Get sum of array for percents/slices
while (list ($key, $val) = each ($values)) {
$sum = $sum + $val;
}
for ($i =0; $i Calculate how big they need to being and then
Draw all about our slices
if ($i = = 0) {
Setup parameters for first slice
$begin = 0;
$val = $values [$i]/$sum;
$end = $val *360;
Swf_translate (-200, 0, 0);
} else {
Setup parameters for every and other slice
$begin = $end;
$val = $values [$i]/$sum;
$end = $end + $val *360;
}
function call to add slice
$objID = 1+ $i *10;
$this->show_slice ($i, $objID, $begin, $end);
Put together percent array for all labels
$this->percents[$i] = round ($values [$i]/$sum *100);
}
}//end Flash_pie
function Show_slice ($i, $objID, $begin, $end) {
Draws a slice and places it in our frame
Swf_addcolor ($this->r_arr[$i], $this->g_arr[$i], $this->b_arr[$i], 1);
Swf_startshape ($objID);
Swf_shapefillsolid (0, 0, 0, 1);
Swf_shapearc (0, 0, $begin, $end);
Swf_shapecurveto (0, 0, 0, 0);
Swf_endshape ($objID);
Swf_pushmatrix ();
Swf_placeobject ($objID, 1);
Swf_popmatrix ();
Swf_showframe ();
}
function Pie_legend ($labels) {
Draws the legend and labels and places it in our frame
for ($i =0; $i Swf_addcolor ($this->r_arr[$i], $this->g_arr[$i], $this->b_arr[$i], 1);
Swf_definerect ($i +1000, 1, 0, 20, 20, 0);
if ($i = = 0) {
Swf_translate (120, 75, 0);
} else {
Swf_translate (0, 20, 0);
}
Swf_placeobject ($i +1000, 1);
Swf_translate (0, 5, 0);
Unset ($label);
$label = $labels [$i];
$label. = "(";
$label. = $this->percents[$i];
$label. = "percent)";
if ($i ==0) {
$width = (Swf_textwidth ($label)/4) +30;
} else {
$width = Round (Swf_textwidth ($label)/2) +30;
}
$this->pie_text ($i -1000, "$label", $width, 0);
Swf_translate (-$width, 0, 0);
}
Swf_translate ($width, 30*count ($labels), 0);
}
function Pie_text ($id, $text, $size, $x, $y) {
Simple function to draw text ($text) at ($x, $y) with font size ($size)
Set color of text to black
Swf_addcolor (0,0,0,0);
Set font size and slant
Swf_fontsize ($size);
Swf_fontslant (0);
Define, position and place text in frame
Swf_definetext ($id, "$text", 1);
Swf_translate ($x, $y, 0);
Swf_placeobject ($id, 1);
}
function Pie_title ($text, $size) {
Simple function to draw title and set lineup
$text should not exceed about characters
$this->pie_text ($text, $size, 0, 150);
Swf_translate (0,-300, 0);
}
Function Show () {
Show the Frame
Swf_showframe ();
}
function Close () {
Flush our buffer and return movie
$data = Swf_closefile (1);
}
}//End Class Flash_pie
?>
Note that you can return the generated SWF file directly to the browser without having to write it to a file like me. This may be useful for testing, but you may rarely use a flash file, and more often you may want to embed the Flash file in an HTML document. If you choose to export the Flash file directly to the browser, you can set the header content type as follows:
Header ("Content-type:application/x-shockwave-flash")
and put swf_openfile (filename ",...) Change to Swf_openfile ("Php://stdout",...)
Links to more information:
http://www.php.net/manual/ref.swf.php a description of the swf_* PHP function
http://reality.sgi.com/grafica/flash/Download the PHP swf Library
http://openswf.org more flash tools and information
http://www.macromedia.com/software/flash/open/licensing/
More information about the Macromedia Flash SDK
The above describes the Flash Player 10.3 official download Flash graphics data Two, including the Flash Player 10.3 official download content, I hope to be interested in PHP tutorial friends helpful.