Home Backend Development PHP Tutorial PHP生成Flash动画的实现代码_php技巧

PHP生成Flash动画的实现代码_php技巧

May 17, 2016 am 09:27 AM
php

其中有一组映射到 SWF 动画中的数据类型的对象:子图形、图形、文本、位图等等。在本文中,我使用了预编译的扩展 php_ming.dll 库用于 Windows 版本的 PHP。

清单 2 显示了使用 Ming 库实现的 HelloWorld 示例。


清单 2. Hello.php
				<br><?php <BR>$f = new SWFFont( '_sans' );<br><br>$t = new SWFTextField();<br>$t->setFont( $f );<br>$t->setColor( 0, 0, 0 );<br>$t->setHeight( 400 );<br>$t->addString( 'Hello World' );<br><br>$m = new SWFMovie();<br>$m->setDimension( 2500, 800 );<br>$m->add( $t );<br><br>$m->save( 'hello.swf' );<br>?><br>
Copy after login

在命令行中运行这段代码将生成文件 hello.swf。当我在 Web 浏览器中打开该文件时,看到了图 1 所示的结果。


图 1. 使用 Ming 的 HelloWorld 示例
使用 Ming 的 HelloWorld 示例

回过头来查看这段代码,我做的第一件事是创建指向一个内置字体(_sans)的指针,然后创建文本字段,设定字体、颜色和大小,最后为其提供 一些文本内容(“Hello World”)。再接下来创建了一个 <font face="NSimsun">SWFMovie</font> 对象并设定其尺寸。最后,向动画中添加了文本元素并将动画保存到文件中。

作为直接构建文件的替代性方法,也可以使用下面的代码,使 SWF 动画像页面那样输出,而无需使用 save 方法:

header( 'Content-type: application/x-shockwave-flash' );<br>$m->output( );<br>
Copy after login

此过程类似于使用 PHP 中的 ImageMagick 库来构建位图。对于所有 Ming 示例,我都将使用 save 方法,但您可以根据喜好来选择是否使用 save 方法。

让文本动起来

只是将一些文本放入 Flash 动画中是没有多大意义的,除非您能让它动起来。因此我整合了清单 2 中的示例,它包括两段文本:一部分开始很小后来变得越来越大,而另一部分保持静态。


清单 3. Text.php
				<br><?php <BR>$f = new SWFFont( '_sans' );<br><br>$pt = new SWFTextField();<br>$pt->setFont( $f );<br>$pt->setColor( 0, 0, 0 );<br>$pt->setHeight( 400 );<br>$pt->addString( '1000' );<br><br>$tt = new SWFTextField();<br>$tt->setFont( $f );<br>$tt->setColor( 192, 192, 192, 90 );<br>$tt->setHeight( 350 );<br>$tt->addString( 'Points' );<br><br>$m = new SWFMovie();<br>$m->setDimension( 2500, 800 );<br><br>$pts = $m->add( $pt );<br>$pts->moveTo( 0, 0 );<br><br>$tts = $m->add( $tt );<br>$tts->moveTo( 1300, 200 );<br><br>for( $i = 0; $i  $m->nextframe();<br> $pts->scaleTo( 1.0 + ( $i / 10.0 ), 1.0 + ( $i / 10.0 ) );<br>}<br><br>$m->save( 'text.swf' );<br>?><br>
Copy after login

在命令行中执行这段代码时,它将生成 text.swf。在 Web 浏览器中打开该文件时,我看到了图 2 所示的图片。


图 2. text.swf 文件
text.swf 文件

文本 “1000” 开始时很小,大小为 350 个点。然后使用 <font face="NSimsun">scaleTo()</font> 方法使其增大为 750 个点,方法是对动画对象使用 <font face="NSimsun">nextframe()</font> 方法。

要理解其工作原理,需要了解一点 Flash 制作动画的方法。Flash 中的动画就像电影中的动画一样运行:按帧运行。子图形将按帧在动画框架中移动。一个主要差别是 Flash 不获取每帧的快照。它存储子图形对象在每帧的状态。

您可能会注意到,我有一个名为 <font face="NSimsun">$pt</font> 的变量,该变量具有文本 “1000”。随后当我把 <font face="NSimsun">$pt</font> 添加到动画中时,获得了通过 <font face="NSimsun">add()</font> 方法返回的名为 <font face="NSimsun">$pts</font> 的新对象。该对象是 <font face="NSimsun">SWFDisplayItem</font>, 表示子图形的实例。然后我可以围绕动画框架的表面逐帧移动实例。 这有点儿混乱,但我可以拥有同时移动的多个版本的 “1000” 文本子图形或 “points” 文本子图形。

