GD2绘制基本图形

一 代码

<?php
    header("Content-type: image/png");    //将图像输出到浏览器
    $img = imagecreate(560, 200);      //创建一个 560×200 的图像
    $bg = imagecolorallocate($img, 0, 0, 255);         
     //设置图像背景色
    $white = imagecolorallocate($img, 255, 255, 255);       
     //设置绘制图像的颜色为白色
	imageline($img, 20, 20, 150, 180, $white);
    imagearc($img, 250, 100, 150, 150, 0, 360, $white);      
    //绘制一个白色的圆
    imagerectangle($img, 350, 20, 500, 170, $white);
	imagepng($img);
	imagedestroy($img); 
?>

 

二 运行结果

 
  • 大小: 1.9 KB
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 PHP 中,可以使用 GD 库来绘制基本图形,包括直线、圆、矩形等。 以下是几个常用的绘图函数: 1. `imageline()`:绘制直线 ```php imageline($image, $x1, $y1, $x2, $y2, $color); ``` 参数说明: - `$image`:图像资源标识符 - `$x1`:起始点 x 坐标 - `$y1`:起始点 y 坐标 - `$x2`:终点 x 坐标 - `$y2`:终点 y 坐标 - `$color`:颜色标识符,可以使用 `imagecolorallocate()` 函数生成 示例代码: ```php // 创建一个 200x200 的空白图像 $image = imagecreatetruecolor(200, 200); // 分配颜色 $color = imagecolorallocate($image, 255, 0, 0); // 绘制一条红色直线 imageline($image, 0, 0, 200, 200, $color); // 输出图像 header('Content-Type: image/png'); imagepng($image); // 释放内存 imagedestroy($image); ``` 2. `imageellipse()`:绘制椭圆 ```php imageellipse($image, $cx, $cy, $width, $height, $color); ``` 参数说明: - `$image`:图像资源标识符 - `$cx`:椭圆中心点 x 坐标 - `$cy`:椭圆中心点 y 坐标 - `$width`:椭圆宽度 - `$height`:椭圆高度 - `$color`:颜色标识符,可以使用 `imagecolorallocate()` 函数生成 示例代码: ```php // 创建一个 200x200 的空白图像 $image = imagecreatetruecolor(200, 200); // 分配颜色 $color = imagecolorallocate($image, 0, 0, 255); // 绘制一个蓝色椭圆 imageellipse($image, 100, 100, 150, 100, $color); // 输出图像 header('Content-Type: image/png'); imagepng($image); // 释放内存 imagedestroy($image); ``` 3. `imagerectangle()`:绘制矩形 ```php imagerectangle($image, $x1, $y1, $x2, $y2, $color); ``` 参数说明: - `$image`:图像资源标识符 - `$x1`:左上角 x 坐标 - `$y1`:左上角 y 坐标 - `$x2`:右下角 x 坐标 - `$y2`:右下角 y 坐标 - `$color`:颜色标识符,可以使用 `imagecolorallocate()` 函数生成 示例代码: ```php // 创建一个 200x200 的空白图像 $image = imagecreatetruecolor(200, 200); // 分配颜色 $color = imagecolorallocate($image, 0, 255, 0); // 绘制一个绿色矩形 imagerectangle($image, 50, 50, 150, 150, $color); // 输出图像 header('Content-Type: image/png'); imagepng($image); // 释放内存 imagedestroy($image); ``` 除了以上几个函数外,还有许多其他绘图函数,可以根据需要选择使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值