PHP如何生成动态图象?
综述:PHP中有一组图像函数,可以动态生成gif格式的图像数据流并输出到服务器,这样我们就可以按照用户的需求及时地在线生成图像。下面我们以生成一个按钮为例子来谈一谈如何用PHP生成动态图像。
动态图像是怎样被生成的?
让我们先看看下面的代码,如果第一次浏览该页或者用户置表单为空,则默认值为"Go!";如果用户输入了信息并提交表单,这段代码将被重新调用且进行初始化。
<?
If (!isset($string)||$string==""){$string="Go!";}
?>
<form action=button.php>
Button text;
<input type=text name=string value="<? pint $string; ?>">
<input type=submit>
</form>
这个文件的其余的部分就是简单的在表格中输出一些元素和调用其他的文件。
现在,我们开始制作按钮。第一个例子是生成一个黑边儿绿底儿的椭圆文字按钮。我们通过一个标准的<IMG>标签儿来显示按钮图象;SRC的值是生成按钮图象的文件。
<img src="green_ellipse.php?string=<? print rawurlencode($string); ?>">
下面让我们看看生成椭圆的文件"green_ellipse.php"
生成椭圆
这里列出了green_ellipse.php的代码,我们以Header()开头,可以向Apache服务器发从一个HTTP的头信息,这一步告诉Server使用的什么数据,本例中我们使用的是GIF的格式:
<?
Header("Content-Type: image/gif");
//现在我们将生成一幅空的图象,并且将其赋值给变量$id:
$id=ImageCreate($img_width,$img_height);
//图像润色
……
//使用内嵌的五号字在按钮上写上文字,最后给按钮上的文字设置变量$black的颜色:
ImageString($id, 5,($img_center_x-($str_width/2+1), img_center_y-($str_height/2)), $string, $black);
//最后,把图象从内存中道出到标准输出缓冲--这是Server将得到的图象的内容:
ImageGIF($id);
?>
这个程序只适合像"Go!"这样短的按钮,像"Please click here!"这样的长字符串就会不好看。没关系,看看下面这个圆边矩形。
生成更美观的图样
这个例子将生成一个黑边儿,黄色棱角的图样。你可以看到这种方法比第一个例子更适合长字符串的按钮。
如果你看了前面的代码,这里的代码你就容易看懂了。我们将从生成按钮的形状开始。
在每一个字符串的结尾处建立20个象素的边缘,并在字符串的上下各建10个象素的边缘:
<?php
初始化,参数设定
……
在图象的左端画了一个半圆,再从六点通过九点画到12点:
ImageArc($id,20,$img_center_y,20,$str_height+20,90,270,$black);
对于右端,是同上面一样的,只是从右端20象素开始画起,并且掠过相反的半圈儿--从12点通过3点到六点:
ImageArc($id,($img_width-20),$img_center_y,20,$img_height+20,270,90,$black);
最后,从一端的端顶向另一端画一条直线,同样在底部也画一条直线。这样闭合了形状的内部。
ImageLine($id,20,0,($img_width-20),0,$black);
ImageLine($id,20,$img_height-1,($img_width-20),$img_height-1,$black);
在这个区域里填充变量$yellow所定义的颜色,并充满整个区域:
ImageFillToBorder($id,$img_center_x,$img_center_y,$black,$yellow);
再将字符串写入到图象中,之后向标准输出打印图象:
ImageString($id, 5, ($img_center_x-($str_width/2)+1,($img_center_y-($str_height/2)), $string, $black);
ImageGIF($id);
?>
如何修改一幅已经存在的图象?
在这里,我们将修改一幅已经存在的图象,将其存为一幅新的图象文件,同时显示一幅动态的图象。首先,我们把mymonkdy.gif调入内存,并将其赋值给$id。
<?
$id=ImageCreateFromGif("mymonkey.gif");
//现在我们设置字体颜色,并将它放到图象上。
$yellow=ImageColorAllocate($id,,255,255,0);
//以左上角为(0,0)点,从(10,20)地方开始用内嵌四号字体写上字符串,同时我将它设为大写,并加了感叹号,以使其显得更加生动。
ImageString($id,4,10,20,strtoupper($string."!"),$yellow);
//现在我们将图象从内存存入文件http://edu.cnzz.cn/NewsInfo/newmonkey.gif。
ImageGIF($id,"http://edu.cnzz.cn/NewsInfo/newmonkey.gif");
?>
这样就差不多了:http://edu.cnzz.cn/NewsInfo/newmonkey.gif可以用来显示了,浏览器的设置是这样的:
<td>
<img src="http://edu.cnzz.cn/NewsInfo/newmonkey.gif" border=0>
</td>
</tr>
</table>
如何在PHP没有GD支持的情况下做统计数据的图形显示?
利用HTML对图形的控制完全可以达到我们的需要,开始之前先让我们回顾一下HTML对图形控制的语法:
< IMG SRC=# ALT=# Width=# Height=# ...... >
我们需要作的就是对Width和Height两个参数的控制,下面我们给个例子:
<?php
$rows[]={12,45,43,23,32,87,14,22,19,40}; //数组初始化,可以从数据取出。
$sums=0;
for ( $i = 0 ; $i < 10 ;$i++ ) $sums+=$rows[$i]; //求和
for ( $i = 0 ; $i < 10 ;$i++ ) {
if ($sums * $rows[$i] ) { //为0则不进行处理,避免0除溢出
$tmp=$rows[$i]/$sums*100;
echo " <img src=http://edu.cnzz.cn/NewsInfo/bar.gif height=9 width=".$tmp.">";
//输出图形,http://edu.cnzz.cn/NewsInfo/bar.gif是只有一个点的图形文件,这里是横向图形,纵向改height即可.
echo " (".sprintf('%01.2f',$tmp)."%)";
echo "<BR>";
}
}
?>
怎样实现PHP和Flash动画的交互操作?
我们用一个flash加上php做身份认证的例子说明一下:
首先在主场景中加入三个textfield和一个按钮。
然后就是给这三个textfield付上名称。
第一个是 name,用来输入用户名
第二个是 password,用来输入密码
第三个是output,用来显示判断结果。
最后就是给按钮的点击加上动作。
Load Variables ("check.php", 1, vars=POST)
这个action的内容很多,第一是把主场景的几个变量值传给check.php.第二还要从check.php中再返回output的值来。
<?
.....
....
///用sql 语句,找出$name的密码赋给 password
//判断第二个文本框的内容,密码是否第一个文本文本框name找到的密码
if($passwd==$password)
echo "output=welcome ".$name." this is a program by php and flash"; //如果等于的话,就将output文本框的值等于欢迎信息
else
echo "output=sorry , ".$name." your password is not correct"; //如果不等于,就显示出错信息。
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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

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,

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

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.

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

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

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.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
