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);

posted @   黑旗君  阅读(89)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示