内容正文:
/182
第7章 PHP图像处理
7.1 配置GD库
7.2 图像基本操作
7.3 绘制图形
7.4 绘制文本
/182
7.1 配置GD库
7.1.1 加载GD库
若要通过PHP调用GD 2库中的图像处理函数,必须确保PHP以
模块方式运行,还要对配置文件php.ini进行修改:
在该文件中查找“extension=php_gd2.dll”,找到后去掉前面的分号。保存php.ini,重启Apache服务器。
7.1.2 获取GD库信息
用gd_info()函数来取得当前安装的GD库的相关信息:
array gd_info ( void )
第8章目录
/182
7.2 图像基本操作
7.2.1 创建图像
创建空白图像
用imagecreate()函数新建一个基于调色板的图像:
resource imagecreate(int $x_size,int $y_size)
用imagecreatetruecolor()函数新建一个真彩色图像:
resource imagecreatetruecolor(int $x_size,int $y_size)
用imagedestroy()函数来销毁图像:
bool imagedestroy(resource $image)
基于文件创建图像
用imagecreatefromgif()函数从一个GIF文件或URL新建一个图像:
resource imagecreatefromgif(string $filename)
用imagecreatefromjpeg()函数从一个JPEG文件或URL新建图像:
resource imagecreatefromjpeg(string $filename)
用imagecreatefrompng函数从PNG文件或URL新建一个图像:
resource imagecreatefrompng(string $filename)
第8章目录
/182
7.2 图像基本操作
7.2.2 输出图像
用header()函数来设置输出文件的MIME类型:
header("Content-type:image/gif");
header("Content-type:image/jpeg");
header("Content-type:image/png");
用imagegif()函数可以通过GIF格式将图像输出到浏览器或文件:
bool imagegif(resource $image[,string $filename])
用imagejpeg()函数可以通过JPEG格式将图像输出到浏览器或文件:
bool imagejpeg(resource $image[,string $filename[,int $quality]])
用imagepng()函数可以通过PNG格式将图像输出到浏览器或文件:
bool imagepng(resource $image[,string $filename])
第8章目录
/182
7.2 图像基本操作
7.2.3 分配颜色
用函数imagecolorallocate()为一幅图像分配颜色:
int imagecolorallocate(resource $image,int $red,int $green,int $blue)
用imagecolorallocatealpha()函数为一幅图像分配颜色和透明度:
int imagecolorallocatealpha(resource $image,int $red,int $green,int
$blue,int $alpha)
第8章目录
/182
7.3 绘制图形
7.3.1 绘制像素
用imagesetpixel()函数来画一个单一像素:
bool imagesetpixel(resource $image,int $x,int $y,int $color)
其中image参数表示图像标识符,是创建图像函数的返回值;x、y参数指定像素在图像上的位置,图像左上角的坐标为(0,0);color参数指定画该像素使用的颜色,是分配颜色函数的返回值。
imagesetpixel()函数在image图像中用color颜色在(x,y)坐标处画一个点。
第8章目录
/182
7.3 绘制图形
7.3.2 绘制轮廓图形
用imageline()函数在图像中绘制一条线段:
bool imageline(resource $image,int $x1,int $y1,int $x2,int $y2,int $color)
用imagedashedline()函数在图像中绘制一条虚线:
bool imagedashedline(r