首页 话题 小组 问答 好文 用户 我的社区 域名交易 唠叨

[函数]imagesetpixel()函数—用法及示例

发布于 2025-04-27 14:48:18
0
114

函数名称:imagesetpixel()

适用版本:PHP 4, PHP 5, PHP 7

函数描述:imagesetpixel() 函数用于在给定的图像上绘制一个单一像素的点。

语法:bool imagesetpixel ( resource $image , int $x , int $y , int $color )

参数:

  • $image:必需参数,图像资源,通过 imagecreate() 或 imagecreatetruecolor() 创建。
  • $x:必需参数,要绘制的像素的 x 坐标。
  • $y:必需参数,要绘制的像素的 y 坐标。
  • $color:必需参数,要使用的颜色,可以使用 imagecolorallocate() 函数为图像资源分配颜色。

返回值:成功时返回 true,失败时返回 false。

示例:

// 创建一个 200x200 的图像
$image = imagecreatetruecolor(200, 200);

// 分配一个红色
$red = imagecolorallocate($image, 255, 0, 0);

// 在坐标 (100, 100) 处绘制一个红色的像素点
imagesetpixel($image, 100, 100, $red);

// 输出图像
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);

以上示例创建了一个 200x200 的图像,并在坐标 (100, 100) 处绘制了一个红色的像素点。最后通过 header() 函数设置输出类型为 image/png,然后使用 imagepng() 输出图像。最后使用 imagedestroy() 销毁图像资源。

评论
一个月内的热帖推荐
啊龙
Lv.1普通用户

9545

帖子

31

小组

3242

积分

赞助商广告
站长交流