GD
一、概述
PHP用于处理动态图片。
用途:
- 验证码
- 水印
- 图片的缩放和剪切
- 动态图表
二、安装
1、确认是否安装
<?php
phpinfo();
2、编辑PHP的配置文件(php.ini)
扩展文件所在目录
extension_dir
extension=php_gd2.dll 去掉前面的分号。
3、重启网络服务器(APACHE)
4、再次确认是否安装
三、应用
1、imagecreatetruecolor(int $width,int $height)
功能:产生GD资源
参数:width 画布的宽
height 画布的高
返回:GD资源
2、imagecolorallocate(resource $img,int $red,int $green,int $blue)
功能:生成颜色
参数:img GD资源
red 红色 (0~255)
green 绿色 (0~255)
blue 蓝色 (0~255)
3、imagefill(resource $img,int $x,int $y,int $color)
功能:填充画布
参数:img GD资源
x,y 填充背景色的坐标
color 颜色
4、展示
- 通知浏览器输出图片
header("content-type:image/图片格式")
图片格式:png、gif、jpeg.
- GD库函数输出图片
image图片格式(resource $img)
5、释放内存
imagedestroy(resource $img)
功能:释放GD资源占用的内存空间。
参数:img GD资源
6、 imagesetpixel(resource $img,int $x,int $y,int $color)
功能:画点
参数:img GD资源
x,y 点的坐标
color 颜色
7、 imageline(resource $img,int $x_1,int $y_1,int $x_2,int $y_2,int $color)
功能:线
参数:img GD资源
x_1,y_1 起点坐标
x_2,y_2 终点坐标
color 线的颜色
8、 imagerectangle(resource $img,int $x1,int $y1,int $x2,int $y2,int $color)
功能:画空心的矩形
参数:img GD资源
x1,y1 矩形的起点坐标
x2,y2 矩形的终点坐标
color 矩形边框的颜色
9、 imagefilledrectangle(resource $img,int $x1,int $y1,int $x2,int $y2,int $color)
功能:画空心的矩形
参数:img GD资源
x1,y1 矩形的起点坐标
x2,y2 矩形的终点坐标
color 矩形的颜色
10、imageellipse(resource $img,int $x,int $y,int $width,int $height,int $color)
功能:画空心圆
参数:img GD资源
x,y 圆心点坐标
width,height 圆的宽和高
color 边框的颜色
11、imagefilledellipse(resource $img,int $x,int $y,int $width,int $height,int $color)
功能:画实心圆
参数:img GD资源
x,y 圆心点坐标
width,height 圆的宽和高
color 实心圆的颜色
12、imagestring(resource $img,int $font,int $x,int $y,string $text,int $color)
功能:在图片上输出文字
参数:img GD资源
font 字体的大小(1~5)数值越小 字体越小
x,y 文字的起始点坐标
text 文字
color 文字的颜色
13、imagettftext(resource $img,int $size,float $angle,int $x,int $y,int $color,string $ttffile,string $text)
功能:输出文字
参数:img GD资源
size 字体的大小。
angle 文字出现的角度
x,y 文字的起始点坐标
color 文字的颜色
ttffile 字体文件的地址(C:\Windows\Fonts)
text 文字
14、image图片格式(resource $img[,string $filename])
功能:保存图片
参数:
img 图片的GD资源
filename 保存图片的地址。注意:图片文件的扩展名称要与函数中指定的图片格式保持一致。
四、验证码
1、安全
2、制作
背景(浅色 颜色随机)
干扰 (点、线)
文字
输出
五、应用不同的背景画图
1、imagecreatefrom图片格式(string $filename)
功能:从现有图片上获取图片资源
参数:filename 获取图片资源的图片文件地址
返回:GD资源
注:图片格式要求必须与获取图片资源的图片文件格式一致。
六、水印
1、getimagesize(string $filename)
功能:获取图片的信息
参数:filename 图片文件的地址
返回:图片的信息,索引数组
0 - 宽
1 - 高
2 - 图片的格式(1=>gif、2=>jpeg、3=>png)
2、imagefontwidth(int $font)
功能:获取字符的宽度
参数:font (1~5)获取imagestring中字体的宽度
返回:字符的宽度
3、imagefontheight(int $font)
功能:获取字符的高度
参数:font (1~5)获取imagestring中字体的高度
返回:字符的高度
4、imagecopy(resource $dst_img,resource $src_img,int $dst_x,int $dst_y,int $src_x,int $src_y,
int $src_w,int $src_h)
功能:合并图片
参数:dst_img 目标图片资源
src_img 水印图片资源
dst_x,dst_y 水印图片贴在目标图片上的坐标
src_x,src_y 水印图片起始点坐标
src_w,src_h 水印图片的宽和高
七、图片的缩放和剪切
imagecopyresampled(resource $dst_img,resource $src_img,int $dst_x,int $dst_y,int $src_x,int $src_y,int $dst_w,int $dst_h,int $src_w,int $src_h)
功能:图片的缩放和剪切
参数:dst_img 目标图片资源
src_img 原图片资源
dst_x,dst_y 目标图片的起始点坐标
src_x,src_y 原图片起始点坐标
dst_w,dst_h 目标图片的尺寸
src_w,src_h 原图片的尺寸
八、图片的旋转和翻转
imagerotate(resource $src_img,float $angle,int $bgcolor)
功能:图片的旋转
参数:src_img 被旋转的图片的资源
angle 旋转的角度
bgcolor 旋转后的背景色
返回:资源 旋转后的图片资源
还不快抢沙发