Quickly generate Flash animations using PHP

Source: Internet
Author: User
Tags command line ming php code

Rich Internet application is a new buzzword in Web 2.0, and one of the key components of Web 2.0 is Adobe Flash. Learn how to integrate Flash animations into your applications and use the Ming library to dynamically generate flash animations.

WEB 2.0 introduces Rich Internet application. But what is the meaning of Rich Internet application? Typically, it means adding highly responsive transaction operations to your application. Specifically, it means that you can instantly change widgets, Web forms, and reports on a page without retrieving new pages from the server.

One way to build Rich Internet application (RIA) is to use dynamic HTML, which is Ajax, JavaScript, cascading style sheets (cascading style Sheet , CSS) and an HTML combination (see Resources). But DHTML is not the only way to add an interactive action to a WEB application. Another important approach is to use Adobe Flash Player to add interactivity to a Web site for ten years.

The first version of Flash was used to create animated pictures, and the latest version of Flash already hosts a complete interface that can be used to control WEB service access and use ECMAScript (the official version of JavaScript) to provide complete scripting support.

Learn about Flash

Flash Player is a plug-in that is integrated into a Web browser that runs microsoft®windows®, Mac OS X, and linux® computers. By the time this article was finalized, the latest version of Flash Player was V8. It is available for free, and most browsers have this plugin installed. It is very popular and has excellent client penetration-and this penetration has been enhanced with services such as YouTube and Google video, which use Flash to display streaming videos.

Flash Player is just one end of the scale. To play a role, Flash Player also needs to use a flash animation. This type of animation is usually a file compiled using a Flash development tool with a. swf file name extension. But as you'll see in this article, you can also use the Ming library to dynamically build a. swf file in the same way that you create pictures dynamically, and draw graphics on the Web server. The Ming Library builds the operation code in the new. swf file with objects and methods built by the PHP code.

You can view a. swf file in a Web site in either of two ways. The first method simply navigates to the URL of the. swf file. Doing so replaces the entire content area of the Web server with Flash animations. This method is easy to debug, but the main use is to embed animations in the <object> tag of an HTML Web page. The <object> tag then references the SWF animation through the URL. The advantage of the <object> method is that you can place the animation anywhere on the page and dynamically control it through JavaScript code, just as you would any other element in the page.

Listing 1 shows an example of a <object> tag that references a SWF animation.

Listing 1. Embedded Flash animation

The following are the referenced contents:
<object classid= "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
Codebase= "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#
version=6,0,40,0 "
Width= "height=" >
<param name= "movie" value=http://chinaz.com/program/php/"lines.swf" >
<embed src=http://chinaz.com/program/php/"lines.swf" width= "height=" "400"
Type= "Application/x-shockwave-flash"
Pluginspage= "Http://www.macromedia.com/go/getflashplayer" >
</EMBED>
</OBJECT>

This set of tags will refer to an animation named lines.swf. The internal <embed> tag is used to ensure that Flash animations can be played in various browsers that have Plug-ins installed.

The tag also assigns the Flash Player's height and width to 550 pixels and 400 pixels, respectively. Notably, the graphics in Flash animations are vector based, which means that when you draw lines and text using the Flash command, those elements are stored as coordinates and scaled in proportion to the matching display area. As you can see, the Flash animation has its own coordinate system, which allows you to keep your code as neat and tidy as possible.

Ming

The first way to use Flash animations in this article is to dynamically build them using the Ming library. The Ming library is a PHP library with a set of objects mapped to the data types in the SWF animation: Child graphics, graphics, text, bitmaps, and so on. I will not discuss how to build and install Ming because its operations are platform-specific and not particularly simple (see resources). In this article, I used the precompiled Extensions Php_ming.dll library for Windows version of PHP.

It must be noted that Ming is still in the development phase. By the time this article was finalized, the version of the library was V0.4, and some of the commands in the older version were not available in the latest version. I used V0.4 to write this article, so to use this code, you need to use this version.

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

Listing 2. hello.php

The following are the referenced contents:
<?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 a Swfmovie object and set its size. Finally, you add the text element to the animation and save the animation to the file.

Total 3 page: previous 1 [2] [3] Next page



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.