PHP生成图片(带背景图,文本)
$srt = "饭店酒店服务与旅游-饭店服务";
// 创建一个新的空白图像资源
$newWidth = 2250;
$newHeight = 3030;
$newImage = imagecreatetruecolor($newWidth, $newHeight);
// 加载背景图片(这里假设背景图片是jpg格式的)
$backgroundImage = imagecreatefromjpeg('./static/text_bg.jpg');
// 将背景图片复制到新图像上
imagecopy($newImage, $backgroundImage, 0, 0, 0, 0, $newWidth, $newHeight);
// 例如,使用白色字体在图像上添加文本
$textColor = imagecolorallocate($newImage, 0, 0, 0); // 黑色
$fontSize = 24;
$fontFile1 = './static/liguofushouxie.ttf'; // 替换为实际字体文件路径
$fontFile2 = './static/simhei.ttf'; // 替换为实际字体文件路径
$fontFile3 = './static/simkai.ttf'; // 替换为实际字体文件路径
$text = 'Hello, World!';
$textWidth = imagettfbbox($fontSize, 0, $font, $text)[2];
$textX = ($newWidth - $textWidth) / 2; // 居中显示文本
$textY = $newHeight / 2 + $fontSize / 2; // 根据字体大小和位置调整Y坐标
//imagettftext($newImage, $fontSize, 0, $textX, $textY, $textColor, $font, $text);
imagettftext($newImage, $fontSize, 0, 360, 1850, $textColor, $fontFile1, '李耀夏');
imagettftext($newImage, $fontSize, 0, 360, 1850, $textColor, $fontFile2, $str);
// 输出图像到浏览器
header('Content-Type: image/jpeg');
imagepng($newImage);
// 释放图像资源
imagedestroy($newImage);
imagedestroy($backgroundImage);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了