创建图像
时间:2007-06-15 来源:vivin
使用PHP创建一个简单的图像需要以下几个函数:
1. header("Content-type:image/png");输出包含创建图像PNG类型的文件头
2. resource ImageCreate(int x_size, int y_size);
返回一个图像标识符。创建一个基于调色板的空白图像,像素尺寸为x_size × y_size。
3. int imagecolorallocate(resource image, int red, int green, int blue);
返回一个标识符,代表了由给定的RGB成分组成的颜色。这些参与是0到255的整数或者十六
进制的0x00到0xFF。该函数必须被调用以创建用在image所代表的图像中用到的
每一种颜色。
注:第一次对该函数的调用会给基于调色板的图像填充背景色,即用imagecreate()
建立的图像
4. bool imagefill(resource image, int x, int y, int color)
在image图像的坐标x,y(左上角为0,0)处用color颜色执行区域填充(即与x,y点
颜色相同且相邻的点都会被填充)
注:color为事先用imagecolorallocate()定义过的颜色。
实际上imagefill()函数有多个版本,下面仅介绍2个
5. bool imagefilledpolygon(resource image, array points,
int num_points, int color)
在image图像中画一多边形并填充。
points参数是一个按顺序包含有多边形顶点的x和y坐标的数组。
num_points参数是顶点的总数,必须大于3。
例:
// 建立多边形各顶点坐标的数组
$values = array(
40, 50, // Point 1 (x, y)
20, 240, // Point 2 (x, y)
60, 60, // Point 3 (x, y)
240, 20, // Point 4 (x, y)
50, 40, // Point 5 (x, y)
10, 10 // Point 6 (x, y)
);
// 创建图像
$image = imagecreatetruecolor(250, 250);
// 设定颜色
$bg = imagecolorallocate($image, 200, 200, 200);
$blue = imagecolorallocate($image, 0, 0, 255);
// 画一个多边形
imagefilledpolygon($image, $values, 6, $blue);
// 输出图像
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
6. bool imagefilledrectangle(resource image, int x1, int y1,int x2, int y2,
int color)
在image图像中画一个用color填充的矩形,其左上角坐标为(x1,y1),
其右下角坐标为(x2,y2)。(0,0)是图像的左上角。
7. bool imagepng(resource image[, string filename])
将GD图像流(image)以PNG格式输出到标准输出(通常为浏览器),或者,如果用
filename给出了文件名则将其输出到该文件。
8. bool imagedestroy(resource image)
释放与image关联的内存
1. header("Content-type:image/png");输出包含创建图像PNG类型的文件头
2. resource ImageCreate(int x_size, int y_size);
返回一个图像标识符。创建一个基于调色板的空白图像,像素尺寸为x_size × y_size。
3. int imagecolorallocate(resource image, int red, int green, int blue);
返回一个标识符,代表了由给定的RGB成分组成的颜色。这些参与是0到255的整数或者十六
进制的0x00到0xFF。该函数必须被调用以创建用在image所代表的图像中用到的
每一种颜色。
注:第一次对该函数的调用会给基于调色板的图像填充背景色,即用imagecreate()
建立的图像
4. bool imagefill(resource image, int x, int y, int color)
在image图像的坐标x,y(左上角为0,0)处用color颜色执行区域填充(即与x,y点
颜色相同且相邻的点都会被填充)
注:color为事先用imagecolorallocate()定义过的颜色。
实际上imagefill()函数有多个版本,下面仅介绍2个
5. bool imagefilledpolygon(resource image, array points,
int num_points, int color)
在image图像中画一多边形并填充。
points参数是一个按顺序包含有多边形顶点的x和y坐标的数组。
num_points参数是顶点的总数,必须大于3。
例:
// 建立多边形各顶点坐标的数组
$values = array(
40, 50, // Point 1 (x, y)
20, 240, // Point 2 (x, y)
60, 60, // Point 3 (x, y)
240, 20, // Point 4 (x, y)
50, 40, // Point 5 (x, y)
10, 10 // Point 6 (x, y)
);
// 创建图像
$image = imagecreatetruecolor(250, 250);
// 设定颜色
$bg = imagecolorallocate($image, 200, 200, 200);
$blue = imagecolorallocate($image, 0, 0, 255);
// 画一个多边形
imagefilledpolygon($image, $values, 6, $blue);
// 输出图像
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>
6. bool imagefilledrectangle(resource image, int x1, int y1,int x2, int y2,
int color)
在image图像中画一个用color填充的矩形,其左上角坐标为(x1,y1),
其右下角坐标为(x2,y2)。(0,0)是图像的左上角。
7. bool imagepng(resource image[, string filename])
将GD图像流(image)以PNG格式输出到标准输出(通常为浏览器),或者,如果用
filename给出了文件名则将其输出到该文件。
8. bool imagedestroy(resource image)
释放与image关联的内存
相关阅读 更多 +
排行榜 更多 +