函数

PHP 画图 imageline() 函数用于绘制一条线段。imagesetstyle() 函数用于设定画线风格。imagearc() 函数用于绘制椭圆弧(包括圆弧) ...

PHP 绘制线段与圆弧 imageline、imagesetstyle 与 imagearc 函数

PHP 画图 imageline() 函数用于绘制一条线段。imagesetstyle() 函数用于设定画线风格。imagearc() 函数用于绘制椭圆弧(包括圆弧) ...

imageline()

imageline() 函数用于绘制一条线段。

语法:

bool imageline( resource image, int x1, int y1, int x2, int y2, int color )

用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角坐标为 0,0)画一条线段。

例子:

<?php
header("Content-type: image/png");

$im = @imagecreate(300, 300)or die("创建图像资源失败");
$bg = imagecolorallocate($im, 204, 204, 204);
$red = imagecolorallocate($im, 255, 0, 0);
imageline($im,0,30,200,30,$red);

imagepng($im);
imagedestroy($im);
?>

浏览器输出图像如下:

参考阅读

  • imagecreate():创建一幅空白图像。
  • imagecolorallocate():为图像分配颜色。

imagesetstyle()

imagesetstyle() 设定所有画线的函数(例如 imageline() 和 imagepolygon())在使用特殊颜色 IMG_COLOR_STYLED 或者用 IMG_COLOR_STYLEDBRUSHED 画一行图像时所使用的风格。如果成功则返回 TRUE ,失败则返回 FALSE 。

语法:

bool imagesetstyle( resource image, array style )

style 参数是像素组成的数组。

imageline() 函数配合 imagesetstyle() 可以画一条虚线段:

<?php
header("Content-type: image/png");
$im = @imagecreate(300, 50)or die("创建图像资源失败");
$bg = imagecolorallocate($im, 204, 204, 204);
$red = imagecolorallocate($im, 255, 0, 0);

// 画一条虚线,5 个红色像素,4 个背景像素
$style = array($red, $red, $red, $red, $red, $bg, $bg, $bg, $bg);
imagesetstyle($im, $style);

imageline($im, 0, 20, 200, 20, IMG_COLOR_STYLED);

imagepng($im);
imagedestroy($im);
?>

浏览器输出图像如下:

PHP 绘制虚线示例

imagearc()

imagearc() 函数用于绘制椭圆弧(包括圆弧)。

语法:

bool imagearc(resource image, int cx, int cy, int w, int h, int s, int e, int color )
参数说明:
参数说明
image图像资源,欲绘制椭圆弧的图像
cx椭圆中心 x 坐标
cy椭圆中心 y 坐标
w椭圆宽度
h椭圆高度
s起始点,0 表示 3 点钟方向
e角度,360 表示完全封闭
color图像颜色

例子:

<?php
header("Content-type: image/png");

$im = @imagecreate(200, 200)or die("创建图像资源失败");

$bg = imagecolorallocate($im, 204, 204, 204);
$red = imagecolorallocate($im, 255, 0, 0);

imagearc($im, 100, 100, 150, 150, 0, 360, $red);

imagepng($im);
imagedestroy($im);
?>

该例子绘制一个圆圈,浏览器输出如下:

PHP 绘制椭圆弧示例

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

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

内容有用

联系
顾问

在线
客服
购物车
账号登录

没有账号?立即注册

忘记密码

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

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

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