#[PHP] GD库(十一)图像填充 imagefill、imagefilledarc 与 imagefilledrectangle() 函数
- imagefill() 函数用于图像区域填充。
- imagefilledarc() 函数画一椭圆弧并填充。
- imagefilledrectangle() 函数画一矩形并填充。
- imagefilledpolygon() 函数画一多边形并填充。
- imagefill()
- imagefill() 函数用于区域填充。
语法:
bool imagefill( resource image, int x, int y, int color )
x,y 分别为填充的起始 x 坐标和 y 坐标,与 x, y 点颜色相同且相邻的点都会被填充。
例子:
<?php
header("Content-type: image/png");
$im = @imagecreatetruecolor(200, 200);
$red = imagecolorallocate($im, 255, 0, 0);
//用 $red 颜色填充图像
imagefill( $im, 0, 0, $red );
imagepng($im);
imagedestroy($im);
?>
提示:对于用 imagecreate() 建立的图像,第一次调用 imagecolorallocate() 会自动给图像填充背景色。
imagefilledarc()
imagefilledarc() 函数画一椭圆弧并填充。
语法:
bool imagefilledarc( resource image, int cx, int cy, int w, int h, int s, int e, int color, int style )
该函数参数用法可参考绘制椭圆弧函数 imagearc() ,只是本函数增加 style 参数表示填充方式。
style 填充方式说明:
填充方式 | 说明 |
---|---|
IMG_ARC_PIE | 普通填充,产生圆形边界 |
IMG_ARC_CHORD | 只是用直线连接了起始和结束点,与 IMG_ARC_PIE 方式互斥 |
IMG_ARC_NOFIL | 指明弧或弦只有轮廓,不填充 |
IMG_ARC_EDGED | 指明用直线将起始和结束点与中心点相连 |
例子:
<?php
header('Content-type: image/png');
$im = imagecreatetruecolor(100, 100);
$red = imagecolorallocate($im, 255, 0, 0);
imagefilledarc($im, 50, 50, 100, 50, 0, 360 , $red, IMG_ARC_PIE);
imagepng($im);
imagedestroy($im);
?>
该函数典型应用之一是画饼状统计图。
imagefilledrectangle()
imagefilledrectangle() 函数画一矩形并填充。
语法:
bool imagefilledrectangle( resource image, int x1, int y1, int x2, int y2, int color )
x1,y1为左上角左边,x2,y2为右下角坐标。
例子:
<?php
header('Content-type: image/png');
$im = imagecreatetruecolor(200, 200);
$yellow = imagecolorallocate($im, 255, 255, 0);
imagefilledrectangle($im, 20, 150, 40, 200, $yellow);
imagefilledrectangle($im, 50, 80, 70, 200, $yellow);
imagepng($im);
imagedestroy($im);
?>
该函数典型应用之一是柱状统计图。
imagefilledpolygon()
imagefilledpolygon() 函数画一多边形并填充。
语法:
bool imagefilledpolygon( resource image, array points, int num_points, int color )
参数说明:
参数 | 说明 |
---|---|
image | 图像资源,欲绘制多边形的图像 |
points | 按顺序包含有多边形各顶点的 x 和 y 坐标的数组 |
num_points | 顶点的总数,必须大于 3 |
color | 图像的颜色 |
绘制一个用红色填充的六边形例子:
<?php
header('Content-type: image/png');
$points = array(
50, 50, // Point 1 (x, y)
100, 50, // Point 2 (x, y)
150, 100, // Point 3 (x, y)
150, 150, // Point 4 (x, y)
100, 150, // Point 5 (x, y)
50, 100 // Point 6 (x, y)
);
$im = imagecreatetruecolor(200, 200);
$red = imagecolorallocate($im, 255, 0, 0);
imagefilledpolygon($im, $points, 6, $red);
imagepng($im);
imagedestroy($im);
?>