绘制一些图形

接下来要处理的是矢量图形。首先仅绘制一条简单的直线,它从框架的左侧顶部到右侧底部。


清单 4. Line.php
				<br><?php <BR>$m = new SWFMovie();<br>$m->setDimension( 300, 300 );<br><br>$s = new SWFShape();<br>$s->setLine( 10, 0, 0, 0 );<br>$s->movePenTo( 10, 10 );<br>$s->drawLineTo( 290, 290 );<br>$m->add( $s );<br><br>$m->save( 'line.swf' );<br>?><br>
Copy after login

在命令行中运行此脚本,然后查看输出的 .swf 文件,效果如图 3 所示。


图 3. 绘制简单的直线
绘制简单的直线

好的 —— 这十分简单,也不怎么令人激动。那么我做了什么?创建了一个新的 <font face="NSimsun">SWFShape</font> 对象,然后向其中添加了一些笔触移动和直线。然后我将其作为子图形添加到了动画中。

为了让它变得更有趣,我使用了与刚才文本中使用的相同的帧式动画。但在本例中,我用下面所示的代码使这条直线围绕动画的中心旋转。


清单 5. 旋转直线
				<br><?php <BR>$m = new SWFMovie();<br>$m->setDimension( 300, 300 );<br><br>$s = new SWFShape();<br>$s->setLine( 5, 0, 0, 0 );<br>$s->movePenTo( -100, -100 );<br>$s->drawLineTo( 100, 100 );<br>$ts = $m->add( $s );<br><br>$ts->moveTo( 150, 150 );<br><br>for( $i = 0; $i  $ts->rotate( 10 );<br> $m->nextframe();<br>}<br><br>$m->save( 'rotate.swf' );<br>?><br>
Copy after login

在本例中,我从 -100, -100 到 100, 100 画了一条直线。这将把直线的中心放在坐标 0,0 处。这样,当我在旋转图形时,直线的中心将发生旋转。

当我向动画中添加图形时,将移动返回到框架中心的 <font face="NSimsun">SWFDisplayItem</font>。然后用 <font face="NSimsun">rotate()</font> 方法使它旋转并每旋转一周就增大其框架。

使用图片

文本和诸如直线、圆、弧、曲线和矩形之类的简单矢量图形都是十分优秀的,但在理想的情况下,您必须能访问这些 Flash 动画中的图片。值得庆幸的是,Ming 库使您可以轻松的使用图片,如下所示。


清单 6. 使用图片
				<br><?php <BR>$img = new SWFBitmap( file_get_contents( 'megan.jpg' ) );<br><br>$s = new SWFShape();<br>$imgf = $s->addFill( $img );<br>$s->setRightFill( $imgf );<br>$s->movePenTo( 0, 0 );<br>$s->drawLineTo( $img->getWidth(), 0 );<br>$s->drawLineTo( $img->getWidth(), $img->getHeight() );<br>$s->drawLineTo( 0, $img->getHeight() );<br>$s->drawLineTo( 0, 0 );<br><br>$m = new SWFMovie();<br>$m->setDimension( $img->getWidth() * 2, $img->getHeight() * 2 );<br>$is = $m->add( $s );<br>$is->moveTo( $img->getWidth() / 2, $img->getHeight() / 2 );<br><br>for( $i = 0; $i { <br>$is->skewx( 0.02 );<br>$is->skewy( -0.03 );<br>$m->nextframe();<br>}<br><br>$m->save( 'image.swf' );<br>?><br>
Copy after login

在命令行中运行此脚本并在浏览器中查看 image.swf,结果如图 4 所示。


图 4. 生成的图片动画
生成的图片动画

此脚本在开始时读取了本地的 .jpeg 文件(在本例中,是我女儿 Megan 的照片)。然后创建一个矩形,并在其中填充图片。在那之后,它在10 帧处使用了位移效果使图片稍微移动。

继续移动

我只是触及了 Ming 库可为您提供的操作的表面。在这里我没有展示交互部分,在交互部分您可以将简单的脚本与元素连接起来。(但是,如果换成是交互操作,如果您有一个十分复杂 的 Flash 动画,则可能需要考虑使用 Flash 开发工具来构建 Web 应用程序内与 Web 服务对话的 Flash 动画。)

构建更加复杂的 Flash 动画的另外一种选择是使用诸如 Adobe Flex 或 Laszlo 之类的制作工具,这两种工具都提供了用于为 Flash 动画的用户界面布局的 XML 语法以及一个更轻松地例程,可用于开发为界面提供互动操作的 JavaScript。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles