PHP to generate Flash animation implementation code _php skills

Source: Internet
Author: User
Tags ming
There is a set of objects that map to the data types in the SWF animation: Child graphics, graphics, text, bitmaps, and so on. In this article, I used the precompiled Extensions Php_ming.dll library for Windows version of PHP.

Listing 2 shows a HelloWorld sample implemented using the Ming library.


Listing 2. hello.php
				
<?php
$f = new Swffont (' _sans ');

$t = new Swftextfield ();
$t->setfont ($f);
$t->setcolor (0, 0, 0);
$t->setheight (400);
$t->addstring (' Hello world ');

$m = new Swfmovie ();
$m->setdimension (2500, 800);
$m->add ($t);

$m->save (' hello.swf ');
?>

Running this code on the command line will generate a file hello.swf. When I open the file in a Web browser, I see the result shown in Figure 1.


Figure 1. Example of HelloWorld using Ming


Looking back at this code, the first thing I do is create a pointer to a built-in font (_sans), create a text field, set the font, color, and size, and give it some text content ("Hello World"). Next, you create an SWFMovie object and set its size. Finally, you add the text element to the animation and save the animation to the file.

As an alternative to directly building a file, you can also use the following code to make a SWF animation output like a page without using the Save method:

Header (' Content-type:application/x-shockwave-flash ');
$m->output ();

This process is similar to using the ImageMagick library in PHP to build bitmaps. For all the Ming examples, I will use the Save method, but you can choose whether to use the Save method according to your preferences.

Let the text move.

Just putting some text into a Flash animation doesn't make much sense unless you can get it moving. So I've consolidated the example in Listing 2, which includes two pieces of text: Some start small and then get bigger, while the other part stays static.


Listing 3. text.php
				
<?php
$f = new Swffont (' _sans ');

$pt = new Swftextfield ();
$pt->setfont ($f);
$pt->setcolor (0, 0, 0);
$pt->setheight (400);
$pt->addstring (' 1000 ');

$tt = new Swftextfield ();
$tt->setfont ($f);
$tt->setcolor (192, 192, 192, 90);
$tt->setheight (350);
$tt->addstring (' Points ');

$m = new Swfmovie ();
$m->setdimension (2500, 800);

$pts = $m->add ($pt);
$pts->moveto (0, 0);

$tts = $m->add ($TT);
$tts->moveto (1300, 200);

for ($i = 0; $i < $i + +) {
$m->nextframe ();
$pts->scaleto (1.0 + ($i/10.0), 1.0 + ($i/10.0));
}

$m->save (' text.swf ');
?>

When this code is executed on the command line, it generates TEXT.SWF. When I open the file in my Web browser, I see the picture shown in Figure 2.


Figure 2. text.swf file


The text "1000" starts small and has a size of 350 dots. Then use the scaleTo() method to increase it to 750 points by using the method on the animated object nextframe() .

To understand how this works, you need to know a little bit about how Flash animations are made. Animations in Flash run like animations in a movie: run by frame. The child shapes move through frames in the animation frame. A major difference is that Flash does not get a snapshot of each frame. It stores the state of the child drawing objects in each frame.

You may notice that I have a variable named, $pt which has the text "1000". Then, when I $pt add it to the animation, I get the add() new object named as returned by the method $pts . The object is an SWFDisplayItem instance that represents a child shape. I can then move the instance around the surface of the animation frame. It's a bit confusing, but I can have multiple versions of the "1000" text or "points" text child graphics that are moved simultaneously.

Draw some graphics

The next thing to deal with is vector graphics. You first draw a simple straight line, from the top of the frame to the bottom of the right.


Listing 4. line.php
				
<?php
$m = new Swfmovie ();
$m->setdimension (300, 300);

$s = new swfshape ();
$s->setline (10, 0, 0, 0);
$s->movepento (10, 10);
$s->drawlineto (290, 290);
$m->add ($s);

$m->save (' line.swf ');
?>

Run this script on the command line, and then view the. swf file for the output, as shown in Figure 3.


Figure 3. Draw a simple line


OK-it's very simple and not very exciting. So, what did I do? A new SWFShape object is created, and some stroke moves and lines are added to it. I then added it as a child graphic to the animation.

To make it more interesting, I used the same frame animation that I used in the text just now. But in this case, I use the code shown below to rotate the line around the center of the animation.


Listing 5. Rotate the line
				
<?php
$m = new Swfmovie ();
$m->setdimension (300, 300);

$s = new swfshape ();
$s->setline (5, 0, 0, 0);
$s->movepento (-100,-100);
$s->drawlineto (100, 100);
$ts = $m->add ($s);

$ts->moveto (150, 150);

for ($i = 0; $i < $i + +) {
$ts->rotate (10);
$m->nextframe ();
}

$m->save (' rotate.swf ');
?>

In this case, I drew a straight line from 100,-100 to 100, 100. This will place the center of the line at coordinates 0, 0. This way, when I rotate the graphic, the center of the line rotates.

When I add a graphic to the animation, the move is returned to the center of the frame SWFDisplayItem . Then use rotate() the method to rotate it and increase its frame each week.

Working with pictures

Text and simple vector graphics such as lines, circles, arcs, curves, and rectangles are excellent, but ideally you must be able to access the pictures in these flash animations. Thankfully, the Ming library makes it easy for you to use pictures, as shown below.


listing 6. Using pictures
				
<?php
$img = new Swfbitmap (file_get_contents (' megan.jpg '));

$s = new swfshape ();
$IMGF = $s->addfill ($img);
$s->setrightfill ($IMGF);
$s->movepento (0, 0);
$s->drawlineto ($img->getwidth (), 0);
$s->drawlineto ($img->getwidth (), $img->getheight ());
$s->drawlineto (0, $img->getheight ());
$s->drawlineto (0, 0);

$m = new Swfmovie ();
$m->setdimension ($img->getwidth () * 2, $img->getheight () * 2);
$is = $m->add ($s);
$is->moveto ($img->getwidth ()/2, $img->getheight ()/2);

for ($i = 0; $i < $i + +)

$is->skewx (0.02);
$is->skewy (-0.03);
$m->nextframe ();
}

$m->save (' image.swf ');
?>

Run this script on the command line and view image.swf in the browser, as shown in Figure 4.


Figure 4. Generated picture animation


This script read the local. jpeg file at the beginning (in this case, it's my daughter Megan's photo). Then create a rectangle and populate it with the picture. After that, it uses a displacement effect at 10 frames to make the picture move slightly.

Keep moving.

I just touched the surface of the operation that the Ming library can provide for you. I don't show the interactive part here, and in the interactive section you can connect simple scripts with elements. (However, if you have an interactive operation, if you have a very complex flash animation, you might want to consider using the Flash development tool to build a flash animation within your Web application that talks to the Web service.) )

Another option for building more complex Flash animations is to use production tools such as Adobe Flex or Laszlo, both of which provide XML syntax for the user interface layout for Flash animations and a more relaxed routine that can be used to develop a Ja that provides interactive action for the interface Vascript.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.