1.如何开启GD库
① phpinfo(); 通过访问此函数文件来查看你的服务器上有没有此模块
② wamp目录 -> bin目录 -> php目录 -> ext目录 -> php_gd2.dll文件
③ php.ini配置文件,通过搜索 php_gd2.dll 可以找到 extension=php_gd2.dll
将其前面的分号去掉,即开启了gd库绘图功能
二、绘画步骤
1.准备画布(颜料)
2.开始绘画
3.输出图像
4.释放资源
示例:
<?php
//1.创建一个画布,颜色
$im = imagecreate(200,200);
$red = imagecolorallocate($im,255,0,0); //创建一个颜色:红色
$blue = imagecolorallocate($im,0,0,255); //创建一个颜色:蓝色
$c1 = imagecolorallocate($im,200,200,200);
//2.开始绘画
imagefill($im,0,0,$c1); //填充背景
//....
//3.输出图像
header("Content-Type: image/jpeg");//设置响应头信息为一个jpeg的图片
imagejpeg($im);//输出一个jpeg图片
//4.销毁
imagedestroy($im); //
三、图片的具体绘制:
3.1 创建一个画布:
imagecreate(宽,高)--创建一个基于256色的画布
imagecreatetruecolor( int x_size, int y_size ) -- 新建一个真彩色图像
//还有基于某个图片的画布(将一张图片放到了画布中)
imagecreatefromgif( string filename )
imagecreatefrompng( string filename )
imagecreatefromjpeg( string filename )
3.2 绘画:
//分配定义颜色
$red = imagecolorallocate($im,255,0,0); //分配一个颜色
//填充背景
bool imagefill(resource image,int x,int y, int color ); //填充背景
//画点
bool imagesetpixel(resource image,int x,int y,int color );//画一个像素点
//画一个线段的函数
bool imageline ( resource image, int x1, int y1, int x2, int y2, int color )
//画矩形框(不填充)
bool imagerectangle ( resource image, int x1, int y1, int x2, int y2, int color )
//画矩形框(填充)
bool imagefilledrectangle ( resource image, int x1, int y1, int x2, int y2, int color )
//绘制多边形
bool imagepolygon ( resource image, array points, int num_points, int color )
bool imagefilledpolygon ( resource image, array points, int num_points, int color )
//绘制椭圆(正圆)
imageellipse ( resource image, int cx, int cy, int w, int h, int color )
imagefilledellipse ( resource image, int cx, int cy, int w, int h, int color )
//绘制圆弧
imagearc ( resource image, int cx, int cy, int w, int h, int s, int e, int color)
imagefilledarc ( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style )
//绘制字串
bool imagestring ( resource image, int font, int x, int y, string s, int col )
bool imagestringup ( resource image, int font, int x, int y, string s, int col )
//绘制文本:
*array imagettftext ( resource image, float size, float angle, int x, int y, int color, string fontfile, string text )
//当上面的字体出现乱码时,使用下面的函数转换编码
string iconv ( string in_charset, string out_charset, string str )
$name="张三";
$str = iconv("ISO8859-1","UTF-8",$name);
$str = iconv("GBK","UTF-8",$name);
$str = iconv("GB2312","UTF-8",$name);
//图片的裁剪、合成、缩放
**bool imagecopyresampled ( resource dst_image, resource src_image, int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h )
* imagesx — 取得图像宽度
* imagesy — 取得图像高度
* array getimagesize ( string $filename [, array &$imageinfo ] ) 取得图像大小
3.3.输出图
header("Content-Type: image/jpeg");//设置响应头信息为一个jpeg的图片
imagejpeg($im);//输出一个jpeg图片
header("Content-Type: image/png");//设置响应头信息为一个png的图片
imagepng($im);//输出一个png图片
//输出到指定文件中(另存为)
imagepng($im,"**.png");
3.4.销毁
imagedestroy($im); // 销毁打开的图片