PHP画布

本文介绍了PHP进行图形绘制的方法,包括使用header指令设置内容类型,通过imagecreatetruecolor创建画布,使用imagepng输出图像,利用imagecolorallocate管理颜色,用imagefill填充画布,运用imagesetpixel画点,imageline画线,imagerectangle与imagefilledrectangle绘制矩形,以及imagettftext绘制文字。通过这些函数,可以实现PHP图像处理的各种功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

header指令

  • header('content-type:image/png')
  • header('Content-Type:image/gif');
  • header('Content-Type:image/jpeg');

创建画布

resource imagecreatetruecolor(int $width,int $height) — 新建一个真彩色图像

返回一个图像标识符,代表了一幅大小为width 和height 的黑色图像
返回值:成功后返回图像资源,失败后返回false

//创建画布
$width = 200;
$height =80;
$img =imagecreatetruecolor($width,$height);

输出图像

bool imagepng(resource $image[,string $filename]) — 以png格式将图像输出到浏览器或文件

将GD图像流(image)以PNG格式输出到标准输出(通常为浏览器),或者如果用filename 给出了文件名则将其输出该文件

//输出画布
imagepng($img);

 

颜色管理

int imagecolorallocate(resource $image,int $red ,int $green ,int $blue) — 为一幅画图像分配颜色

red green blue 分别 是所需要的颜色的红 绿 蓝成分 这些参数是0到225的整数或者十六进制的0x00到0xFF

//颜色管理
$color =imagecolorallocate($img,0xcc,0xcc,0xcc);//灰色

填充画布

bool imagefill  ( resource $image  , int $x  , int $y  , int $color  )

image  图像的坐标 x , y (图像左上角为 0, 0)处用 color  颜色执行区域填充(即与 x, y 点颜色相同且相邻的点都会被填充)

//填充画布
imagefill($img,0,0,$color);

销毁图像

 

bool imagedestroy (resource $image) — 释放与image关联的内存

//销毁画布
imagedestroy($img);

 

绘制图形

点:

bool imagesetpixel(resource $image,int $x,int $y,int $color)画一个单一像素

在image图像中用color颜色在x,y坐标(图像为左上角为0,0)上画一个点

//画点
imagesetpixel($img,$x,$y,$color);

线:

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

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

//画线
imageline($img,$x1,$y1,$x2,$y2,$color);

绘制矩形有两种方式

第一种:

bool imagerectangle(resource $image,int $x1,int $y1,int $x2,int $col)

用color颜色在image图像中画一个矩形,其左上角坐标为x1,y1,右下角的坐标为x2,y2。图像的左上角坐标为0,0

 

第二种是颜色填充的矩形:

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

在image图像中画一个用color颜色填充了的矩形 其左上角坐标为x1,y1,右下角坐标为x2,y2。  0,0是图像的左上角
 

绘制文字

array imagettftext(resource $image,float $size ,float $angle ,int $x ,int $y,int $color,string $fontfile ,string $text)

size : 字体的尺寸,根据GD的版本,为像素尺寸(GD1)或点(磅)尺寸(GD2)
angle:角度制表示的角度,0度为从左向右读的文本。更高数值表示逆时针旋转。
            由x,y所表示的坐标定义了第一个字符的基本点(大概是字符的左下角)
color:颜色索引
fontfile: 是想要使用的True Type 字体的路径
text :UTF-8编码的文本字符串

//绘制文字
$color = imagecolorallocate($img,255,0,0);
    $index = rand(0,$len-1);
    $chr = substr($str,$index,1);
    $yzm .= $chr;
    $x = 20 +$i*40;
    $y = 60;
    imagettftext($img,50,rand(-70,70),$x,$y,$color,$font,$chr);

 

 

 

 

 

### PHP 中使用 GD 库创建画布并进行图像处理 在 PHP 中,GD 庿是一个功能强大的工具集,用于动态生成和操作图像。通过 GD 库可以实现从简单图形到复杂图表的各种图像处理需求。以下是几个关键的函数及其用途: #### 创建画布的核心函数 - **`imagecreatetruecolor(int $width, int $height)`**: 此函数用于创建一个新的真彩色图像资源对象,指定宽度和高度作为参数。这是最常用的创建画布的方法之一[^1]。 ```php $image = imagecreatetruecolor(800, 600); ``` - **`imagecreate(int $width, int $height)`**: 类似于 `imagecreatetruecolor()`,但它创建的是调色板模式的图像,通常适用于较小尺寸或颜色较少的情况[^2]。 ```php $image = imagecreate(400, 300); ``` #### 配置画布的颜色和其他属性 一旦创建了画布,可以通过分配颜色等方式进一步配置其外观: - **`imagecolorallocate(resource $image, int $red, int $green, int $blue)`**: 分配一种新的颜色给图像资源对象,并返回该颜色索引值。RGB 参数范围为 0 到 255[^3]。 ```php $black = imagecolorallocate($image, 0, 0, 0); // 黑色 $white = imagecolorallocate($image, 255, 255, 255); // 白色 ``` #### 绘制基本形状 GD 库还提供了绘制线条、矩形、圆形等功能的支持: - **`imageline(resource $image, int $x1, int $y1, int $x2, int $y2, int $color)`**: 在两个坐标之间绘制一条直线。 ```php imageline($image, 50, 50, 750, 550, $black); ``` - **`imagerectangle(resource $image, int $x1, int $y1, int $x2, int $y2, int $color)`**: 绘制一个由两组对角顶点定义的矩形框。 ```php imagerectangle($image, 100, 100, 700, 500, $white); ``` - **`imageellipse(resource $image, int $cx, int $cy, int $width, int $height, int $color)`**: 绘制椭圆或圆形,其中 `(cx, cy)` 是中心点位置。 ```php imageellipse($image, 400, 300, 300, 200, $black); ``` #### 输出与保存图像 完成绘图后,可以选择将最终结果输出至浏览器或者保存成文件: - **`header(string $string)` 和 `imagepng/resource/jpg/gif(resource $image [, string $filename])`**: 设置 HTTP 头部信息并将图像数据发送到客户端;如果指定了 `$filename` 参数,则会把图像写入磁盘[^4]。 ```php // 将 PNG 格式的图像直接显示在网页上 header('Content-Type: image/png'); imagepng($image); // 或者保存为本地文件 imagepng($image, 'output.png'); // 清理内存占用 imagedestroy($image); ``` 以上就是利用 PHP 的 GD 库创建画布以及执行一些基础图像处理的主要流程和技术要点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值