当前位置:首页 > PHP > 关键词 > 函数 > 正文

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

PHP 图像填充 imagefill()函数用于图像区域填充;imagefilledarc函数画一椭圆弧并填充;imagefilledrectangle() 函数画一矩形并填充;imagefilled

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);
?>

本文属原创,转载请注明原文:https://www.zhimatong.com/jiaocheng/322.html

为保证教程的实用性及扩大知识面覆盖,如果您有相似问题而未解决,可联系在线客服免费技术支持。

内容有用

联系
顾问

在线
客服
账号登录

没有账号?立即注册

忘记密码

登录即同意用户协议没有账号? 立即注册
账号注册
我已阅读并同意用户协议
立即注册
注册即同意用户协议已有账号? 立即登录
找回密码

操作步骤:邮箱验证->设置新密码

注册即同意用户协议已有账号? 立即登录