腾讯云618限量抢购
  1. PHP+MySQL数据库教程
  2. PHP进阶教程
  3. ThinkPHP 3.0/3.1 教程

PHP 图像填充 imagefill、imagefilledarc 与 imagefilledrectangle() 函数

最后更新:2011-01-04 22:58阅读:36052

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_NOFILL 指明弧或弦只有轮廓,不填充
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);
?>

本章节内容共分 10 部分:
  1. 1. PHP 获取图像信息 getimagesize 函数
  2. 2. PHP 创建图像、销毁图像 imagecreate 与 imagedestroy 函数
  3. 3. PHP 分配、取消图像颜色 imagecolorallocate 与 imagecolordeallocate 函数
  4. 4. PHP 拷贝图像 imagecopy 与 imagecopyresized 函数
  5. 5. PHP 绘制线段与圆弧 imageline、imagesetstyle 与 imagearc 函数
  6. 6. PHP 图像处理
  7. 7. PHP 载入图像 imagecreatefrom_gif_jpeg_png 系列函数
  8. 8. PHP 合并图像 imagecopymerge 函数(水印制作实例)
  9. 9. PHP 输出图像 imagegif 、imagejpeg 与 imagepng 函数
  10. 10. PHP 图像填充 imagefill、imagefilledarc 与 imagefilledrectangle() 函数
腾讯云618限量抢购