本文将演示GD库的最基本的使用,说明如何在WEB页面中创建图像。

系统要求:WEB服务器  PHP V5.1.4或者以上版本

PHP5 附带安装GD V2.X ,较早版本可能GD是额外的安装,因此可能未安装,你的第一项工作就是检查自己机器上的GD库版本

-------------

<?php
$array=gd_info ();
foreach ($array as $key=>$val) {
   if ($val===true) {
      $val="Enabled";
   } 
   if ($val===false) {
      $val="Disabled";
   }
   echo "$key: $val <br />\n";
}
?>

--------------

将以上代码复制到任何编辑器中保存为扩展名.php的文件,打开浏览器放到服务器上,即可显示自己的GD库版本

--------------

使用GD创建和处理图像

先上代码,后解释:

------------------

<?php
// file type
header("Content-type: image/png");
// create image
$img = imagecreatetruecolor(300, 350);
// set colors
$blue = imagecolorallocate($img, 0, 0, 255);
$green = imagecolorallocate($img, 0, 255, 0);
// draw two rectangles, one is filled
imagerectangle($img, 20,20,120,120, $blue);
imagefilledrectangle($img, 150,150,250,320, $green);
// display image
imagepng($img);
// release image from memory
imagedestroy($img)
?>

------------------

在基于文本的编辑器中输入上面代码,并将其保存为带有 PHP 扩展名 (.php) 的文件,然后将其放到 Web 服务器中并通过浏览器访问。

上面代码的工作原理:

1.header("Content-type: image/png");//指示文件的类型,GD还可支持JPG GIF等,这里使用PNG

2.imagecreatetruecolor(x, y)此函数创建绘制图像的画布,两个参数分别表示画布的宽和高

3.imagecolorallocate($img, r, g, b);此函数用来定义颜色,第一个参数表示图像的句柄,后面的三个参数表示颜色RGB值

4.imagerectangle($img, x,y,x1,y1, $color);imagefilledrectangle($img, x,y,x1,y1, $color);这两个函数分别用来2个矩形,第一个是空心的,第二个是用颜色填充的,四个参数分为两组,分别表示左上和右下的坐标值

5.imagepng($img);此函数用来显示图像到浏览器上

6.imagedestroy($img);此函数用来销毁内存中的该图像,注意不是让浏览器中的图像消失

-----------------------

创建图像的大体格式与步骤都是如上:

下面说明一些不同形状和不同效果的图像用到的函数:

------------------------

imagefill($img, 0, 0, $color );//此函数用来设置画布的背景颜色

------------------------

imagefilledrectangle($img, 10,10,160,110, $blue);
imagefilledrectangle($img, 40,40,190,140, $green);
imagefilledrectangle($img, 70,70,220,170, $white);
imagefilledrectangle($img, 100,100,250,200, $black);//如上格式会创建四个重叠的矩形,后创建的会覆盖前面创建的,

注意使用之前,用imagecolorallocate($img, r, g, b)先定义好颜色

-------------------------

添加数学公式:

我们可以使用一个循环来创建大小不同的4个逐渐变化矩形,形状就像一条领带一样:源代码如下:

-------------------------

<?php
// file type
header("Content-type: image/png");
// create image
$img = imagecreatetruecolor(400, 400);
// set colors
$green = imagecolorallocate($img, 0, 255, 0);
$red = imagecolorallocate($img, 255, 0, 0);
// set background to green
imagefill($img, 0, 0, $green );
for ($i=1;$i<=25;$i++) {
  imagefilledrectangle($img, $i * 8, $i * 8, $i * 10, $i * 10, $red);
}
// display the image
imagepng($img);
// release image from memory
imagedestroy($img);
?>

------------------------------------

再来看别的图形,例如椭圆和圆的创建:

magefilledellipse($img, x,y,a,b,$color);此函数用来创建椭圆,x,y表示椭圆的中心坐标,a ,b表示椭圆的长半径和短半径,换言之,若a = b,则该图像就为圆

下面提供一个有趣的一系列重叠椭圆的源码:

---------------------

<?php
// file type
header("Content-type: image/png");
// create image
$img = imagecreatetruecolor(400, 400);
// set background to light gray
imagefill($img,0,0,imagecolorallocate($img, 221, 221, 221));
// draw ellipses with random colors
for ($i=20;$i>=1;$i--) {//此处从大到小循环式因为避免后面的椭圆覆盖掉前面的
imagefilledellipse($img, 200,200,$i*14,$i*10,
imagecolorallocate($img, rand(50,255), rand(50,255), rand(50,255)));
}
// display image
imagepng($img);
// release image from memory
imagedestroy($img);
?>

-----------------------------

再提供一个有趣的脸的图像的源代码:

------------------------

<?php
// file type
header("Content-type: image/png");
// create image
$img = imagecreatetruecolor(400, 400);
// set background to white
imagefill($img,0,0,imagecolorallocate($img, 255, 255, 255));
// set colors
$black = imagecolorallocate($img, 0, 0, 0);
$blue = imagecolorallocate($img, 0, 0, 255);
$purple = imagecolorallocate($img, 204, 33, 255);
$orange = imagecolorallocate($img, 255, 66, 0);
$yellow = imagecolorallocate($img, 255, 255, 0);
// draw a border
imagesetthickness($img,4);
imagerectangle($img, 5,5,395,395,$black);
// draw a face
imagefilledellipse($img, 200,200,200,200,$orange);
imagefilledellipse($img, 160,160,20,20,$yellow); // left eye
imagefilledellipse($img, 160,160,10,10,$blue); // left eye
imagefilledellipse($img, 240,160,20,20,$yellow); // right eye
imagefilledellipse($img, 240,160,10,10,$blue); // right eye
imagefilledellipse($img, 200,200,10,20,$blue);  // nose
imagefilledarc($img, 200, 240, 100, 30, 0, 180, $yellow, IMG_ARC_PIE);
// draw three touching vertical ovals
imagefilledellipse($img, 60,60,20,60,$purple);
imagefilledellipse($img, 80,60,20,60,$purple);
imagefilledellipse($img, 100,60,20,60,$purple);
// draw three touching horizontal ovals
imagefilledellipse($img, 200,330,300,20,$blue);
imagefilledellipse($img, 200,350,300,20,$blue);
imagefilledellipse($img, 200,370,300,20,$blue);
// display image
imagepng($img);
// release image from memory
imagedestroy($img);
?>

-------------------------------------

注意:“嘴巴”的创建用到了 imagefilledarc($img, 200, 240, 100, 30, 0, 180, $yellow, IMG_ARC_PIE);此函数表示创建一个弧形。

还有许多更多的图形,比如正弦曲线,辐射曲线,图像的浮雕效果等等,我还在研究之中,会在随后的文章发表。

来源:http://www.unix-center.net/bbs/viewthread.php?tid=11312




posted on 2011-02-18 16:36  jinquan  阅读(173)  评论(0)    收藏  举报