当前位置: 首页> 函数类别大全> imagestring

imagestring

水平绘制一个字符串
名称:imagestring
分类:图像处理GD
所属语言:php
一句话介绍:水平绘制一个字符串

imagestring 函数

适用PHP版本

PHP 4.0.0 及以上版本

函数说明

imagestring 函数在指定的图像上绘制一串文字。它使用内置的 5px 字体,在图像的指定位置绘制一行文本。

函数语法

int imagestring(resource $image, int $font, int $x, int $y, string $text, int $color)

参数

  • image (resource): 要在其上绘制文本的图像资源。
  • font (int): 字体的大小,PHP 默认使用 5px 字体,这个参数通常取值为 1、2、3、4、5 之间的整数。
  • x (int): 文本开始绘制的位置的 X 坐标。
  • y (int): 文本开始绘制的位置的 Y 坐标。
  • text (string): 要显示的文本内容。
  • color (int): 文本颜色,可以使用 GD 库中分配的颜色资源。

返回值

成功时返回文本的长度,失败时返回 -1。

示例

下面的示例代码展示了如何使用 imagestring 函数在图像上绘制文本:

示例代码的说明

本示例创建了一幅 200x100 像素的图像,设置背景色为白色,然后在图像的坐标 (50, 40) 位置绘制了“Hello, World!”文本,字体大小为 5,文本颜色为黑色。

<?php
// 创建一个 200x100 的图像
$image = imagecreatetruecolor(200, 100);

// 分配背景色为白色
$background_color = imagecolorallocate($image, 255, 255, 255);

// 填充背景色
imagefill($image, 0, 0, $background_color);

// 分配文本颜色为黑色
$text_color = imagecolorallocate($image, 0, 0, 0);

// 在图像上绘制文本
imagestring($image, 5, 50, 40, "Hello, World!", $text_color);

// 设置 Content-Type 为图片
header("Content-Type: image/png");

// 输出图像
imagepng($image);

// 清理内存
imagedestroy($image);
?>

以上代码创建了一个 200x100 的图像,并在该图像的指定位置绘制了文本“Hello, World!”。

同类函数
热门文章