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 @ 2024-06-27 16:19  黑旗君  阅读(170)  评论(0)    收藏  举报