PHP 中的 imagechar() 函数

phpserver side programmingprogramming更新于 2025/7/6 7:37:17

imagechar() 函数用于水平绘制字符。

语法

bool imagechar( img, font, x, y, ch, color )

参数

  • img:使用 imagecreatetruecolor() 创建图像。

  • font:设置字体大小

  • x:x 坐标

  • y:y 坐标

  • c:要绘制的字符。

  • color:使用imagecolorallocate()。

返回

imagechar() 函数成功时返回 TRUE,失败时返回 FALSE。

示例

以下是示例:

<?php
   $img = imagecreate(400, 300);
   $str = 'Demo';
   $bgcolor = imagecolorallocate($img, 30, 80, 70);
   $textcolor = imagecolorallocate($img, 255, 255, 255);
   imagechar($img, 80, 100, 90, $str, $textcolor);
   header('Content-type: image/png');
   imagepng($img);
?>

输出

输出结果如下:

示例

让我们看另一个示例,其中我们使用不同的坐标和高度/宽度绘制一个字符,如上例所示:

<?php
   $img = imagecreate(350, 270);
   $str = 'Example';
   $bgcolor = imagecolorallocate($img, 70, 100, 130);
   $textcolor = imagecolorallocate($img, 200, 195, 210);
   imagechar($img, 100, 50, 70, $str, $textcolor);
   header('Content-type: image/png');
   imagepng($img);
?>

输出

输出结果如下:


相关文章