以下是一个使用gdImageStringFT函数的简单示例:
#include <gd.h>
int main() {
// 创建一张新的图像
gdImagePtr image = gdImageCreateTrueColor(400, 300);
// 定义字体颜色和字体大小
int fontColor = gdImageColorAllocate(image, 255, 0, 0); // 红色
double fontSize = 20.0;
// 设置文本输出位置
int x = 100;
int y = 150;
// 设置字体路径,这里假设有一个名为"arial.ttf"的TrueType字体文件
const char* fontPath = "arial.ttf";
// 要显示的字符串
const char* text = "Hello World!";
// 使用gdImageStringFT绘制文本到图像上
int boundingBox[8]; // 文本边界框(左下、右下、右上、左上)
gdImageStringFT(NULL, &boundingBox[0], fontColor, fontPath, fontSize, 0.0, 0, 0, text);
gdImageStringFT(image, &boundingBox[0], fontColor, fontPath, fontSize, angle, x - boundingBox[6], y - boundingBox[7], text);
// 将图像保存到文件中
FILE *outputFile;
outputFile = fopen("output.png", "wb");
gdImagePng(image,outputFile);
fclose(outputFile);
// 清理内存,销毁图像对象
gdImageDestroy(image);
return 0;
}
请确保已安装GD库并链接到相应的头文件和库文件。
内容由零声教学AI助手提供,问题来源于学员提问




