PHP Generate Flash Animation implementation Code _php tutorial

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

Listing 2 shows an example of the HelloWorld implemented using the Ming library.

Listing 2. hello.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 the file hello.swf. When I open the file in my Web browser, I see the result shown in Figure 1.

Figure 1. HelloWorld Example using Ming

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

As an alternative way to build files directly, you can also use the following code to make the 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 Ming examples, I will use the Save method, but you can choose whether to use the Save method, depending on your preferences.

Let the text move.

It doesn't make much sense to put some text in a Flash animation unless you can get it moving. So I've integrated the example in Listing 2, which includes two paragraphs of text: Some start small and then get bigger, and the other part remains static.

Listing 3. text.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 you execute this code 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 points. Then use the method to scaleTo() increase it to 750 points by using the method for the animated object nextframe() .

To understand how it works, you need to know a little bit about how Flash makes animations. Animations in Flash run like animations in movies: Run as frames. The sprite moves in the animation frame by frame. One major difference is that Flash does not get a snapshot of each frame. It stores the state of the sub-graphic object at each frame.

You may notice that I have a $pt variable named, which has the text "1000". Then when I $pt add to the animation, I get the add() new object named by the method $pts . The object is an SWFDisplayItem instance that represents a child shape. I can then move the instance around the frame of the animation frame. It's kind of confusing, but I can have multiple versions of the "1000" text sub-graphic or "points" text sub-shape that move at the same time.

Draw some graphics

The next step is to work with vector graphics. First, draw only a simple line, which is from the top left of the frame to the bottom right.

Listing 4. line.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 look at the output of the. swf file, as shown in effect 3.

Figure 3: Draw a simple line

Well--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. Then I added it as a sub-shape 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: Rotating a line

$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 example, I draw a line from 100, 100 to 100, 100. This will place the center of the line in coordinates 0, 0. This way, when I rotate the shape, the center of the line rotates.

When I add a graphic to the animation, it moves back to the center of the frame SWFDisplayItem . Then use rotate() the method to rotate it and increase its frame every rotation of the week.

Working with pictures

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

listing 6. Using a picture

$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 result 4.

Figure 4: The resulting picture animation

This script reads the Local. jpeg file at the beginning (in this case, the photo of my daughter Megan). Then create a rectangle and fill it with the picture. After that, it uses a displacement effect at 10 frames to move the picture slightly.

Continue to move

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

Another option for building more complex flash animations is to use authoring tools such as Adobe Flex or Laszlo, both of which provide XML syntax for the user interface layout for Flash animations and an easier routine for developing JA, which provides interactive operations for the interface Vascript. true techarticle there is a set of objects that map to the data types in the SWF animation: child shapes, graphics, text, bitmaps, and so on. In this article, I used the precompiled Extensions Php_ming.dll library for Wi ...

  • Related Article

    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: 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.