牛牛在线工具

imagettftext()

收录 8868 个 PHP 函数,含用途说明与用法示例。

imagettftext()

将 TrueType 字体的文本添加到图像上

函数名:imagettftext() 适用版本:PHP 4 >= 4.0.7, PHP 5, PHP 7 用法:imagettftext() 函数用于将 TrueType 字体的文本添加到图像上。 语法:bool imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text ) 参数: $image:图像资源,通过 imagecreatetruecolor() 或者其他图像创建函数创建的图像。 $size:文本的大小,以像素为单位。 $angle:文本的旋转角度,以度为单位(逆时针方向为正)。 $x:文本的起始 x 坐标。 $y:文本的起始 y 坐标。 $color:文本的颜色,可以使用 imagecolorallocate() 函数来创建。 $fontfile:TrueType 字体文件的路径。 $text:要添加的文本。 返回值:成功时返回 true,失败时返回 false。 示例: <?php // 创建一个空白图像 $image = imagecreatetruecolor(400, 200); // 设置背景颜色 $bgColor = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $bgColor); // 设置文本颜色 $textColor = imagecolorallocate($image, 0, 0, 0); // 设置 TrueType 字体文件路径 $fontFile = 'path/to/font.ttf'; // 添加文本到图像 $text = 'Hello World!'; imagettftext($image, 20, 0, 50, 100, $textColor, $fontFile, $text); // 输出图像 header('Content-Type: image/png'); imagepng($image); // 释放图像资源 imagedestroy($image); ?> 以上示例创建了一个空白图像,设置了背景颜色和文本颜色,然后使用 imagettftext() 函数将文本 "Hello World!" 添加到图像上,并输出为 PNG 图片。请确保将 $fontFile 替换为真实的 TrueType 字体文件路径。